Goji

A web microframework for Golang

Godoc   /   GitHub
            
package main

import (
        "fmt"
        "net/http"

        "github.com/zenazn/goji"
        "github.com/zenazn/goji/web"
)

func hello(c web.C, w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello, %s!", c.URLParams["name"])
}

func main() {
        goji.Get("/hello/:name", hello)
        goji.Serve()
}
          

Flexible Routing

            
func main() {
        // Use your favorite HTTP verbs
        goji.Get("/", root)
        goji.Post("/comment", newComment)
        // Sinatra-style patterns are supported
        goji.Get("/comment/:id", getComment)
        // As are regular expressions
        re := regexp.MustCompile("^/comment/(?P<id>\d+)$")
        goji.Delete(re, deleteComment)

        // Compatible with your net/http Handlers
        goji.Get("/comments", http.RedirectHandler("/comment", 301))
}
          

Extensible Middleware

            
func MyMiddleware(c *web.C, h http.Handler) http.Handler {
        fn := func (w http.ResponseWriter, r *http.Request) {
                // Pass data through the environment
                c.Env["Hello"] = "world"
                // Fully control how the next layer is called
                h.ServeHTTP(w, r)
        }
        return http.HandlerFunc(fn)
}

func main() {
        // Middleware are fully reconfigurable at any time
        goji.Use(MyMiddleware)
        goji.Insert(NotPicturedMiddleware, MyMiddleware)
        goji.Abandon(MyMiddleware)
}
          

Production Ready

            
func main() {
        // This line gives you:
        // - Out-of-the-box support for TCP, UNIX sockets, Einhorn, and systemd
        // - Best-in-class graceful shutdown
        // - Zero-downtime code reloading (with Einhorn)
        // It's really that simple!
        goji.Serve()
}