Config. environment (throughenv var 'ENVIRONMENT')

This commit is contained in:
Denis Arh
2020-11-04 15:13:27 +01:00
parent 7666fe2d62
commit ed8fc547f7
12 changed files with 106 additions and 76 deletions
+5 -9
View File
@@ -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
View File
@@ -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")
+6 -8
View File
@@ -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
View File
@@ -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)