aloja

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2015 License: MIT Imports: 12 Imported by: 0

README

aloja - little opinionated framework for plain http.Handlers

This framework provide convenience for the usual suspects when developing RESTful APIs or Web Applications.

It uses mainstream and common utilities and it is heavily opinionated with focus to alleviate repetitive tasks.

Features

In this release, we have:

  • Fast routing (by way of httptreemux)
  • Zero downtime restarts through endless
  • Per request Context (provided by httpcontext)
  • Middleware (gently managed by alice)
  • Named params
  • Serve static pages
  • Serve http.Templates
  • Some middleware: -- compression -- logs -- cors

Installation

go get github.com/jllopis/aloja

Quick sample

The framework lacks documentation. The use is something like this:

package main

import (
	"fmt"
	"net/http"
	"testing"

	"github.com/jllopis/aloja"
	"github.com/jllopis/aloja/mw"
)

func indexHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Welcome!")
}

func sayHello(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello little grasshoper!")
}

func sayHelloName(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello %s!", aloja.Params(r).ByName("name"))
}

func main() {
	// New server on port 8888
	server := New().Port("8888")

	// Use CORS Handler in every request
	server.AddGlobal(mw.CorsHandler)

	// Log every request
	server.AddGlobal(mw.LogHandler)

	// handler on main route
	server.HandleFunc("GET", "/", indexHandler)

	// Create a subrouter on branch /v1
	r1 := server.NewSubrouter("/v1")

	// Accept compression on this subrouter
	r1.Use(mw.CompressHandler)

	// Say hello!
	r1.Get("/hello", http.HandlerFunc(sayHello))

	// Say hello!
	r1.Get("/hello/:name", http.HandlerFunc(sayHelloName))

	// Run rabbit run!
	server.Run()
}

Documentation

Index

Constants

View Source
const (
	VERSION = "v0.2.0"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Aloja

type Aloja struct {
	*Subrouter
	// contains filtered or unexported fields
}

Aloja has the methods to abstract the work away. It provides a default Subrouter to carry on requests to '/'. You can coall NewSubrouter method on it to group routes and apply different middlewares to them

func New

func New(options ...func(s *Aloja) *Aloja) *Aloja

New creates a new Aloja with options. Accepted are: - Host - Port - SSLConf It exposes a global middleware that is called on every request, independently of the sobrouter configured if any

func (*Aloja) AddGlobal

func (s *Aloja) AddGlobal(m ...mw.Middleware)

Add stacks a new middleware. It will be the last called. It accepts a list of middlewares and ordere is preserved left to right.

func (*Aloja) Host

func (s *Aloja) Host(h string) *Aloja

Host set the hostname for the running server

func (*Aloja) LoadTemplates

func (s *Aloja) LoadTemplates(tdir string, templateDelims []string) (*template.Template, error)

func (*Aloja) Port

func (s *Aloja) Port(p string) *Aloja

Port set the port for the running server

func (*Aloja) Run

func (s *Aloja) Run()

Run is a convenience function to start an http server. If cert and key were provided, ONLY https will be started! If they aren't, http will boot up. NOT RECOMMENDED!

func (*Aloja) SSLConf

func (s *Aloja) SSLConf(cert, key string) *Aloja

SSLConf pass in the cert and key for StartTLS

type ParamCol added in v0.2.0

type ParamCol map[string]string

func Params

func Params(req *http.Request) ParamCol

Params returns the httptreemux.Params for req.

func (ParamCol) ByName added in v0.2.0

func (p ParamCol) ByName(name string) string

type Subrouter

type Subrouter struct {
	*mw.Stack
	// contains filtered or unexported fields
}

func (*Subrouter) Delete

func (s *Subrouter) Delete(path string, handler http.Handler)

Delete registers a DELETE handler for the given path.

func (*Subrouter) Get

func (s *Subrouter) Get(path string, handler http.Handler)

Get registers a GET handler for the given path.

func (*Subrouter) Handle

func (s *Subrouter) Handle(method string, path string, h http.Handler)

Handle serves an endpoint with the provided handler

func (*Subrouter) HandleFunc

func (s *Subrouter) HandleFunc(method string, path string, f func(w http.ResponseWriter, r *http.Request))

HandleFunc serves an endpoint with the provided handler

func (*Subrouter) NewSubrouter

func (s *Subrouter) NewSubrouter(subpath string, middlewares ...mw.Middleware) *Subrouter

func (*Subrouter) Options

func (s *Subrouter) Options(path string, handler http.Handler)

Options registers a OPTIONS handler for the given path.

func (*Subrouter) Patch

func (s *Subrouter) Patch(path string, handler http.Handler)

Patch registers a PATCH handler for the given path.

func (*Subrouter) Post

func (s *Subrouter) Post(path string, handler http.Handler)

Post registers a POST handler for the given path.

func (*Subrouter) Put

func (s *Subrouter) Put(path string, handler http.Handler)

Put registers a PUT handler for the given path.

func (*Subrouter) ServeStatic

func (s *Subrouter) ServeStatic(rpath string, dir string)

ServeStatic provides a quick way to serve static files

func (*Subrouter) Use

func (s *Subrouter) Use(m ...mw.Middleware)

func (*Subrouter) UseHandler added in v0.2.0

func (s *Subrouter) UseHandler(handler http.Handler)

UseHandler registers an http.Handler as a middleware.

Directories

Path Synopsis
Package original source: https://github.com/gorilla/handlers
Package original source: https://github.com/gorilla/handlers

Jump to

Keyboard shortcuts

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