orujo

package module
v0.0.0-...-8b413d3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2015 License: BSD-3-Clause Imports: 1 Imported by: 3

README

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

Documentation

Overview

Package 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. A simple hello world would look like the following snippet:

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Errors

func Errors(w http.ResponseWriter) []error

Errors is used to retrieve the errors registered via RegisterError() during the execution of the handlers pipe.

func M

func M(h http.Handler) http.Handler

M is a helper to set a handler as "mandatory".

func NewPipe

func NewPipe(handlers ...http.Handler) http.Handler

NewPipe returns a new HTTP pipe.

func RegisterError

func RegisterError(w http.ResponseWriter, err error)

RegisterError can be used by Handlers to register errors.

Types

This section is empty.

Directories

Path Synopsis
Package basic implements basic auth mechanisms for orujo.
Package basic implements basic auth mechanisms for orujo.
Package log implements the bult-in logging handler of orujo.
Package log implements the bult-in logging handler of orujo.
Package sessions implements the bult-in sessions handler of orujo.
Package sessions implements the bult-in sessions handler of orujo.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL