Giter Site home page Giter Site logo

routes's Introduction

routes.go

a simple http routing API for the Go programming language

go get github.com/drone/routes

for more information see: http://gopkgdoc.appspot.com/pkg/github.com/bradrydzewski/routes

Getting Started

package main

import (
    "fmt"
    "github.com/drone/routes"
    "net/http"
)

func Whoami(w http.ResponseWriter, r *http.Request) {
    params := r.URL.Query()
    lastName := params.Get(":last")
    firstName := params.Get(":first")
    fmt.Fprintf(w, "you are %s %s", firstName, lastName)
}

func main() {
    mux := routes.New()
    mux.Get("/:last/:first", Whoami)

    http.Handle("/", mux)
    http.ListenAndServe(":8088", nil)
}

Route Examples

You can create routes for all http methods:

mux.Get("/:param", handler)
mux.Put("/:param", handler)
mux.Post("/:param", handler)
mux.Patch("/:param", handler)
mux.Del("/:param", handler)

You can specify custom regular expressions for routes:

mux.Get("/files/:param(.+)", handler)

You can also create routes for static files:

pwd, _ := os.Getwd()
mux.Static("/static", pwd)

this will serve any files in /static, including files in subdirectories. For example /static/logo.gif or /static/style/main.css.

Filters / Middleware

You can apply filters to routes, which is useful for enforcing security, redirects, etc.

You can, for example, filter all request to enforce some type of security:

var FilterUser = func(w http.ResponseWriter, r *http.Request) {
	if r.URL.User == nil || r.URL.User.Username() != "admin" {
		http.Error(w, "", http.StatusUnauthorized)
	}
}

r.Filter(FilterUser)

You can also apply filters only when certain REST URL Parameters exist:

r.Get("/:id", handler)
r.Filter("id", func(rw http.ResponseWriter, r *http.Request) {
	...
})

Helper Functions

You can use helper functions for serializing to Json and Xml. I found myself constantly writing code to serialize, set content type, content length, etc. Feel free to use these functions to eliminate redundant code in your app.

Helper function for serving Json, sets content type to application/json:

func handler(w http.ResponseWriter, r *http.Request) {
	mystruct := { ... }
    routes.ServeJson(w, &mystruct)
}

Helper function for serving Xml, sets content type to application/xml:

func handler(w http.ResponseWriter, r *http.Request) {
	mystruct := { ... }
    routes.ServeXml(w, &mystruct)
}

Helper function to serve Xml OR Json, depending on the value of the Accept header:

func handler(w http.ResponseWriter, r *http.Request) {
	mystruct := { ... }
    routes.ServeFormatted(w, r, &mystruct)
}

routes's People

Contributors

bradrydzewski avatar jplock avatar

Watchers

James Cloos avatar Quintin  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.