stack

package module
v0.0.0-...-49679f5 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2014 License: MPL-2.0 Imports: 0 Imported by: 0

README

stack GoDoc Build Status

stack is a framework to build JSON-APIs faster. It is based on the series of articles "Build Your Own Web Framework in Go" I began to write a few weeks ago.

The public API of the package is stable and you can use it right now to make your application. I don't indend to break any existing feature but will add new features to make the framework more useful for production. Look at the documentation.

I am also developing new ideas that could become new features in the experimental branch. There are some exciting stuff in it, so check it out!

Getting started

package main

import (
  "net/http"
  "github.com/nmerouze/stack/jsonapi"
)

type Tea struct {
  Name string `json:"name"`
}

type TeaCollection struct {
  Data []Tea `json:"data"`  
}

type TeaResource struct {
  Data Tea `json:"data"`  
}

func teaHandler(w http.ResponseWriter, r *http.Request) {
  res := getTeas() // Returns a *TeaCollection
  jsonapi.Write(w, res)  
}

func teaHandler(w http.ResponseWriter, r *http.Request) {
  res := getTea(mux.Params(r).ByName("id")) // Returns a *TeaResource
  jsonapi.Write(w, res)  
}

func createTeaHandler(w http.ResponseWriter, r *http.Request) {
  res := createTea(jsonapi.Body(r).(*TeaResource))
  jsonapi.Write(w, res)
}

func main() {
  m := jsonapi.New()
  m.Get("/teas").ThenFunc(teasHandler)
  m.Get("/teas/:id").ThenFunc(teaHandler)
  m.Post("/teas").Use(jsonapi.ContentTypeHandler, jsonapi.BodyHandler(TeaResource{})).ThenFunc(createTeaHandler)
}

Documentation

Overview

stack provides two packages: mux and jsonapi.

mux wraps julienschmidt/httprouter, justinas/alice and gorilla/context in an elegant API. Thus, mux is fast, uses standard middlewares and handlers syntax and stores URL params in a context.

jsonapi adds utility functions and middlewares to create JSON APIs.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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