From ad3c562ed97aa5351a2cf5d28540b1e9eeb23fab Mon Sep 17 00:00:00 2001 From: ThinkChaos Date: Tue, 2 Apr 2024 22:17:05 -0400 Subject: [PATCH] refactor: switch HTTP API to Service pattern --- api/api_interface_impl.go | 2 +- api/api_interface_impl_test.go | 2 +- api/service.go | 39 ++++++++++++++++++++++++++++++++++ config/config.go | 2 ++ config/doh_service.go | 1 + server/http.go | 5 ++--- server/server.go | 4 +++- server/server_endpoints.go | 4 +--- 8 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 api/service.go diff --git a/api/api_interface_impl.go b/api/api_interface_impl.go index ec1b3e958..8779787ac 100644 --- a/api/api_interface_impl.go +++ b/api/api_interface_impl.go @@ -53,7 +53,7 @@ type CacheControl interface { FlushCaches(ctx context.Context) } -func RegisterOpenAPIEndpoints(router chi.Router, impl StrictServerInterface) { +func registerOpenAPIEndpoints(router chi.Router, impl StrictServerInterface) { middleware := []StrictMiddlewareFunc{ctxWithHTTPRequestMiddleware} HandlerFromMuxWithBaseURL(NewStrictHandler(impl, middleware), router, "/api") diff --git a/api/api_interface_impl_test.go b/api/api_interface_impl_test.go index 454443950..6d13916ae 100644 --- a/api/api_interface_impl_test.go +++ b/api/api_interface_impl_test.go @@ -105,7 +105,7 @@ var _ = Describe("API implementation tests", func() { Describe("RegisterOpenAPIEndpoints", func() { It("adds routes", func() { rtr := chi.NewRouter() - RegisterOpenAPIEndpoints(rtr, sut) + registerOpenAPIEndpoints(rtr, sut) Expect(rtr.Routes()).ShouldNot(BeEmpty()) }) diff --git a/api/service.go b/api/service.go new file mode 100644 index 000000000..1b2b575f4 --- /dev/null +++ b/api/service.go @@ -0,0 +1,39 @@ +package api + +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 +} + +func NewService(cfg config.APIService, server StrictServerInterface) *Service { + endpoints := util.ConcatSlices( + service.EndpointsFromAddrs(service.HTTPProtocol, cfg.Addrs.HTTP), + service.EndpointsFromAddrs(service.HTTPSProtocol, cfg.Addrs.HTTPS), + ) + + s := &Service{ + HTTPInfo: service.HTTPInfo{ + Info: service.Info{ + Name: "API", + Endpoints: endpoints, + }, + + Mux: chi.NewMux(), + }, + } + + registerOpenAPIEndpoints(s.Mux, server) + + return s +} + +func (s *Service) Merge(other service.Service) (service.Merger, error) { + return service.MergeHTTP(s, other) +} diff --git a/config/config.go b/config/config.go index d240a64d7..4a04f7fea 100644 --- a/config/config.go +++ b/config/config.go @@ -272,6 +272,7 @@ type Config struct { // The `yaml` struct tags are just for manual testing, // and require replacing `yaml:"-"` in Config to work. type Services struct { + API APIService `yaml:"control-api"` DoH DoHService `yaml:"dns-over-https"` Metrics MetricsService `yaml:"metrics"` } @@ -625,6 +626,7 @@ func (cfg *Config) CopyPortsToServices() { } cfg.Services = Services{ + API: APIService{Addrs: httpAddrs}, DoH: DoHService{Addrs: httpAddrs}, Metrics: MetricsService{Addrs: httpAddrs}, } diff --git a/config/doh_service.go b/config/doh_service.go index 27c429448..8df6767c5 100644 --- a/config/doh_service.go +++ b/config/doh_service.go @@ -1,6 +1,7 @@ package config type ( + APIService httpService DoHService httpService MetricsService httpService ) diff --git a/server/http.go b/server/http.go index b13c3389a..0ee157800 100644 --- a/server/http.go +++ b/server/http.go @@ -6,7 +6,6 @@ import ( "net/http" "time" - "github.com/0xERR0R/blocky/api" "github.com/0xERR0R/blocky/config" "github.com/0xERR0R/blocky/service" "github.com/0xERR0R/blocky/util" @@ -23,7 +22,7 @@ type httpMiscService struct { service.HTTPInfo } -func newHTTPMiscService(cfg *config.Config, openAPIImpl api.StrictServerInterface) *httpMiscService { +func newHTTPMiscService(cfg *config.Config) *httpMiscService { endpoints := util.ConcatSlices( service.EndpointsFromAddrs(service.HTTPProtocol, cfg.Ports.HTTP), service.EndpointsFromAddrs(service.HTTPSProtocol, cfg.Ports.HTTPS), @@ -36,7 +35,7 @@ func newHTTPMiscService(cfg *config.Config, openAPIImpl api.StrictServerInterfac Endpoints: endpoints, }, - Mux: createHTTPRouter(cfg, openAPIImpl), + Mux: createHTTPRouter(cfg), }, } } diff --git a/server/server.go b/server/server.go index ff9d053f6..7ff9b5590 100644 --- a/server/server.go +++ b/server/server.go @@ -13,6 +13,7 @@ import ( "strings" "time" + "github.com/0xERR0R/blocky/api" "github.com/0xERR0R/blocky/config" "github.com/0xERR0R/blocky/log" "github.com/0xERR0R/blocky/metrics" @@ -179,8 +180,9 @@ func (s *Server) createServices() ([]service.Service, error) { } res := []service.Service{ - newHTTPMiscService(s.cfg, openAPIImpl), + newHTTPMiscService(s.cfg), newDoHService(s.cfg.Services.DoH, s.handleReq), + api.NewService(s.cfg.Services.API, openAPIImpl), metrics.NewService(s.cfg.Services.Metrics, s.cfg.Prometheus), } diff --git a/server/server_endpoints.go b/server/server_endpoints.go index e5077fb7e..39ade0112 100644 --- a/server/server_endpoints.go +++ b/server/server_endpoints.go @@ -60,11 +60,9 @@ func (s *Server) Query( return s.resolve(ctx, req) } -func createHTTPRouter(cfg *config.Config, openAPIImpl api.StrictServerInterface) *chi.Mux { +func createHTTPRouter(cfg *config.Config) *chi.Mux { router := chi.NewRouter() - api.RegisterOpenAPIEndpoints(router, openAPIImpl) - configureDebugHandler(router) configureDocsHandler(router)