type Middleware interface {
Apply(f HandlerFunc) HandlerFunc
}
type MiddlewareFunc func(HandlerFunc) HandlerFuncMiddleware as a struct:
type MyMiddleware struct {
SomeData string
}
func(m *MyMiddleware) Apply(h HandlerFunc) HandlerFunc {
return func(w http.ResponseWriter, r *http.Request, vars map[string]string) error {
// Do something before handler
h(w, r, vars)
// Do something after handler
}
}Or as a function
func MyFunctionMiddleware(h HandlerFunc) HandlerFunc {
return func(w http.ResponseWriter, r *http.Request, vars map[string]string) error {
// Do something before handler
h(w, r, vars)
// Do something after handler
}
}Add them to your router with:
router.Use(MyMiddleware)
router.Use(MiddlewareFunc(MyMiddleware))Middlewares have to be setup before setting the handlers.
The handler returns an error which can be read/managed by the middlwares. So you can add your Airbrake or Rollbar notification as a simple middleware.
mux.Vars(req) → vars map[string]string argument of Handler
logger := logrus.New()
middleware := NewLoggingMiddleware(logger)
router.Use(middleware)That being said there when NewRouter it creates a LoggingMiddleware by
default.
router.Use(MiddlewareFunc(NewCorsMiddleware))Thie middleware writes in the logs with the Error log level.
To send logs to rollbar, ensure your logger is properly configured
with the rollbar hook.
import (
"gopkg.in/Scalingo/logrus-rollbar.v1"
)
logger := logger.Default(logrus_rollbar.New(0))
router := handlers.NewRouter(logger)
router.Use(MiddlewareFunc(ErrorHandler))This package provides a ready-to-use router exposing Go's net/http/pprof endpoints behind HTTP Basic Auth.
Profiling endpoints are enabled only if all the following environment variables are set:
PPROF_ENABLED:trueto enablePPROF_USERNAME: username for Basic AuthPPROF_PASSWORD: password for Basic Auth
If one of these variables is missing (or if PPROF_ENABLED is false), the profiling router is returned but has no routes registered (requests will return 404).
Create the router with NewProfilingRouter(ctx) and mount it on your main router.
import (
"context"
"net/http"
"github.com/Scalingo/go-handlers"
"github.com/Scalingo/go-utils/logger"
)
func main() {
log := logger.Default()
ctx := logger.ToCtx(context.Background(), log)
router := handlers.NewRouter(log)
pprofRouter, err := handlers.NewProfilingRouter(ctx)
if err != nil {
log.WithError(err).Error("Fail to start profiling routes")
} else {
log.Info("Adding profiling routes to the web server")
// Route all /debug/pprof/* requests to the profiling router.
router.PathPrefix(handlers.PprofRoutePrefix).Handler(pprofRouter)
}
_ = http.ListenAndServe(":8080", router)
}The profiling endpoints are exposed under handlers.PprofRoutePrefix (currently /debug/pprof).
When enabled, the following endpoints are available under /debug/pprof:
/(index)/profile/symbol/cmdline/trace/allocs/heap/mutex/goroutine/threadcreate/block
Example (CPU profile):
go tool pprof -http=:12345 http://PPROF_USERNAME:PPROF_PASSWORD@localhost:8080/debug/pprof/profile?seconds=30Bump new version number in CHANGELOG.md and README.md.
Commit, tag and create a new release:
version="1.11.1"
git switch --create release/${version}
git add CHANGELOG.md README.md
git commit --message="Bump v${version}"
git push --set-upstream origin release/${version}
gh pr create --reviewer=Scalingo/team-ist --title "$(git log -1 --pretty=%B)"Once the pull request merged, you can tag the new release.
git tag v${version}
git push origin master v${version}
gh release create v${version}The title of the release should be the version number and the text of the release is the same as the changelog.