-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.go
36 lines (25 loc) · 893 Bytes
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package rest
import (
"encoding/json"
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/renanferr/swapi-golang-rest-api/pkg/adding"
"github.com/renanferr/swapi-golang-rest-api/pkg/http/rest/planets"
"github.com/renanferr/swapi-golang-rest-api/pkg/listing"
)
const ApiPrefix = "/api"
func Handler(a adding.Service, l listing.Service) http.Handler {
apiRouter := chi.NewRouter()
apiRouter.Use(middleware.Recoverer, middleware.RequestID, middleware.Logger)
apiRouter.Mount("/planets", planets.Handler(a, l))
router := chi.NewRouter()
router.Get("/healthcheck", handleHealthcheck)
router.Mount(ApiPrefix, apiRouter)
return router
}
func handleHealthcheck(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Add("Content-type", "application/json")
json.NewEncoder(w).Encode(map[string]bool{"alive": true})
}