refactor: add `service.SimpleHTTP` to reduce required boilerplate

This commit is contained in:
ThinkChaos 2024-04-03 13:39:07 -04:00
parent 1515e17f0f
commit 48cad3b786
No known key found for this signature in database
7 changed files with 45 additions and 63 deletions

View File

@ -4,12 +4,11 @@ import (
"github.com/0xERR0R/blocky/config"
"github.com/0xERR0R/blocky/service"
"github.com/0xERR0R/blocky/util"
"github.com/go-chi/chi/v5"
)
// Service implements service.HTTPService.
type Service struct {
service.HTTPInfo
service.SimpleHTTP
}
func NewService(cfg config.APIService, server StrictServerInterface) *Service {
@ -19,21 +18,10 @@ func NewService(cfg config.APIService, server StrictServerInterface) *Service {
)
s := &Service{
HTTPInfo: service.HTTPInfo{
Info: service.Info{
Name: "API",
Endpoints: endpoints,
},
Mux: chi.NewMux(),
},
SimpleHTTP: service.NewSimpleHTTP("API", endpoints),
}
registerOpenAPIEndpoints(s.Mux, server)
return s
}
func (s *Service) Merge(other service.Service) (service.Merger, error) {
return service.MergeHTTP(s, other)
}

View File

@ -4,13 +4,12 @@ import (
"github.com/0xERR0R/blocky/config"
"github.com/0xERR0R/blocky/service"
"github.com/0xERR0R/blocky/util"
"github.com/go-chi/chi/v5"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// Service implements service.HTTPService.
type Service struct {
service.HTTPInfo
service.SimpleHTTP
}
func NewService(cfg config.MetricsService, metricsCfg config.Metrics) *Service {
@ -25,14 +24,7 @@ func NewService(cfg config.MetricsService, metricsCfg config.Metrics) *Service {
}
s := &Service{
HTTPInfo: service.HTTPInfo{
Info: service.Info{
Name: "Metrics",
Endpoints: endpoints,
},
Mux: chi.NewMux(),
},
SimpleHTTP: service.NewSimpleHTTP("Metrics", endpoints),
}
s.Mux.Handle(
@ -42,7 +34,3 @@ func NewService(cfg config.MetricsService, metricsCfg config.Metrics) *Service {
return s
}
func (s *Service) Merge(other service.Service) (service.Merger, error) {
return service.MergeHTTP(s, other)
}

View File

@ -13,7 +13,7 @@ import (
)
type dohService struct {
service.HTTPInfo
service.SimpleHTTP
handler dnsHandler
}
@ -25,14 +25,7 @@ func newDoHService(cfg config.DoHService, handler dnsHandler) *dohService {
)
s := &dohService{
HTTPInfo: service.HTTPInfo{
Info: service.Info{
Name: "DoH",
Endpoints: endpoints,
},
Mux: chi.NewMux(),
},
SimpleHTTP: service.NewSimpleHTTP("DoH", endpoints),
handler: handler,
}
@ -50,10 +43,6 @@ func newDoHService(cfg config.DoHService, handler dnsHandler) *dohService {
return s
}
func (s *dohService) Merge(other service.Service) (service.Merger, error) {
return service.MergeHTTP(s, other)
}
func (s *dohService) handleGET(rw http.ResponseWriter, req *http.Request) {
dnsParam, ok := req.URL.Query()["dns"]
if !ok || len(dnsParam[0]) < 1 {

View File

@ -19,7 +19,7 @@ import (
// that expose everything. The goal is to split it up
// and remove it.
type httpMiscService struct {
service.HTTPInfo
service.SimpleHTTP
}
func newHTTPMiscService(cfg *config.Config) *httpMiscService {
@ -28,20 +28,13 @@ func newHTTPMiscService(cfg *config.Config) *httpMiscService {
service.EndpointsFromAddrs(service.HTTPSProtocol, cfg.Ports.HTTPS),
)
return &httpMiscService{
HTTPInfo: service.HTTPInfo{
Info: service.Info{
Name: "HTTP",
Endpoints: endpoints,
},
Mux: createHTTPRouter(cfg),
},
s := &httpMiscService{
SimpleHTTP: service.NewSimpleHTTP("API", endpoints),
}
}
func (s *httpMiscService) Merge(other service.Service) (service.Merger, error) {
return service.MergeHTTP(s, other)
configureHTTPRouter(s.Router(), cfg)
return s
}
// httpServer implements subServer for HTTP.

View File

@ -60,9 +60,7 @@ func (s *Server) Query(
return s.resolve(ctx, req)
}
func createHTTPRouter(cfg *config.Config) *chi.Mux {
router := chi.NewRouter()
func configureHTTPRouter(router chi.Router, cfg *config.Config) {
configureDebugHandler(router)
configureDocsHandler(router)
@ -70,11 +68,9 @@ func createHTTPRouter(cfg *config.Config) *chi.Mux {
configureStaticAssetsHandler(router)
configureRootHandler(cfg, router)
return router
}
func configureDocsHandler(router *chi.Mux) {
func configureDocsHandler(router chi.Router) {
router.Get("/docs/openapi.yaml", func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set(contentTypeHeader, yamlContentType)
_, err := writer.Write([]byte(docs.OpenAPI))
@ -82,7 +78,7 @@ func configureDocsHandler(router *chi.Mux) {
})
}
func configureStaticAssetsHandler(router *chi.Mux) {
func configureStaticAssetsHandler(router chi.Router) {
assets, err := web.Assets()
util.FatalOnError("unable to load static asset files", err)
@ -90,7 +86,7 @@ func configureStaticAssetsHandler(router *chi.Mux) {
router.Handle("/static/*", http.StripPrefix("/static/", fs))
}
func configureRootHandler(cfg *config.Config, router *chi.Mux) {
func configureRootHandler(cfg *config.Config, router chi.Router) {
router.Get("/", func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set(contentTypeHeader, htmlContentType)
t := template.New("index")
@ -145,6 +141,6 @@ func logAndResponseWithError(err error, message string, writer http.ResponseWrit
}
}
func configureDebugHandler(router *chi.Mux) {
func configureDebugHandler(router chi.Router) {
router.Mount("/debug", middleware.Profiler())
}

View File

@ -30,8 +30,29 @@ type HTTPInfo struct {
Mux *chi.Mux
}
func NewHTTPInfo(name string, endpoints []Endpoint) HTTPInfo {
return HTTPInfo{
Info: NewInfo(name, endpoints),
Mux: chi.NewMux(),
}
}
func (i *HTTPInfo) Router() chi.Router { return i.Mux }
var _ HTTPService = (*SimpleHTTP)(nil)
// SimpleHTTP implements HTTPService usinig the default HTTP merger.
type SimpleHTTP struct{ HTTPInfo }
func NewSimpleHTTP(name string, endpoints []Endpoint) SimpleHTTP {
return SimpleHTTP{HTTPInfo: NewHTTPInfo(name, endpoints)}
}
func (s *SimpleHTTP) Merge(other Service) (Merger, error) {
return MergeHTTP(s, other)
}
// MergeHTTP merges two compatible HTTPServices.
//
// The second parameter is of type `Service` to make it easy to call

View File

@ -49,6 +49,13 @@ type Info struct {
Endpoints []Endpoint
}
func NewInfo(name string, endpoints []Endpoint) Info {
return Info{
Name: name,
Endpoints: endpoints,
}
}
func (i *Info) ServiceName() string { return i.Name }
func (i *Info) ExposeOn() []Endpoint { return i.Endpoints }
func (i *Info) String() string { return svcString(i) }