go-michi / michi

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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 middlewares
  • No external dependencies - Only use standard package
  • Lightweight - Only 160 lines
  • Performance - Fast michi == http.ServeMux

Why michi?

After Go 1.22, HTTP routing in the standard library is now more expressive. The patterns used by net/http.ServeMux have been enhanced to accept methods and wildcards. 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 -u 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() {
  r := michi.NewRouter()
  r.Use(middleware.Logger)
  r.HandleFunc("POST /a/{id}/{$}", func(w http.ResponseWriter, req *http.Request) {
    w.Write([]byte("Hello " + req.PathValue("id")))
  })
  http.ListenAndServe(":3000", r)
}

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

Migrating to michi(http.ServeMux) from chi

There are several changes, but rather than changing from chi to michi, it's about changing from chi to http.ServeMux. Therefore, what you need to understand is how to use the standard library's http.ServeMux, and the knowledge specific to michi is kept to a minimum.

import michi package

This change is due to michi.

- import  "github.com/go-chi/chi"
+ import  "github.com/go-michi/michi"

func main() {
-   r := chi.NewRouter()
+   r := michi.NewRouter()
}

Use Handle or HandleFunc method instead of Get, Post, Put, Delete, Patch, Options, Head method

This change is due to http.ServeMux.

func main() {
-   r.Get("/user/{id}", userHandler)
+   r.HandleFunc("GET /user/{id}", userHandler)
}

Use http.Request.PathValue method

This change is due to http.ServeMux.

func Handler(w http.ResponseWriter, r *http.Request) {
-   id := chi.URLParam(r, "id")
+   id := r.PathValue("id")
}

Use {$} suffix for exact match

This change is due to http.ServeMux.

  • with {$}, routing pattern match rule is same as chi
    • /a/{$} matches request /a/
  • without {$}, routing pattern match rule is same as old http.ServeMux
    • /a/ matches request /a/ and /a/b
func main() {
-   r.Handle("/a/", userHandler)
+   r.Handle("/a/{$}", userHandler)
}

Sub Router

This change is due to http.ServeMux. http.ServeMux doesn't have Mount method, use Handle method instead of Mount method. Handle method can't omit parent path.

// chi
func main() {
   r := chi.NewRouter()
   // omit /a/ path
   r.Handle("/hello", handler("hello"))
   r2 := chi.NewRouter()
   r2.Mount("/a", r)
 }
// michi
func main() {
    r := michi.NewRouter()
    // can't omit /a/ path
    r.Handle("/a/hello", handler("hello"))
    r2 := michi.NewRouter()
    r2.Handle("/a/", r)
}
func main() {
-   r.Handle("/hello", handler("hello"))
+   r.Handle("/a/hello", handler("hello"))
-   r2.Mount("/a", r)
+   r2.Handle("/a/", r)
 }

or using Route

// michi
func main() {
    r := michi.NewRouter()
    // can't omit /a/ path
    r.Route("/a", func(r michi.Router) {
        r.Handle("/hello", handler("hello"))
    })
}

Support version

michi only supports Go 1.22 or later and the two latest versions. Currently, supports Go 1.22.

Reference

Credits

About

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

License:MIT License


Languages

Language:Go 100.0%