Code Monkey home page Code Monkey logo

way's Introduction

Way

HTTP router for Go 1.7

  • Deliberately simple
  • Extremely fast
  • Route based on HTTP methods and path
  • Path parameters via Context (e.g. /music/:band/:song)
  • Trailing / matches path prefixes

Install

There's no need to add a dependency to Way, just copy way.go and way_test.go into your project, or drop them in:

drop github.com/matryer/way

If you prefer, it is go gettable:

go get github.com/matryer/way

Usage

  • Use NewRouter to make a new Router
  • Call Handle and HandleFunc to add handlers
  • Specify HTTP method and path pattern for each route
  • Use Param function to get the path parameters from the context
func main() {
	router := way.NewRouter()

	router.HandleFunc("GET", "/music/:band/:song", handleReadSong)
	router.HandleFunc("PUT", "/music/:band/:song", handleUpdateSong)
	router.HandleFunc("DELETE", "/music/:band/:song", handleDeleteSong)

	log.Fatalln(http.ListenAndServe(":8080", router))
}

func handleReadSong(w http.ResponseWriter, r *http.Request) {
	band := way.Param(r.Context(), "band")
	song := way.Param(r.Context(), "song")
	// use 'band' and 'song' parameters...
}
  • Prefix matching

To match any path that has a specific prefix, use the ... prefix indicator:

func main() {
	router := way.NewRouter()

	router.HandleFunc("GET", "/images...", handleImages)
	log.Fatalln(http.ListenAndServe(":8080", router))
}

In the above example, the following paths will match:

  • /images

  • /images/

  • /images/one/two/three.jpg

  • Set Router.NotFound to handle 404 errors manually

func main() {
	router := way.NewRouter()
	
	router.NotFound = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusNotFound)
		fmt.Fprintf(w, "This is not the page you are looking for")
	})
	
	log.Fatalln(http.ListenAndServe(":8080", router))
}

Why another HTTP router?

I know, I know. But no routers offer the simplicity of path parameters via Context, and HTTP method matching. Which covers 100% of my use cases so far.

way's People

Contributors

matryer avatar martinrue avatar

Watchers

 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.