Skip to content

Commit

Permalink
refactor(server): move middleware setup to httpServer
Browse files Browse the repository at this point in the history
  • Loading branch information
ThinkChaos committed Apr 3, 2024
1 parent 35b1c16 commit 36d4437
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 37 deletions.
50 changes: 49 additions & 1 deletion server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"net"
"net/http"
"time"

"github.com/go-chi/chi/v5"
"github.com/go-chi/cors"
)

type httpServer struct {
Expand All @@ -26,7 +29,7 @@ func newHTTPServer(name string, handler http.Handler) *httpServer {
ReadHeaderTimeout: readHeaderTimeout,
WriteTimeout: writeTimeout,

Handler: handler,
Handler: withCommonMiddleware(handler),
},

name: name,
Expand All @@ -46,3 +49,48 @@ func (s *httpServer) Serve(ctx context.Context, l net.Listener) error {

return s.inner.Serve(l)
}

func withCommonMiddleware(inner http.Handler) *chi.Mux {
// Middleware must be defined before routes, so
// create a new router and mount the inner handler
mux := chi.NewMux()

mux.Use(
secureHeadersMiddleware,
newCORSMiddleware(),
)

mux.Mount("/", inner)

return mux
}

type httpMiddleware = func(http.Handler) http.Handler

func secureHeadersMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.TLS != nil {
w.Header().Set("strict-transport-security", "max-age=63072000")
w.Header().Set("x-frame-options", "DENY")
w.Header().Set("x-content-type-options", "nosniff")
w.Header().Set("x-xss-protection", "1; mode=block")
}

next.ServeHTTP(w, r)
})
}

func newCORSMiddleware() httpMiddleware {
const corsMaxAge = 5 * time.Minute

options := cors.Options{
AllowCredentials: true,
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
AllowedMethods: []string{"GET", "POST"},
AllowedOrigins: []string{"*"},
ExposedHeaders: []string{"Link"},
MaxAge: int(corsMaxAge.Seconds()),
}

return cors.New(options).Handler
}
36 changes: 0 additions & 36 deletions server/server_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io"
"net"
"net/http"
"time"

"github.com/0xERR0R/blocky/metrics"
"github.com/0xERR0R/blocky/resolver"
Expand All @@ -23,7 +22,6 @@ import (

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
"github.com/miekg/dns"
)

Expand All @@ -33,22 +31,8 @@ const (
dnsContentType = "application/dns-message"
htmlContentType = "text/html; charset=UTF-8"
yamlContentType = "text/yaml"
corsMaxAge = 5 * time.Minute
)

func secureHeader(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.TLS != nil {
w.Header().Set("strict-transport-security", "max-age=63072000")
w.Header().Set("x-frame-options", "DENY")
w.Header().Set("x-content-type-options", "nosniff")
w.Header().Set("x-xss-protection", "1; mode=block")
}

next.ServeHTTP(w, r)
})
}

func (s *Server) createOpenAPIInterfaceImpl() (impl api.StrictServerInterface, err error) {
bControl, err := resolver.GetFromChainWithType[api.BlockingControl](s.queryResolver)
if err != nil {
Expand Down Expand Up @@ -175,10 +159,6 @@ func (s *Server) Query(
func createHTTPRouter(cfg *config.Config, openAPIImpl api.StrictServerInterface) *chi.Mux {
router := chi.NewRouter()

configureSecureHeaderHandler(router)

configureCorsHandler(router)

api.RegisterOpenAPIEndpoints(router, openAPIImpl)

configureDebugHandler(router)
Expand Down Expand Up @@ -265,22 +245,6 @@ func logAndResponseWithError(err error, message string, writer http.ResponseWrit
}
}

func configureSecureHeaderHandler(router *chi.Mux) {
router.Use(secureHeader)
}

func configureDebugHandler(router *chi.Mux) {
router.Mount("/debug", middleware.Profiler())
}

func configureCorsHandler(router *chi.Mux) {
crs := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
ExposedHeaders: []string{"Link"},
AllowCredentials: true,
MaxAge: int(corsMaxAge.Seconds()),
})
router.Use(crs.Handler)
}

0 comments on commit 36d4437

Please sign in to comment.