37 lines
1.0 KiB
Go
37 lines
1.0 KiB
Go
package routes
|
|
|
|
import (
|
|
"encoding/json"
|
|
"gitea.deepak.science/deepak/gogmagog/models"
|
|
"github.com/go-chi/chi"
|
|
"net/http"
|
|
)
|
|
|
|
// NewRouter returns a router powered by the provided model.
|
|
func NewRouter(m *models.Model) http.Handler {
|
|
router := chi.NewRouter()
|
|
router.MethodNotAllowed(methodNotAllowedHandler)
|
|
router.NotFound(notFoundHandler)
|
|
router.Mount("/plans", newPlanRouter(m))
|
|
router.Mount("/health", newHealthRouter(m))
|
|
router.Get("/ping", ping)
|
|
return router
|
|
}
|
|
func methodNotAllowedHandler(w http.ResponseWriter, r *http.Request) {
|
|
code := http.StatusMethodNotAllowed
|
|
http.Error(w, http.StatusText(code), code)
|
|
}
|
|
func notFoundHandler(w http.ResponseWriter, r *http.Request) {
|
|
code := http.StatusNotFound
|
|
http.Error(w, http.StatusText(code), code)
|
|
}
|
|
|
|
func ping(w http.ResponseWriter, r *http.Request) {
|
|
// A very simple health check.
|
|
w.Header().Add("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusOK)
|
|
if err := json.NewEncoder(w).Encode(map[string]string{"ping": "pong"}); err != nil {
|
|
serverError(w, err)
|
|
}
|
|
}
|