mirror of https://github.com/0xERR0R/blocky.git
refactor: add `service.SimpleHTTP` to reduce required boilerplate
This commit is contained in:
parent
1515e17f0f
commit
48cad3b786
|
@ -4,12 +4,11 @@ import (
|
||||||
"github.com/0xERR0R/blocky/config"
|
"github.com/0xERR0R/blocky/config"
|
||||||
"github.com/0xERR0R/blocky/service"
|
"github.com/0xERR0R/blocky/service"
|
||||||
"github.com/0xERR0R/blocky/util"
|
"github.com/0xERR0R/blocky/util"
|
||||||
"github.com/go-chi/chi/v5"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Service implements service.HTTPService.
|
// Service implements service.HTTPService.
|
||||||
type Service struct {
|
type Service struct {
|
||||||
service.HTTPInfo
|
service.SimpleHTTP
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewService(cfg config.APIService, server StrictServerInterface) *Service {
|
func NewService(cfg config.APIService, server StrictServerInterface) *Service {
|
||||||
|
@ -19,21 +18,10 @@ func NewService(cfg config.APIService, server StrictServerInterface) *Service {
|
||||||
)
|
)
|
||||||
|
|
||||||
s := &Service{
|
s := &Service{
|
||||||
HTTPInfo: service.HTTPInfo{
|
SimpleHTTP: service.NewSimpleHTTP("API", endpoints),
|
||||||
Info: service.Info{
|
|
||||||
Name: "API",
|
|
||||||
Endpoints: endpoints,
|
|
||||||
},
|
|
||||||
|
|
||||||
Mux: chi.NewMux(),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
registerOpenAPIEndpoints(s.Mux, server)
|
registerOpenAPIEndpoints(s.Mux, server)
|
||||||
|
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) Merge(other service.Service) (service.Merger, error) {
|
|
||||||
return service.MergeHTTP(s, other)
|
|
||||||
}
|
|
||||||
|
|
|
@ -4,13 +4,12 @@ import (
|
||||||
"github.com/0xERR0R/blocky/config"
|
"github.com/0xERR0R/blocky/config"
|
||||||
"github.com/0xERR0R/blocky/service"
|
"github.com/0xERR0R/blocky/service"
|
||||||
"github.com/0xERR0R/blocky/util"
|
"github.com/0xERR0R/blocky/util"
|
||||||
"github.com/go-chi/chi/v5"
|
|
||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Service implements service.HTTPService.
|
// Service implements service.HTTPService.
|
||||||
type Service struct {
|
type Service struct {
|
||||||
service.HTTPInfo
|
service.SimpleHTTP
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewService(cfg config.MetricsService, metricsCfg config.Metrics) *Service {
|
func NewService(cfg config.MetricsService, metricsCfg config.Metrics) *Service {
|
||||||
|
@ -25,14 +24,7 @@ func NewService(cfg config.MetricsService, metricsCfg config.Metrics) *Service {
|
||||||
}
|
}
|
||||||
|
|
||||||
s := &Service{
|
s := &Service{
|
||||||
HTTPInfo: service.HTTPInfo{
|
SimpleHTTP: service.NewSimpleHTTP("Metrics", endpoints),
|
||||||
Info: service.Info{
|
|
||||||
Name: "Metrics",
|
|
||||||
Endpoints: endpoints,
|
|
||||||
},
|
|
||||||
|
|
||||||
Mux: chi.NewMux(),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
s.Mux.Handle(
|
s.Mux.Handle(
|
||||||
|
@ -42,7 +34,3 @@ func NewService(cfg config.MetricsService, metricsCfg config.Metrics) *Service {
|
||||||
|
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) Merge(other service.Service) (service.Merger, error) {
|
|
||||||
return service.MergeHTTP(s, other)
|
|
||||||
}
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type dohService struct {
|
type dohService struct {
|
||||||
service.HTTPInfo
|
service.SimpleHTTP
|
||||||
|
|
||||||
handler dnsHandler
|
handler dnsHandler
|
||||||
}
|
}
|
||||||
|
@ -25,14 +25,7 @@ func newDoHService(cfg config.DoHService, handler dnsHandler) *dohService {
|
||||||
)
|
)
|
||||||
|
|
||||||
s := &dohService{
|
s := &dohService{
|
||||||
HTTPInfo: service.HTTPInfo{
|
SimpleHTTP: service.NewSimpleHTTP("DoH", endpoints),
|
||||||
Info: service.Info{
|
|
||||||
Name: "DoH",
|
|
||||||
Endpoints: endpoints,
|
|
||||||
},
|
|
||||||
|
|
||||||
Mux: chi.NewMux(),
|
|
||||||
},
|
|
||||||
|
|
||||||
handler: handler,
|
handler: handler,
|
||||||
}
|
}
|
||||||
|
@ -50,10 +43,6 @@ func newDoHService(cfg config.DoHService, handler dnsHandler) *dohService {
|
||||||
return s
|
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) {
|
func (s *dohService) handleGET(rw http.ResponseWriter, req *http.Request) {
|
||||||
dnsParam, ok := req.URL.Query()["dns"]
|
dnsParam, ok := req.URL.Query()["dns"]
|
||||||
if !ok || len(dnsParam[0]) < 1 {
|
if !ok || len(dnsParam[0]) < 1 {
|
||||||
|
|
|
@ -19,7 +19,7 @@ import (
|
||||||
// that expose everything. The goal is to split it up
|
// that expose everything. The goal is to split it up
|
||||||
// and remove it.
|
// and remove it.
|
||||||
type httpMiscService struct {
|
type httpMiscService struct {
|
||||||
service.HTTPInfo
|
service.SimpleHTTP
|
||||||
}
|
}
|
||||||
|
|
||||||
func newHTTPMiscService(cfg *config.Config) *httpMiscService {
|
func newHTTPMiscService(cfg *config.Config) *httpMiscService {
|
||||||
|
@ -28,20 +28,13 @@ func newHTTPMiscService(cfg *config.Config) *httpMiscService {
|
||||||
service.EndpointsFromAddrs(service.HTTPSProtocol, cfg.Ports.HTTPS),
|
service.EndpointsFromAddrs(service.HTTPSProtocol, cfg.Ports.HTTPS),
|
||||||
)
|
)
|
||||||
|
|
||||||
return &httpMiscService{
|
s := &httpMiscService{
|
||||||
HTTPInfo: service.HTTPInfo{
|
SimpleHTTP: service.NewSimpleHTTP("API", endpoints),
|
||||||
Info: service.Info{
|
|
||||||
Name: "HTTP",
|
|
||||||
Endpoints: endpoints,
|
|
||||||
},
|
|
||||||
|
|
||||||
Mux: createHTTPRouter(cfg),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func (s *httpMiscService) Merge(other service.Service) (service.Merger, error) {
|
configureHTTPRouter(s.Router(), cfg)
|
||||||
return service.MergeHTTP(s, other)
|
|
||||||
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// httpServer implements subServer for HTTP.
|
// httpServer implements subServer for HTTP.
|
||||||
|
|
|
@ -60,9 +60,7 @@ func (s *Server) Query(
|
||||||
return s.resolve(ctx, req)
|
return s.resolve(ctx, req)
|
||||||
}
|
}
|
||||||
|
|
||||||
func createHTTPRouter(cfg *config.Config) *chi.Mux {
|
func configureHTTPRouter(router chi.Router, cfg *config.Config) {
|
||||||
router := chi.NewRouter()
|
|
||||||
|
|
||||||
configureDebugHandler(router)
|
configureDebugHandler(router)
|
||||||
|
|
||||||
configureDocsHandler(router)
|
configureDocsHandler(router)
|
||||||
|
@ -70,11 +68,9 @@ func createHTTPRouter(cfg *config.Config) *chi.Mux {
|
||||||
configureStaticAssetsHandler(router)
|
configureStaticAssetsHandler(router)
|
||||||
|
|
||||||
configureRootHandler(cfg, 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) {
|
router.Get("/docs/openapi.yaml", func(writer http.ResponseWriter, request *http.Request) {
|
||||||
writer.Header().Set(contentTypeHeader, yamlContentType)
|
writer.Header().Set(contentTypeHeader, yamlContentType)
|
||||||
_, err := writer.Write([]byte(docs.OpenAPI))
|
_, 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()
|
assets, err := web.Assets()
|
||||||
util.FatalOnError("unable to load static asset files", err)
|
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))
|
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) {
|
router.Get("/", func(writer http.ResponseWriter, request *http.Request) {
|
||||||
writer.Header().Set(contentTypeHeader, htmlContentType)
|
writer.Header().Set(contentTypeHeader, htmlContentType)
|
||||||
t := template.New("index")
|
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())
|
router.Mount("/debug", middleware.Profiler())
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,8 +30,29 @@ type HTTPInfo struct {
|
||||||
Mux *chi.Mux
|
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 }
|
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.
|
// MergeHTTP merges two compatible HTTPServices.
|
||||||
//
|
//
|
||||||
// The second parameter is of type `Service` to make it easy to call
|
// The second parameter is of type `Service` to make it easy to call
|
||||||
|
|
|
@ -49,6 +49,13 @@ type Info struct {
|
||||||
Endpoints []Endpoint
|
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) ServiceName() string { return i.Name }
|
||||||
func (i *Info) ExposeOn() []Endpoint { return i.Endpoints }
|
func (i *Info) ExposeOn() []Endpoint { return i.Endpoints }
|
||||||
func (i *Info) String() string { return svcString(i) }
|
func (i *Info) String() string { return svcString(i) }
|
||||||
|
|
Loading…
Reference in New Issue