added REST API and simple unit test - and compose starter, still WIP for integ test
This commit is contained in:
43
api/api.go
Normal file
43
api/api.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/chi/middleware"
|
||||
"github.com/go-chi/render"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func ApiInit() {
|
||||
log.Println("Initializing API server")
|
||||
r := chi.NewRouter()
|
||||
addMiddleware(r)
|
||||
addRoutes(r)
|
||||
start(r)
|
||||
}
|
||||
|
||||
func addMiddleware(mux *chi.Mux) {
|
||||
mux.Use(middleware.RequestID)
|
||||
mux.Use(middleware.Logger)
|
||||
mux.Use(middleware.Recoverer)
|
||||
mux.Use(middleware.URLFormat)
|
||||
mux.Use(render.SetContentType(render.ContentTypeJSON))
|
||||
mux.Use(middleware.Timeout(60 * time.Second)) // Set a timeout
|
||||
}
|
||||
|
||||
func addRoutes(mux *chi.Mux) {
|
||||
// Add a simple resource
|
||||
mux.Mount("/app", AppResource{}.Routes())
|
||||
// Live a healthy life!
|
||||
mux.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("yolo"))
|
||||
})
|
||||
}
|
||||
|
||||
func start(mux *chi.Mux) {
|
||||
err := http.ListenAndServe(":8080", mux)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user