michi

package module
v0.0.0-...-1c3174d Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2024 License: MIT Imports: 5 Imported by: 0

README

michi

michi is a true 100% compatible with net/http router for Go web applications.

Go Reference Go Report Card MIT Code size

Features

  • True 100% compatible with net/http - http.ServerMux, http.Handler and http.HandlerFunc
  • Enhanced http.ServeMux - After Go 1.22, it is possible to use http method and path values
  • API like chi - Route, Group, With and chi middlewares
  • No external dependencies - Only use standard package
  • Lightweight - Only 200 lines. michi only supports to setup handlers, routing is delegated to http.ServeMux
  • Performance - michi == http.ServeMux

Why michi?

Go 1.22 added features to identify requests by HTTP methods like GET or POST and to use wildcards in paths. But these were already in 3rd party Routing libraries. So, we removed these overlapping features and provide a lack of http.ServeMux features.

About michi(道)

  • michi(道) means routes in Japanese.

Getting Started

go get github.com/go-michi/michi
package main

import (
	"fmt"
	"net/http"

	"github.com/go-chi/chi/v5/middleware"
	"github.com/go-michi/michi"
)

func main() {
	rt := michi.NewRouter()
	// you want to exec /a/ handler when server receives /a like chi, configure the following options.
	// rt := michi.NewRouter(michi.ExecTrailingSlashHandler())
	rt.Use(middleware.Logger)
	rt.HandleFunc("POST /a/{id}", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hello " + r.PathValue("id")))
	})
	http.ListenAndServe(":3000", rt)
}

Before using michi, please be sure to read the http.ServeMux GoDoc. For more detailed usage, please check the Example in GoDoc.

Blog

  • aaaa

Credits

Documentation

Overview

Example
package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/sonatard/michi"
)

func main() {
	handler := func(name string) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			fmt.Println(name + " handler")
		})
	}
	middleware := func(name string) func(next http.Handler) http.Handler {
		return func(next http.Handler) http.Handler {
			return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				fmt.Println(name + " start")
				next.ServeHTTP(w, r)
				fmt.Println(name + " end")
			})
		}
	}
	rt := michi.NewRouter()
	rt.Use(middleware("a"))
	rt.Handle("/a", handler("a"))
	rt.Route("/b", func(rt michi.Router) {
		rt.Use(middleware("b"))
		rt.Handle("", handler("b"))
		rt.With(middleware("c1")).Handle("/c1", handler("c1"))
		rt.Group(func(rt michi.Router) {
			rt.Use(middleware("c2"))
			rt.Handle("/c2", handler("c2"))
		})
	})
	{
		w := httptest.NewRecorder()
		target := "https://example.com/a"
		r := httptest.NewRequest(http.MethodPost, target, nil)
		fmt.Println(target)
		rt.ServeHTTP(w, r)
		fmt.Println()
	}
	{
		w := httptest.NewRecorder()
		target := "https://example.com/b"
		r := httptest.NewRequest(http.MethodPost, target, nil)
		fmt.Println(target)
		rt.ServeHTTP(w, r)
		fmt.Println()
	}
	{
		w := httptest.NewRecorder()
		target := "https://example.com/b/c1"
		r := httptest.NewRequest(http.MethodPost, target, nil)
		fmt.Println(target)
		rt.ServeHTTP(w, r)
		fmt.Println()
	}
	{
		w := httptest.NewRecorder()
		target := "https://example.com/b/c2"
		r := httptest.NewRequest(http.MethodPost, target, nil)
		fmt.Println(target)
		rt.ServeHTTP(w, r)
		fmt.Println()
	}
}
Output:

https://example.com/a
a start
a handler
a end

https://example.com/b
a start
b start
b handler
b end
a end

https://example.com/b/c1
a start
b start
c1 start
c1 handler
c1 end
b end
a end

https://example.com/b/c2
a start
b start
c2 start
c2 handler
c2 end
b end
a end

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*router)

Option is the option for router

func ExecTrailingSlashHandlerOption

func ExecTrailingSlashHandlerOption() Option

ExecTrailingSlashHandlerOption is the option for router. Execute the handler when the url path has no trailing slash.

func ServeMux

func ServeMux(serveMux *http.ServeMux) Option

ServeMux is the option for router. Change the default http.ServeMux.

type Router

type Router interface {
	// Handler has ServeHTTP(ResponseWriter, *Request)
	http.Handler
	// Use adds middlewares to the router
	Use(middlewares ...func(http.Handler) http.Handler)
	// With creates a sub router with the given middlewares
	With(middlewares ...func(http.Handler) http.Handler) Router
	// Group creates a sub router with the given sub router function
	Group(fn func(sub Router))
	// Route creates a sub router with the given sub router function and path pattern
	Route(pattern string, fn func(sub Router))
	// Handle registers a handler for the given pattern
	Handle(pattern string, handler http.Handler)
	// HandleFunc registers a handler function for the given pattern
	HandleFunc(pattern string, handlerFunc http.HandlerFunc)
}

Router is a http.Handler which can be used to dispatch requests to different

func NewRouter

func NewRouter(options ...Option) Router

NewRouter creates a new router.

Jump to

Keyboard shortcuts

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