route

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

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

Go to latest
Published: Mar 12, 2015 License: Apache-2.0 Imports: 5 Imported by: 12

README

route

taironas/route is an URL router for Go allowing usage of regexp in URL paths.

Getting Started

After installing Go and setting up your environment, create a .go file named main.go.

package main

import (
  "github.com/taironas/route"
  "net/http"
  "fmt"
)

func main() {
  r := new(route.Router)
  r.Handle("/", http.FileServer(http.Dir("/static_files")))
  r.HandleFunc("/users", usersHandler)
  r.HandleFunc("/users/:id", userHandler)
  r.HandleFunc("/users/:id/friends/:username", friendHandler)
  r.AddStaticResource("static/images")

  http.ListenAndServe(":8080", r)
}

func usersHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Welcome to users handler!")
}

func userHandler(w http.ResponseWriter, r *http.Request) {
  fmt.Fprintf(w, "Welcome to user handler, user id = %s!", route.Context.Get(r, "id"))
}

func friendHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Welcome to friend handler, friend username = %s!", route.Context.Get(r, "username"))
}

Then get the route package:

> go get github.com/taironas/route

Then build your server:

> cd $GOPATH/<APP_PATH>/<APP_DIR>
> go build

Then run your server:

> go run main

You will now have a Go net/http webserver running on localhost:8080.

Named Parameters

named parameters are accessible via route.Context:

r.HandleFunc("/hello/:name", helloHandler)

func helloHandler(w http.ResponseWriter, r *http.Request) {
  fmt.Fprintf(w, "Hello %s!", route.Context.Get(r, "name"))
}

If same named parameter is used more than one time in a path, it is the value of the last parameter which will be retrieved.

Trailing Slash

No need to define /hello/?, just /hello. localhost:8080/hello/ and localhost:8080/hello will reach the same handler.

Testing

> go test

Documentation

> godoc -http=:6060

Documentation

Overview

Package route provides URL router allowing usage of regexp in URL paths.

package main

import (
  "github.com/taironas/route"
  "net/http"
  "fmt"
)

func main() {
  r := new(route.Router)
  r.HandleFunc("/users", usersHandler)
  r.HandleFunc("/users/:id", userHandler)
  r.HandleFunc("/users/:id/friends/:username", friendHandler)

  http.ListenAndServe(":8080", r)
}

func usersHandler(w http.ResponseWriter, r *http.Request) {
  fmt.Fprintf(w, "Welcome to users handler!")
}

func userHandler(w http.ResponseWriter, r *http.Request) {
  fmt.Fprintf(w, "Welcome to user handler, user id = %s!", route.Context.Get(r, "id"))
}

func friendHandler(w http.ResponseWriter, r *http.Request) {
  fmt.Fprintf(w, "Welcome to friend handler, friend username = %s!", route.Context.Get(r, "username"))
}

Index

Constants

This section is empty.

Variables

View Source
var Context context

Context holds URL parameters indexed by HTTP request

Functions

This section is empty.

Types

type Router

type Router struct {
	// contains filtered or unexported fields
}

Router serves HTTP requests for added routes and static resources

func (*Router) AddStaticResource

func (r *Router) AddStaticResource(resource *string)

AddStaticResource adds a resource value to an array of static resources. Use this if you want to serve a static directory and it's sub directories.

func (*Router) Handle

func (r *Router) Handle(pattern string, handler http.Handler)

Handle registers the handler for the given pattern in the router.

func (*Router) HandleFunc

func (r *Router) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))

HandleFunc registers the handler function for the given pattern in the router.

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP looks for a matching route among the routes. Returns 404 if no match is found.

Jump to

Keyboard shortcuts

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