Skip to content

Orujo allows the execution of several middlewares per route, working seamlessly with the standard net/http library.

License

Notifications You must be signed in to change notification settings

jroimartin/orujo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

66 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Orujo GoDoc

Introduction

Orujo solves a common problem, which is the execution of several middlewares per route. It has been designed to work seamlessly with the standard net/http library.

Routes and Pipes

So, how can I link several actions/middlewares with a route? the answer is "pipes". Let me show this with a single example:

package main

import (
    "fmt"
    "log"
    "net/http"

    "github.com/jroimartin/orujo"
)

func main() {
    http.Handle("/hello", orujo.NewPipe(
        http.HandlerFunc(helloHandler),
        orujo.M(http.HandlerFunc(worldHandler)),
    ))
    log.Fatal(http.ListenAndServe(":8080", nil))
}

func helloHandler(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(http.StatusInternalServerError)
    fmt.Fprint(w, "Hello, ")
}

func worldHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "world")
}

In this example, we are linking the route /hello/ with the following pipe of handlers:

helloHandler --> M(worldHandler)

A pipe is a sequence of handlers that will be executed until one of the handlers explicitly calls the function ResponseWriter.WriteHeader(). From that moment only mandatory handlers get executed. In this example, the only mandatory handler in the pipe would be "worldHandler", which was marked as mandatory using the helper function M().

http.Handler

One of the main goals behind Orujo is standarization. Due to this, the handlers accepted by Orujo must satisfy the interface http.Handler and, of course, the returned pipe also implements the interface http.Handler. This way, everything that already works with the Go's standard library must work with Orujo.

func (h LogHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	...
}

Some ready-to-use handlers are present in this repo (e.g. basic auth, logging, sessions). They can be used via:

import "github.com/jroimartin/orujo/<handler_name>"

Installation

go get github.com/jroimartin/orujo

More information

godoc github.com/jroimartin/orujo

About

Orujo allows the execution of several middlewares per route, working seamlessly with the standard net/http library.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages