Skip to content

Latest commit

 

History

History
82 lines (60 loc) · 2.12 KB

README.md

File metadata and controls

82 lines (60 loc) · 2.12 KB

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