An open API service indexing awesome lists of open source software.

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: 6 months ago
JSON representation

:skull: Skeleton for writing a middleware handler

Awesome Lists containing this project

README

          

Mooseware
=========

[![Build Status](https://travis-ci.com/xyproto/mooseware.svg?branch=master)](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")
}
~~~