Config. environment (throughenv var 'ENVIRONMENT')
This commit is contained in:
+5
-9
@@ -9,20 +9,16 @@ import (
|
||||
type ctxKeyDebug struct{}
|
||||
|
||||
// Packs remote address to context
|
||||
func DebugToContext(next http.Handler) http.Handler {
|
||||
if true {
|
||||
// debug disabled
|
||||
return next
|
||||
func DebugToContext(production bool) func(next http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||
next.ServeHTTP(w, req.WithContext(context.WithValue(req.Context(), ctxKeyDebug{}, !production)))
|
||||
})
|
||||
}
|
||||
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||
next.ServeHTTP(w, req.WithContext(context.WithValue(req.Context(), ctxKeyDebug{}, true)))
|
||||
})
|
||||
}
|
||||
|
||||
// DebugFromContext returns remote IP address from context
|
||||
func DebugFromContext(ctx context.Context) bool {
|
||||
return true
|
||||
debug, ok := ctx.Value(ctxKeyDebug{}).(bool)
|
||||
return ok && debug
|
||||
}
|
||||
|
||||
+1
-1
@@ -96,7 +96,7 @@ func encode(w http.ResponseWriter, r *http.Request, payload interface{}) {
|
||||
_ = err.Apply(errors.StackTrimAtFn("http.HandlerFunc.ServeHTTP"))
|
||||
}
|
||||
|
||||
errors.ServeHTTP(w, r, err, DebugFromContext(r.Context()))
|
||||
errors.ServeHTTP(w, r, err, !DebugFromContext(r.Context()))
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
@@ -2,24 +2,22 @@ package server
|
||||
|
||||
import (
|
||||
"github.com/cortezaproject/corteza-server/pkg/api"
|
||||
"github.com/cortezaproject/corteza-server/pkg/logger"
|
||||
"github.com/getsentry/sentry-go/http"
|
||||
"github.com/go-chi/chi/middleware"
|
||||
"go.uber.org/zap"
|
||||
"net/http"
|
||||
"os"
|
||||
"runtime/debug"
|
||||
|
||||
"github.com/go-chi/chi/middleware"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/getsentry/sentry-go/http"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/logger"
|
||||
)
|
||||
|
||||
func BaseMiddleware(log *zap.Logger) []func(http.Handler) http.Handler {
|
||||
func BaseMiddleware(isProduction bool, log *zap.Logger) []func(http.Handler) http.Handler {
|
||||
return []func(http.Handler) http.Handler{
|
||||
handleCORS,
|
||||
middleware.RealIP,
|
||||
api.RemoteAddrToContext,
|
||||
middleware.RequestID,
|
||||
api.DebugToContext(isProduction),
|
||||
contextLogger(log),
|
||||
}
|
||||
}
|
||||
|
||||
+13
-10
@@ -16,19 +16,22 @@ import (
|
||||
|
||||
type (
|
||||
server struct {
|
||||
log *zap.Logger
|
||||
httpOpt options.HTTPServerOpt
|
||||
waitForOpt options.WaitForOpt
|
||||
endpoints []func(r chi.Router)
|
||||
log *zap.Logger
|
||||
httpOpt options.HTTPServerOpt
|
||||
waitForOpt options.WaitForOpt
|
||||
environmentOpt options.EnvironmentOpt
|
||||
endpoints []func(r chi.Router)
|
||||
}
|
||||
)
|
||||
|
||||
func New(log *zap.Logger, httpOpt options.HTTPServerOpt, waitForOpt options.WaitForOpt) *server {
|
||||
func New(log *zap.Logger, envOpt options.EnvironmentOpt, httpOpt options.HTTPServerOpt, waitForOpt options.WaitForOpt) *server {
|
||||
return &server{
|
||||
endpoints: make([]func(r chi.Router), 0),
|
||||
log: log.Named("http"),
|
||||
httpOpt: httpOpt,
|
||||
waitForOpt: waitForOpt,
|
||||
endpoints: make([]func(r chi.Router), 0),
|
||||
log: log.Named("http"),
|
||||
|
||||
environmentOpt: envOpt,
|
||||
httpOpt: httpOpt,
|
||||
waitForOpt: waitForOpt,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,7 +51,7 @@ func (s server) Serve(ctx context.Context) {
|
||||
router := chi.NewRouter()
|
||||
|
||||
// Base middleware, CORS, RealIP, RequestID, context-logger
|
||||
router.Use(BaseMiddleware(s.log)...)
|
||||
router.Use(BaseMiddleware(s.environmentOpt.IsProduction(), s.log)...)
|
||||
|
||||
router.Group(func(r chi.Router) {
|
||||
s.bindMiscRoutes(r)
|
||||
|
||||
Reference in New Issue
Block a user