Skip to content

Commit

Permalink
refactor(server): simplify HTTP mux setup
Browse files Browse the repository at this point in the history
  • Loading branch information
ThinkChaos committed Apr 1, 2024
1 parent 28f979f commit 109726b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 31 deletions.
26 changes: 10 additions & 16 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,11 @@ func NewServer(ctx context.Context, cfg *config.Config) (server *Server, err err
return nil, fmt.Errorf("server creation failed: %w", err)
}

httpRouter := createHTTPRouter(cfg)
httpsRouter := createHTTPSRouter(cfg)

httpListeners, httpsListeners, err := createHTTPListeners(cfg)
if err != nil {
return nil, err
}

if len(httpListeners) != 0 || len(httpsListeners) != 0 {
metrics.Start(httpRouter, cfg.Prometheus)
metrics.Start(httpsRouter, cfg.Prometheus)
}

metrics.RegisterEventListeners()

bootstrap, err := resolver.NewBootstrap(ctx, cfg)
Expand All @@ -156,24 +148,26 @@ func NewServer(ctx context.Context, cfg *config.Config) (server *Server, err err
cfg: cfg,
httpListeners: httpListeners,
httpsListeners: httpsListeners,
httpMux: httpRouter,
httpsMux: httpsRouter,
cert: cert,
}

server.printConfiguration()

server.registerDNSHandlers(ctx)
err = server.registerAPIEndpoints(httpRouter)

openAPIImpl, err := server.createOpenAPIInterfaceImpl()
if err != nil {
return nil, err
}

err = server.registerAPIEndpoints(httpsRouter)
server.registerDNSHandlers(ctx)

if err != nil {
return nil, err
if len(cfg.Ports.HTTP) != 0 {
server.httpMux = createHTTPRouter(cfg, openAPIImpl)
server.registerDoHEndpoints(server.httpMux)
}

if len(cfg.Ports.HTTPS) != 0 {
server.httpsMux = createHTTPSRouter(cfg, openAPIImpl)
server.registerDoHEndpoints(server.httpsMux)
}

return server, err
Expand Down
26 changes: 11 additions & 15 deletions server/server_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"time"

"github.com/0xERR0R/blocky/metrics"
"github.com/0xERR0R/blocky/resolver"

"github.com/0xERR0R/blocky/api"
Expand Down Expand Up @@ -64,24 +65,15 @@ func (s *Server) createOpenAPIInterfaceImpl() (impl api.StrictServerInterface, e
return api.NewOpenAPIInterfaceImpl(bControl, s, refresher, cacheControl), nil
}

func (s *Server) registerAPIEndpoints(router *chi.Mux) error {
func (s *Server) registerDoHEndpoints(router *chi.Mux) {
const pathDohQuery = "/dns-query"

openAPIImpl, err := s.createOpenAPIInterfaceImpl()
if err != nil {
return err
}

api.RegisterOpenAPIEndpoints(router, openAPIImpl)

router.Get(pathDohQuery, s.dohGetRequestHandler)
router.Get(pathDohQuery+"/", s.dohGetRequestHandler)
router.Get(pathDohQuery+"/{clientID}", s.dohGetRequestHandler)
router.Post(pathDohQuery, s.dohPostRequestHandler)
router.Post(pathDohQuery+"/", s.dohPostRequestHandler)
router.Post(pathDohQuery+"/{clientID}", s.dohPostRequestHandler)

return nil
}

func (s *Server) dohGetRequestHandler(rw http.ResponseWriter, req *http.Request) {
Expand Down Expand Up @@ -177,34 +169,38 @@ func (s *Server) Query(
return s.resolve(ctx, req)
}

func createHTTPSRouter(cfg *config.Config) *chi.Mux {
func createHTTPSRouter(cfg *config.Config, openAPIImpl api.StrictServerInterface) *chi.Mux {
router := chi.NewRouter()

configureSecureHeaderHandler(router)

registerHandlers(cfg, router)
registerHandlers(cfg, router, openAPIImpl)

return router
}

func createHTTPRouter(cfg *config.Config) *chi.Mux {
func createHTTPRouter(cfg *config.Config, openAPIImpl api.StrictServerInterface) *chi.Mux {
router := chi.NewRouter()

registerHandlers(cfg, router)
registerHandlers(cfg, router, openAPIImpl)

return router
}

func registerHandlers(cfg *config.Config, router *chi.Mux) {
func registerHandlers(cfg *config.Config, router *chi.Mux, openAPIImpl api.StrictServerInterface) {
configureCorsHandler(router)

api.RegisterOpenAPIEndpoints(router, openAPIImpl)

configureDebugHandler(router)

configureDocsHandler(router)

configureStaticAssetsHandler(router)

configureRootHandler(cfg, router)

metrics.Start(router, cfg.Prometheus)
}

func configureDocsHandler(router *chi.Mux) {
Expand Down

0 comments on commit 109726b

Please sign in to comment.