Skip to content

FGtatsuro/tinyrouter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tinyrouter

Minimum HTTP request router

Usage

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/FGtatsuro/tinyrouter"
)

func main() {
	router := tinyrouter.New()
	// Same signature to http.Handle
	router.Handle("/handle", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("handle\n"))
	}))
	// Same signature to http.HandleFunc
	router.HandleFunc("/handlefunc", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("handlefunc\n"))
	})
	// Get PathVars via context
	router.HandleFunc("/users/{[0-9a-zA-Z]+}", func(w http.ResponseWriter, r *http.Request) {
		ctx := r.Context()
		vars := ctx.Value(tinyrouter.PathVarsContextKey).([]string)
		w.Write([]byte(fmt.Sprintf("user %v\n", vars[0])))
	})
	http.Handle("/", router)

	log.Fatal(http.ListenAndServe(":8080", nil))
}

About

Minimum HTTP request router

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages