• Stars
    star
    194
  • Rank 200,219 (Top 4 %)
  • Language
    Go
  • License
    MIT License
  • Created over 8 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

HTTP routing for Go 1.7

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.

More Repositories

1

xbar

Put the output from any script or program into your macOS Menu Bar (the BitBar reboot)
Go
17,509
star
2

xbar-plugins

Plugin repository for xbar (the BitBar reboot)
Shell
2,446
star
3

moq

Interface mocking tool for go generate
Go
1,884
star
4

is

Professional lightweight testing mini-framework for Go.
Go
1,731
star
5

vice

Go channels at horizontal scale (powered by message queues)
Go
1,540
star
6

goblueprints

Source code for Go Programming Blueprints
Go
1,346
star
7

silk

Markdown based document-driven RESTful API testing.
Go
939
star
8

gopherize.me

Gopherize.me app
JavaScript
649
star
9

try

Simple idiomatic retry package for Go
Go
332
star
10

drop

Dependency-less dependencies for Go.
Go
120
star
11

goscript

Goscript: Runtime execution of Go code.
Go
103
star
12

runner

Interruptable goroutines
Go
99
star
13

respond

Idiomatic API responses for Go.
Go
84
star
14

resync

sync.Once with Reset()
Go
71
star
15

anno

Go package for text annotation.
Go
70
star
16

crunchcrunchcrunchstack

This repo is a starting point for a Go + Svelte.js + TailwindCSS project.
Go
64
star
17

golanguk

Source code for Golang UK talk - August 21st 2015, London.
Go
42
star
18

httperr

HTTP error wrapper
Go
35
star
19

articles

Source code repository for articles on https://medium.com/@matryer
Go
34
star
20

m

Get and Set using JavaScript notation
Go
31
star
21

pangaea

Powerful pre-processor for any kind of text file - powered by JavaScript
Go
29
star
22

version

Command line tool for versioning projects
Go
28
star
23

gae-records

Active Record like wrapper for Google App Engine Datasource in Go
Go
28
star
24

present

Presentations repository
Go
26
star
25

codeform

Easy Go code generation using templates
Go
25
star
26

filedb

File based storage and querying package
Go
23
star
27

mix

Go http.Handler that mixes many files into one request.
Go
13
star
28

captainslog

Logging package for Go
Go
12
star
29

persist

Persist loads and saves Go objects to files
Go
11
star
30

appengine

Example application for Google App Engine (As of November 2018)
Go
10
star
31

gocmds

Template for Go commands
Go
7
star
32

flower

In-process task flow management
Go
6
star
33

PathKit

Path tools for iOS and SpriteKit
Objective-C
6
star
34

oo

oo is the worlds simplest JavaScript OO implementation; for when you need classes and nothing else.
JavaScript
4
star
35

github-downloads

Tool (and Go package) for counting GitHub downloads
Go
4
star
36

str

String parsing package for Go. Converts strings to best guess value type.
Go
4
star
37

isvalid

Ultra simple data parsing and validation in Go
Go
3
star
38

codeform-templates

Repository of Codeform templates
Smarty
3
star
39

funkjs

A sweet selection of JavaScript funktion extensions
JavaScript
2
star
40

tailwindcss-github-theme

TailwindCSS GitHub theme for building GitHub styled apps using Tailwind.
2
star
41

rapid-api

Code for 3.5 hour workshop project at infoShare 2016
1
star
42

testing-in-go

Tutorial repo for Testing in Go talk
1
star
43

berlin-devfest-workshop

Code created during Berlin DevFest Workshop
Go
1
star
44

gimme

old location for github.com/matryer/drop
1
star