upd(all): restructure middleware
This commit is contained in:
parent
ef1507920e
commit
e0de29c201
@ -3,32 +3,24 @@ package service
|
||||
import (
|
||||
"context"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/chi/middleware"
|
||||
"github.com/go-chi/cors"
|
||||
|
||||
"github.com/crusttech/crust/crm/rest"
|
||||
"github.com/crusttech/crust/internal/config"
|
||||
"github.com/crusttech/crust/internal/metrics"
|
||||
"github.com/crusttech/crust/internal/middleware"
|
||||
"github.com/crusttech/crust/internal/routes"
|
||||
"github.com/crusttech/crust/internal/version"
|
||||
)
|
||||
|
||||
func Routes(ctx context.Context) *chi.Mux {
|
||||
r := chi.NewRouter()
|
||||
middleware.Mount(ctx, r, flags.http)
|
||||
MountRoutes(ctx, r)
|
||||
routes.Print(r)
|
||||
MountSystemRoutes(r, flags.http)
|
||||
middleware.MountSystemRoutes(ctx, r, flags.http)
|
||||
return r
|
||||
}
|
||||
|
||||
func MountRoutes(ctx context.Context, r chi.Router) {
|
||||
r.Use(handleCORS)
|
||||
r.Use(middleware.RealIP)
|
||||
r.Use(middleware.RequestID)
|
||||
|
||||
// Only protect application routes with JWT
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(jwtVerifier, jwtAuthenticator)
|
||||
@ -36,32 +28,8 @@ func MountRoutes(ctx context.Context, r chi.Router) {
|
||||
})
|
||||
}
|
||||
|
||||
func MountSystemRoutes(r chi.Router, opts *config.HTTP) {
|
||||
metrics.MountRoutes(r, opts)
|
||||
r.Mount("/debug", middleware.Profiler())
|
||||
r.Get("/version", version.HttpHandler)
|
||||
}
|
||||
|
||||
func mountRoutes(r chi.Router, opts *config.HTTP, mounts ...func(r chi.Router)) {
|
||||
if opts.Logging {
|
||||
r.Use(middleware.Logger)
|
||||
}
|
||||
if opts.Metrics {
|
||||
r.Use(metrics.Middleware("crm"))
|
||||
}
|
||||
|
||||
for _, mount := range mounts {
|
||||
mount(r)
|
||||
}
|
||||
}
|
||||
|
||||
// Sets up default CORS rules to use as a middleware
|
||||
func handleCORS(next http.Handler) http.Handler {
|
||||
return cors.New(cors.Options{
|
||||
AllowedOrigins: []string{"*"},
|
||||
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"},
|
||||
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
|
||||
AllowCredentials: true,
|
||||
MaxAge: 300, // Maximum value not ignored by any of major browsers
|
||||
}).Handler(next)
|
||||
}
|
||||
|
||||
18
internal/middleware/cors.go
Normal file
18
internal/middleware/cors.go
Normal file
@ -0,0 +1,18 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/cors"
|
||||
)
|
||||
|
||||
// Sets up default CORS rules to use as a middleware
|
||||
func handleCORS(next http.Handler) http.Handler {
|
||||
return cors.New(cors.Options{
|
||||
AllowedOrigins: []string{"*"},
|
||||
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"},
|
||||
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
|
||||
AllowCredentials: true,
|
||||
MaxAge: 300, // Maximum value not ignored by any of major browsers
|
||||
}).Handler(next)
|
||||
}
|
||||
30
internal/middleware/mount.go
Normal file
30
internal/middleware/mount.go
Normal file
@ -0,0 +1,30 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/chi/middleware"
|
||||
|
||||
"github.com/crusttech/crust/internal/config"
|
||||
"github.com/crusttech/crust/internal/metrics"
|
||||
"github.com/crusttech/crust/internal/version"
|
||||
)
|
||||
|
||||
func Mount(ctx context.Context, r chi.Router, opts *config.HTTP) {
|
||||
r.Use(handleCORS)
|
||||
r.Use(middleware.RealIP)
|
||||
r.Use(middleware.RequestID)
|
||||
if opts.Logging {
|
||||
r.Use(middleware.Logger)
|
||||
}
|
||||
if opts.Metrics {
|
||||
r.Use(metrics.Middleware("crust"))
|
||||
}
|
||||
}
|
||||
|
||||
func MountSystemRoutes(ctx context.Context, r chi.Router, opts *config.HTTP) {
|
||||
metrics.MountRoutes(r, opts)
|
||||
r.Mount("/debug", middleware.Profiler())
|
||||
r.Get("/version", version.HttpHandler)
|
||||
}
|
||||
@ -3,33 +3,25 @@ package service
|
||||
import (
|
||||
"context"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/chi/middleware"
|
||||
"github.com/go-chi/cors"
|
||||
|
||||
"github.com/crusttech/crust/internal/config"
|
||||
"github.com/crusttech/crust/internal/metrics"
|
||||
"github.com/crusttech/crust/internal/middleware"
|
||||
"github.com/crusttech/crust/internal/routes"
|
||||
"github.com/crusttech/crust/internal/version"
|
||||
"github.com/crusttech/crust/messaging/rest"
|
||||
"github.com/crusttech/crust/messaging/websocket"
|
||||
)
|
||||
|
||||
func Routes(ctx context.Context) *chi.Mux {
|
||||
r := chi.NewRouter()
|
||||
middleware.Mount(ctx, r, flags.http)
|
||||
MountRoutes(ctx, r)
|
||||
routes.Print(r)
|
||||
MountSystemRoutes(r, flags.http)
|
||||
middleware.MountSystemRoutes(ctx, r, flags.http)
|
||||
return r
|
||||
}
|
||||
|
||||
func MountRoutes(ctx context.Context, r chi.Router) {
|
||||
r.Use(handleCORS)
|
||||
r.Use(middleware.RealIP)
|
||||
r.Use(middleware.RequestID)
|
||||
|
||||
// Only protect application routes with JWT
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(jwtVerifier, jwtAuthenticator)
|
||||
@ -37,32 +29,8 @@ func MountRoutes(ctx context.Context, r chi.Router) {
|
||||
})
|
||||
}
|
||||
|
||||
func MountSystemRoutes(r chi.Router, opts *config.HTTP) {
|
||||
metrics.MountRoutes(r, opts)
|
||||
r.Mount("/debug", middleware.Profiler())
|
||||
r.Get("/version", version.HttpHandler)
|
||||
}
|
||||
|
||||
func mountRoutes(r chi.Router, opts *config.HTTP, mounts ...func(r chi.Router)) {
|
||||
if opts.Logging {
|
||||
r.Use(middleware.Logger)
|
||||
}
|
||||
if opts.Metrics {
|
||||
r.Use(metrics.Middleware("messaging"))
|
||||
}
|
||||
|
||||
for _, mount := range mounts {
|
||||
mount(r)
|
||||
}
|
||||
}
|
||||
|
||||
// Sets up default CORS rules to use as a middleware
|
||||
func handleCORS(next http.Handler) http.Handler {
|
||||
return cors.New(cors.Options{
|
||||
AllowedOrigins: []string{"*"},
|
||||
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"},
|
||||
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
|
||||
AllowCredentials: true,
|
||||
MaxAge: 300, // Maximum value not ignored by any of major browsers
|
||||
}).Handler(next)
|
||||
}
|
||||
|
||||
@ -3,32 +3,24 @@ package service
|
||||
import (
|
||||
"context"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/chi/middleware"
|
||||
"github.com/go-chi/cors"
|
||||
|
||||
"github.com/crusttech/crust/internal/config"
|
||||
"github.com/crusttech/crust/internal/metrics"
|
||||
"github.com/crusttech/crust/internal/middleware"
|
||||
"github.com/crusttech/crust/internal/routes"
|
||||
"github.com/crusttech/crust/internal/version"
|
||||
"github.com/crusttech/crust/system/rest"
|
||||
)
|
||||
|
||||
func Routes(ctx context.Context) *chi.Mux {
|
||||
r := chi.NewRouter()
|
||||
middleware.Mount(ctx, r, flags.http)
|
||||
MountRoutes(ctx, r)
|
||||
routes.Print(r)
|
||||
MountSystemRoutes(r, flags.http)
|
||||
middleware.MountSystemRoutes(ctx, r, flags.http)
|
||||
return r
|
||||
}
|
||||
|
||||
func MountRoutes(ctx context.Context, r chi.Router) {
|
||||
r.Use(handleCORS)
|
||||
r.Use(middleware.RealIP)
|
||||
r.Use(middleware.RequestID)
|
||||
|
||||
// Only protect application routes with JWT
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(jwtVerifier, jwtAuthenticator)
|
||||
@ -36,32 +28,8 @@ func MountRoutes(ctx context.Context, r chi.Router) {
|
||||
})
|
||||
}
|
||||
|
||||
func MountSystemRoutes(r chi.Router, opts *config.HTTP) {
|
||||
metrics.MountRoutes(r, opts)
|
||||
r.Mount("/debug", middleware.Profiler())
|
||||
r.Get("/version", version.HttpHandler)
|
||||
}
|
||||
|
||||
func mountRoutes(r chi.Router, opts *config.HTTP, mounts ...func(r chi.Router)) {
|
||||
if opts.Logging {
|
||||
r.Use(middleware.Logger)
|
||||
}
|
||||
if opts.Metrics {
|
||||
r.Use(metrics.Middleware("auth"))
|
||||
}
|
||||
|
||||
for _, mount := range mounts {
|
||||
mount(r)
|
||||
}
|
||||
}
|
||||
|
||||
// Sets up default CORS rules to use as a middleware
|
||||
func handleCORS(next http.Handler) http.Handler {
|
||||
return cors.New(cors.Options{
|
||||
AllowedOrigins: []string{"*"},
|
||||
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"},
|
||||
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
|
||||
AllowCredentials: true,
|
||||
MaxAge: 300, // Maximum value not ignored by any of major browsers
|
||||
}).Handler(next)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user