|
|
|
/*
|
|
|
|
------------------------------------------------------------------------------------------------------------------------
|
|
|
|
####### router ####### Copyright (c) 2021 mls-361 ################################################## MIT License #######
|
|
|
|
------------------------------------------------------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
|
|
|
"expvar"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"forge.chapril.org/mls-361/jsonapi"
|
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
|
|
|
|
"forge.chapril.org/armen/armen/internal/components"
|
|
|
|
)
|
|
|
|
|
|
|
|
type router struct {
|
|
|
|
mux *httprouter.Router
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRouter() *router {
|
|
|
|
return &router{
|
|
|
|
mux: httprouter.New(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build AFAIRE.
|
|
|
|
func Build(_ *components.Components) components.Router {
|
|
|
|
cr := newRouter()
|
|
|
|
|
|
|
|
cr.mux.PanicHandler = func(rw http.ResponseWriter, _ *http.Request, _ interface{}) {
|
|
|
|
//AFINIR: logger
|
|
|
|
rw.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
cr.mux.Handle(
|
|
|
|
http.MethodGet,
|
|
|
|
"/debug",
|
|
|
|
func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
|
expvar.Handler().ServeHTTP(w, r)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
cr.mux.Handle(
|
|
|
|
http.MethodGet,
|
|
|
|
"/status",
|
|
|
|
func(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
return cr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handler AFAIRE.
|
|
|
|
func (cr *router) Handler() http.Handler {
|
|
|
|
return cr.mux
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get AFAIRE.
|
|
|
|
func (cr *router) Get(path string, handler http.Handler) {
|
|
|
|
cr.mux.Handler(http.MethodGet, path, handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeFiles AFAIRE.
|
|
|
|
func (cr *router) ServeFiles(path string, root http.FileSystem) {
|
|
|
|
cr.mux.ServeFiles(path, root)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cr *router) handleJSON(method string, path string, handler jsonapi.Handler) {
|
|
|
|
cr.mux.Handle(
|
|
|
|
method,
|
|
|
|
path,
|
|
|
|
func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
|
var mp map[string]string
|
|
|
|
|
|
|
|
if len(ps) > 0 {
|
|
|
|
mp = make(map[string]string, len(ps))
|
|
|
|
|
|
|
|
for _, p := range ps {
|
|
|
|
mp[p.Key] = p.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handler.Serve(jsonapi.NewRequest(r, w, mp))
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetJSON AFAIRE.
|
|
|
|
func (cr *router) GetJSON(path string, handler jsonapi.Handler) {
|
|
|
|
cr.handleJSON(http.MethodGet, path, handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PostJSON AFAIRE.
|
|
|
|
func (cr *router) PostJSON(path string, handler jsonapi.Handler) {
|
|
|
|
cr.handleJSON(http.MethodPost, path, handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
######################################################################################################## @(°_°)@ #######
|
|
|
|
*/
|