type Middleware interface {
Apply(f HandlerFunc) HandlerFunc
}
type MiddlewareFunc func(HandlerFunc) HandlerFunc
Middleware 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))
Bump new version number in CHANGELOG.md
and README.md
.
Commit, tag and create a new release:
version="1.8.2"
git switch --create release/${version}
git add CHANGELOG.md README.md
git commit -m "Bump v${version}"
git push --set-upstream origin release/${version}
gh pr create --reviewer=EtienneM --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.