Skip to content

Commit

Permalink
refactor: switch HTTP API to Service pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
ThinkChaos committed Aug 30, 2024
1 parent a4e3c49 commit 3a1f5ca
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 9 deletions.
2 changes: 1 addition & 1 deletion api/api_interface_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion api/api_interface_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
Expand Down
39 changes: 39 additions & 0 deletions api/service.go
Original file line number Diff line number Diff line change
@@ -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)
}
43 changes: 43 additions & 0 deletions api/service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package api

import (
"github.com/0xERR0R/blocky/config"
"github.com/0xERR0R/blocky/service"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("API Service", func() {
var (
cfg config.APIService
sut *Service
err error
)

BeforeEach(func() {
cfg, err = config.WithDefaults[config.APIService]()
Expect(err).Should(Succeed())

cfg.Addrs.HTTP = config.ListenConfig{":80"}
cfg.Addrs.HTTPS = config.ListenConfig{":443"}
})

JustBeforeEach(func() {
sut = NewService(cfg, nil)
})

Describe("NewService", func() {
When("enabled", func() {
It("uses the configured addresses", func() {
Expect(sut.ExposeOn()).Should(ContainElements(
Equal(service.Endpoint{Protocol: "http", AddrConf: ":80"}),
Equal(service.Endpoint{Protocol: "https", AddrConf: ":443"}),
))
})

It("sets up routes", func() {
Expect(sut.Router().Routes()).ShouldNot(BeEmpty())
})
})
})
})
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down Expand Up @@ -625,6 +626,7 @@ func (cfg *Config) CopyPortsToServices() {
}

cfg.Services = Services{
API: APIService{Addrs: httpAddrs},
DoH: DoHService{Addrs: httpAddrs},
Metrics: MetricsService{Addrs: httpAddrs},
}
Expand Down
1 change: 1 addition & 0 deletions config/http_services.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

type (
APIService HTTPService
DoHService HTTPService
MetricsService HTTPService
)
Expand Down
5 changes: 2 additions & 3 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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),
Expand All @@ -36,7 +35,7 @@ func newHTTPMiscService(cfg *config.Config, openAPIImpl api.StrictServerInterfac
Endpoints: endpoints,
},

Mux: createHTTPRouter(cfg, openAPIImpl),
Mux: createHTTPRouter(cfg),
},
}
}
Expand Down
4 changes: 3 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
}

Expand Down
4 changes: 1 addition & 3 deletions server/server_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3a1f5ca

Please sign in to comment.