56 lines
1.8 KiB
Go
56 lines
1.8 KiB
Go
package endpoint
|
|
|
|
import (
|
|
"net/http"
|
|
"path"
|
|
|
|
"git.sendico.io/tech/sxchnge/product/auth/response"
|
|
api "git.sendico.io/tech/sxchnge/shared/http"
|
|
"git.sendico.io/tech/sxchnge/shared/service"
|
|
"github.com/go-chi/chi/v5"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func (er *HttpEndpointRouter) chooseMethod(method api.HTTPMethod) RegistratorT {
|
|
switch method {
|
|
case api.Get:
|
|
return func(r chi.Router, p string, h http.HandlerFunc) { r.Get(p, h) }
|
|
case api.Post:
|
|
return func(r chi.Router, p string, h http.HandlerFunc) { r.Post(p, h) }
|
|
case api.Put:
|
|
return func(r chi.Router, p string, h http.HandlerFunc) { r.Put(p, h) }
|
|
case api.Delete:
|
|
return func(r chi.Router, p string, h http.HandlerFunc) { r.Delete(p, h) }
|
|
case api.Patch:
|
|
return func(r chi.Router, p string, h http.HandlerFunc) { r.Patch(p, h) }
|
|
case api.Options:
|
|
return func(r chi.Router, p string, h http.HandlerFunc) { r.Options(p, h) }
|
|
case api.Head:
|
|
return func(r chi.Router, p string, h http.HandlerFunc) { r.Head(p, h) }
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (er *HttpEndpointRouter) endpoint(service mservice.Type, handler string) string {
|
|
return path.Join(er.apiEndpoint, service, handler)
|
|
}
|
|
|
|
func (er *HttpEndpointRouter) InstallHandler(service mservice.Type, endpoint string, method api.HTTPMethod, handler sresponse.HandlerFunc) {
|
|
ep := er.endpoint(service, endpoint)
|
|
hm := er.chooseMethod(method)
|
|
if hm == nil {
|
|
er.logger.Warn("Handler install skipped because method is unsupported",
|
|
zap.String("endpoint", ep),
|
|
zap.String("method", api.HTTPMethod2String(method)),
|
|
)
|
|
return
|
|
}
|
|
hndlr := func(w http.ResponseWriter, r *http.Request) {
|
|
res := handler(r)
|
|
res(w, r)
|
|
}
|
|
hm(er.router, ep, hndlr)
|
|
er.logger.Info("Handler installed", zap.String("endpoint", ep), zap.String("method", api.HTTPMethod2String(method)))
|
|
}
|