stom

package module
v0.0.0-...-db6bee2 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2015 License: MIT Imports: 6 Imported by: 0

README

Stom

Stom is a lightweight library that helps you write web applications and APIs in Go. The goal of stom is to be as non-intrusive as possible, by just adding useful features that are commonly used in web applications without adding any magic. It tries to stick to the standard http library as much as possible, by using the standard http handlers (func(http.ResponseWriter, *http.Request)) without adding any extra fluff. It also recommends some usage patterns based in regards to handling application Context.

For routing, it uses github.com/julienschmidt/httprouter, and exposes route variables through the standard http.Request.FormValue.

Please note that this project is still extremely young and may change often. The API should remain stable, though.

Usage

package main

import (
	"log"
	"net/http"

	"github.com/jamal/stom"
	"github.com/jamal/stom/middleware"
)

func Index(w http.ResponseWriter, r *http.Request) {
	stom.WriteString(w, "Welcome")
}

func Hello(w http.ResponseWriter, r *http.Request) {
	storm.WriteString(w, "Hello, %s", r.FormValue("name"))
}

func main() {
	s := stom.New()
	s.UseAfter(middleware.Logger{})
	s.Get("/", Index)
	s.Get("/hello/:name", Hello)
	log.Fatal(http.ListenAndServe(":8080", s))
}

See example/main.go for a more detailed example.

Request Context

Request context is not handled by this library, because it isn't needed. Instead, it offers the Context interface as an suggestion on how to approach this problem. With this pattern, you only need to fetch the request context on handlers that need it, and NewContext should handle any parsing of request parameters that are needed (such as reading a session cookie and fetching the User object). For example:

type Context {
	User *User
}

func NewContext(r *http.Request) *Context {
	ctx := new(Context)
	sessionID, _ := r.Cookie("session_id")
	user, _ := FetchSessionUser(sessionID)
	ctx.User = user
	return ctx
}

func ContextHandler(w http.ResponseWriter, r *http.Request) {
	ctx := NewContext(r)
	
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WriteJSON

func WriteJSON(w http.ResponseWriter, v interface{})

WriteJSON is a helper function that will write the JSON encoded version of v to the ResponseWriter and set the application/json Content-Type header.

func WriteString

func WriteString(w http.ResponseWriter, format string, args ...interface{})

WriteString is a Printf-like helper function that will write a text/plain header and string to the ResponseWriter.

Types

type Context

type Context interface {
	NewContext(r *http.Request) Context
}

The Context interface allows you to implement a shared context that is needed among a group of request types. NewContext should process the Request for any information that is necessary for a particular request type. For example, an endpoint that requires authentication could parse the authentication header, or a session cookie, and set User in the context. The idea is that Context is not generated by stom because it may not be needed in every request. In your request handler simply call NewContext(r) when you need it.

A simple example:

type User struct {
    ID int
    Name string
}

type Context struct {
    User *User
}

func NewContext(r *http.Request) *Context {
    ctx := new(Context)
    // Error handling omitted for simplicity
    sessionID, _ := r.Cookie("session_id")
    user, _ := FetchSessionUser(sessionID)
    ctx.User = user
    return ctx
}

func HelloName(w http.ResponseWriter, r *http.Request) {
    ctx := NewContext(r)
    if ctx.User != nil {
        fmt.Fprintf(w, "Hello, %s", ctx.User.Name)
    } else {
        fmt.Fprintf(w, "Hello, I don't know who you are!")
    }
}

type Handle

type Handle func(http.ResponseWriter, *http.Request)

type ResponseWriter

type ResponseWriter struct {
	http.ResponseWriter
	Status    int
	StartTime time.Time
	// contains filtered or unexported fields
}

func (*ResponseWriter) Write

func (w *ResponseWriter) Write(data []byte) (int, error)

func (*ResponseWriter) WriteHeader

func (w *ResponseWriter) WriteHeader(status int)

type Server

type Server struct {
	PanicHandler func(http.ResponseWriter, *http.Request, interface{})
	// contains filtered or unexported fields
}

func New

func New() *Server

func (*Server) Delete

func (s *Server) Delete(path string, handle Handle)

func (*Server) Get

func (s *Server) Get(path string, handle Handle)

func (*Server) Handle

func (s *Server) Handle(method, path string, handle Handle)

func (*Server) Head

func (s *Server) Head(path string, handle Handle)

func (*Server) Post

func (s *Server) Post(path string, handle Handle)

func (*Server) Put

func (s *Server) Put(path string, handle Handle)

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(hw http.ResponseWriter, r *http.Request)

func (*Server) Use

func (s *Server) Use(middleware http.Handler)

func (*Server) UseAfter

func (s *Server) UseAfter(middleware http.Handler)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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