Skip to content

Define a custom Handler and router based on gorilla + negroni with easy integration of middlewares

Notifications You must be signed in to change notification settings

Scalingo/go-handlers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Custom Router and Handler v1.8.2

Codeship Status for Scalingo/go-handlers

Advantages

Injection of middlewares

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.

Error propagation in the middlewares

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.

Testable handlers based on Gorilla Muxer

mux.Vars(req)vars map[string]string argument of Handler

Included middlewares

Logging Middleware

logger := logrus.New()
middleware := NewLoggingMiddleware(logger)
router.Use(middleware)

That being said there when NewRouter it creates a LoggingMiddleware by default.

Cors Middleware

router.Use(MiddlewareFunc(NewCorsMiddleware))

Error Middleware

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))

Release a New Version

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.

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.

About

Define a custom Handler and router based on gorilla + negroni with easy integration of middlewares

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages