upd(all): move around system routes, routes print
This commit is contained in:
+13
-38
@@ -2,13 +2,9 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"runtime"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/99designs/basicauth-go"
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/chi/middleware"
|
||||
"github.com/go-chi/cors"
|
||||
@@ -16,11 +12,19 @@ import (
|
||||
"github.com/crusttech/crust/crm/rest"
|
||||
"github.com/crusttech/crust/internal/config"
|
||||
"github.com/crusttech/crust/internal/metrics"
|
||||
"github.com/crusttech/crust/internal/routes"
|
||||
"github.com/crusttech/crust/internal/version"
|
||||
)
|
||||
|
||||
func Routes(ctx context.Context) *chi.Mux {
|
||||
r := chi.NewRouter()
|
||||
MountRoutes(ctx, r)
|
||||
routes.Print(r)
|
||||
MountSystemRoutes(r, flags.http)
|
||||
return r
|
||||
}
|
||||
|
||||
func MountRoutes(ctx context.Context, r chi.Router) {
|
||||
r.Use(handleCORS)
|
||||
r.Use(middleware.RealIP)
|
||||
r.Use(middleware.RequestID)
|
||||
@@ -30,10 +34,12 @@ func Routes(ctx context.Context) *chi.Mux {
|
||||
r.Use(jwtVerifier, jwtAuthenticator)
|
||||
mountRoutes(r, flags.http, rest.MountRoutes())
|
||||
})
|
||||
}
|
||||
|
||||
printRoutes(r, flags.http)
|
||||
mountSystemRoutes(r, flags.http)
|
||||
return r
|
||||
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)) {
|
||||
@@ -49,37 +55,6 @@ func mountRoutes(r chi.Router, opts *config.HTTP, mounts ...func(r chi.Router))
|
||||
}
|
||||
}
|
||||
|
||||
func mountSystemRoutes(r chi.Router, opts *config.HTTP) {
|
||||
if opts.Metrics {
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(basicauth.New("Metrics", map[string][]string{
|
||||
opts.MetricsUsername: {opts.MetricsPassword},
|
||||
}))
|
||||
r.Handle("/metrics", metrics.Handler())
|
||||
})
|
||||
}
|
||||
r.Mount("/debug", middleware.Profiler())
|
||||
r.Get("/version", version.HttpHandler)
|
||||
}
|
||||
|
||||
func printRoutes(r chi.Router, opts *config.HTTP) {
|
||||
var printRoutes func(chi.Routes, string, string)
|
||||
printRoutes = func(r chi.Routes, indent string, prefix string) {
|
||||
routes := r.Routes()
|
||||
for _, route := range routes {
|
||||
if route.SubRoutes != nil && len(route.SubRoutes.Routes()) > 0 {
|
||||
fmt.Printf(indent+"%s - with %d handlers, %d subroutes\n", route.Pattern, len(route.Handlers), len(route.SubRoutes.Routes()))
|
||||
printRoutes(route.SubRoutes, indent+"\t", prefix+route.Pattern[:len(route.Pattern)-2])
|
||||
} else {
|
||||
for key, fn := range route.Handlers {
|
||||
fmt.Printf("%s%s\t%s -> %s\n", indent, key, prefix+route.Pattern, runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
printRoutes(r, "", "")
|
||||
}
|
||||
|
||||
// Sets up default CORS rules to use as a middleware
|
||||
func handleCORS(next http.Handler) http.Handler {
|
||||
return cors.New(cors.Options{
|
||||
|
||||
@@ -2,8 +2,8 @@ package routes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
"reflect"
|
||||
"runtime"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
)
|
||||
|
||||
+13
-38
@@ -2,19 +2,16 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"runtime"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/99designs/basicauth-go"
|
||||
"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/routes"
|
||||
"github.com/crusttech/crust/internal/version"
|
||||
"github.com/crusttech/crust/messaging/rest"
|
||||
"github.com/crusttech/crust/messaging/websocket"
|
||||
@@ -22,6 +19,13 @@ import (
|
||||
|
||||
func Routes(ctx context.Context) *chi.Mux {
|
||||
r := chi.NewRouter()
|
||||
MountRoutes(ctx, r)
|
||||
routes.Print(r)
|
||||
MountSystemRoutes(r, flags.http)
|
||||
return r
|
||||
}
|
||||
|
||||
func MountRoutes(ctx context.Context, r chi.Router) {
|
||||
r.Use(handleCORS)
|
||||
r.Use(middleware.RealIP)
|
||||
r.Use(middleware.RequestID)
|
||||
@@ -31,10 +35,12 @@ func Routes(ctx context.Context) *chi.Mux {
|
||||
r.Use(jwtVerifier, jwtAuthenticator)
|
||||
mountRoutes(r, flags.http, rest.MountRoutes(), websocket.MountRoutes(ctx, flags.repository))
|
||||
})
|
||||
}
|
||||
|
||||
printRoutes(r, flags.http)
|
||||
mountSystemRoutes(r, flags.http)
|
||||
return r
|
||||
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)) {
|
||||
@@ -50,37 +56,6 @@ func mountRoutes(r chi.Router, opts *config.HTTP, mounts ...func(r chi.Router))
|
||||
}
|
||||
}
|
||||
|
||||
func mountSystemRoutes(r chi.Router, opts *config.HTTP) {
|
||||
if opts.Metrics {
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(basicauth.New("Metrics", map[string][]string{
|
||||
opts.MetricsUsername: {opts.MetricsPassword},
|
||||
}))
|
||||
r.Handle("/metrics", metrics.Handler())
|
||||
})
|
||||
}
|
||||
r.Mount("/debug", middleware.Profiler())
|
||||
r.Get("/version", version.HttpHandler)
|
||||
}
|
||||
|
||||
func printRoutes(r chi.Router, opts *config.HTTP) {
|
||||
var printRoutes func(chi.Routes, string, string)
|
||||
printRoutes = func(r chi.Routes, indent string, prefix string) {
|
||||
routes := r.Routes()
|
||||
for _, route := range routes {
|
||||
if route.SubRoutes != nil && len(route.SubRoutes.Routes()) > 0 {
|
||||
fmt.Printf(indent+"%s - with %d handlers, %d subroutes\n", route.Pattern, len(route.Handlers), len(route.SubRoutes.Routes()))
|
||||
printRoutes(route.SubRoutes, indent+"\t", prefix+route.Pattern[:len(route.Pattern)-2])
|
||||
} else {
|
||||
for key, fn := range route.Handlers {
|
||||
fmt.Printf("%s%s\t%s -> %s\n", indent, key, prefix+route.Pattern, runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
printRoutes(r, "", "")
|
||||
}
|
||||
|
||||
// Sets up default CORS rules to use as a middleware
|
||||
func handleCORS(next http.Handler) http.Handler {
|
||||
return cors.New(cors.Options{
|
||||
|
||||
+13
-38
@@ -2,25 +2,29 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"runtime"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/99designs/basicauth-go"
|
||||
"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/routes"
|
||||
"github.com/crusttech/crust/internal/version"
|
||||
"github.com/crusttech/crust/system/rest"
|
||||
)
|
||||
|
||||
func Routes(ctx context.Context) *chi.Mux {
|
||||
r := chi.NewRouter()
|
||||
MountRoutes(ctx, r)
|
||||
routes.Print(r)
|
||||
MountSystemRoutes(r, flags.http)
|
||||
return r
|
||||
}
|
||||
|
||||
func MountRoutes(ctx context.Context, r chi.Router) {
|
||||
r.Use(handleCORS)
|
||||
r.Use(middleware.RealIP)
|
||||
r.Use(middleware.RequestID)
|
||||
@@ -30,10 +34,12 @@ func Routes(ctx context.Context) *chi.Mux {
|
||||
r.Use(jwtVerifier, jwtAuthenticator)
|
||||
mountRoutes(r, flags.http, rest.MountRoutes(flags.oidc, flags.social, jwtEncoder))
|
||||
})
|
||||
}
|
||||
|
||||
printRoutes(r, flags.http)
|
||||
mountSystemRoutes(r, flags.http)
|
||||
return r
|
||||
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)) {
|
||||
@@ -49,37 +55,6 @@ func mountRoutes(r chi.Router, opts *config.HTTP, mounts ...func(r chi.Router))
|
||||
}
|
||||
}
|
||||
|
||||
func mountSystemRoutes(r chi.Router, opts *config.HTTP) {
|
||||
if opts.Metrics {
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(basicauth.New("Metrics", map[string][]string{
|
||||
opts.MetricsUsername: {opts.MetricsPassword},
|
||||
}))
|
||||
r.Handle("/metrics", metrics.Handler())
|
||||
})
|
||||
}
|
||||
r.Mount("/debug", middleware.Profiler())
|
||||
r.Get("/version", version.HttpHandler)
|
||||
}
|
||||
|
||||
func printRoutes(r chi.Router, opts *config.HTTP) {
|
||||
var printRoutes func(chi.Routes, string, string)
|
||||
printRoutes = func(r chi.Routes, indent string, prefix string) {
|
||||
routes := r.Routes()
|
||||
for _, route := range routes {
|
||||
if route.SubRoutes != nil && len(route.SubRoutes.Routes()) > 0 {
|
||||
fmt.Printf(indent+"%s - with %d handlers, %d subroutes\n", route.Pattern, len(route.Handlers), len(route.SubRoutes.Routes()))
|
||||
printRoutes(route.SubRoutes, indent+"\t", prefix+route.Pattern[:len(route.Pattern)-2])
|
||||
} else {
|
||||
for key, fn := range route.Handlers {
|
||||
fmt.Printf("%s%s\t%s -> %s\n", indent, key, prefix+route.Pattern, runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
printRoutes(r, "", "")
|
||||
}
|
||||
|
||||
// Sets up default CORS rules to use as a middleware
|
||||
func handleCORS(next http.Handler) http.Handler {
|
||||
return cors.New(cors.Options{
|
||||
|
||||
Reference in New Issue
Block a user