https://github.com/xyproto/mooseware
:skull: Skeleton for writing a middleware handler
https://github.com/xyproto/mooseware
example go middleware negroni negroni-middleware-handler
Last synced: 9 months ago
JSON representation
:skull: Skeleton for writing a middleware handler
- Host: GitHub
- URL: https://github.com/xyproto/mooseware
- Owner: xyproto
- License: mit
- Created: 2014-08-27T07:48:37.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2020-01-31T14:04:32.000Z (almost 6 years ago)
- Last Synced: 2025-03-12T13:22:48.364Z (11 months ago)
- Topics: example, go, middleware, negroni, negroni-middleware-handler
- Language: Go
- Homepage:
- Size: 24.4 KB
- Stars: 47
- Watchers: 6
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Mooseware
=========
[](https://travis-ci.com/xyproto/mooseware)
Simple example/skeleton code for writing a middleware handler.
Middleware example
------------------
~~~ go
package moose
import (
"log"
"net/http"
)
type Middleware struct {
moose bool
}
// Middleware is a struct that has a ServeHTTP method
func NewMiddleware() *Middleware {
return &Middleware{true}
}
// The middleware handler
func (l *Middleware) ServeHTTP(w http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
// Log moose status
log.Printf("MOOSE: %v\n", l.moose)
// Call the next middleware handler
next(w, req)
}
~~~
Usage
-----
~~~ go
package main
import (
"fmt"
"net/http"
"github.com/urfave/negroni"
"github.com/xyproto/mooseware"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
fmt.Fprint(w, "Bafflesnark!")
})
n := negroni.Classic()
// Moose status
n.Use(moose.NewMiddleware())
// Handler goes last
n.UseHandler(mux)
// Serve
n.Run(":3000")
}
~~~