mux/README.md

116 lines
2.8 KiB
Markdown
Raw Normal View History

2024-10-12 14:04:17 +00:00
# Mux
2024-09-30 13:31:11 +00:00
2024-10-12 14:04:17 +00:00
Tiny wrapper around Go's builtin http.ServeMux with easy routing methods.
## Example
```go
package main
import (
"log/slog"
"net/http"
"gitserver.in/patialtech/mux"
)
func main() {
2024-10-12 14:10:54 +00:00
// create a new router
2024-10-12 14:04:17 +00:00
r := mux.NewRouter()
2024-10-12 14:10:54 +00:00
// you can use any middleware that is: "func(http.Handler) http.Handler"
// so you can use any of it
// - https://github.com/gorilla/handlers
// - https://github.com/go-chi/chi/tree/master/middleware
// add some root level middlewares, these will apply to all routes after it
2024-10-12 14:04:17 +00:00
r.Use(middleware1, middleware2)
// let's add a route
2024-11-04 05:30:02 +00:00
r.GET("/hello", func(w http.ResponseWriter, r *http.Request) {
2024-10-12 14:10:54 +00:00
w.Write([]byte("i am route /hello"))
2024-10-12 14:04:17 +00:00
})
// r.Post(pattern string, h http.HandlerFunc)
// r.Put(pattern string, h http.HandlerFunc)
// ...
// you can inline middleware(s) to a route
r.
With(mwInline).
2024-11-04 05:30:02 +00:00
GET("/hello-2", func(w http.ResponseWriter, r *http.Request) {
2024-10-12 14:10:54 +00:00
w.Write([]byte("i am route /hello-2 with my own middleware"))
2024-10-12 14:04:17 +00:00
})
2024-10-12 14:10:54 +00:00
// define a resource
2024-10-12 14:04:17 +00:00
r.Resource("/photos", func(resource *mux.Resource) {
2024-10-12 14:10:54 +00:00
// rails style resource routes
2024-10-12 14:04:17 +00:00
// GET /photos
// GET /photos/new
// POST /photos
// GET /photos/:id
// GET /photos/:id/edit
2024-10-12 14:10:54 +00:00
// PUT /photos/:id
// PATCH /photos/:id
2024-10-12 14:04:17 +00:00
// DELETE /photos/:id
resource.Index(func(w http.ResponseWriter, r *http.Request) {
2024-10-12 14:10:54 +00:00
w.Write([]byte("all photos"))
2024-10-12 14:04:17 +00:00
})
resource.New(func(w http.ResponseWriter, r *http.Request) {
2024-10-12 14:10:54 +00:00
w.Write([]byte("upload a new pohoto"))
2024-10-12 14:04:17 +00:00
})
})
2024-10-12 14:10:54 +00:00
// create a group of few routes with their own middlewares
2024-10-12 14:04:17 +00:00
r.Group(func(grp *mux.Router) {
grp.Use(mwGroup)
2024-11-04 05:30:02 +00:00
grp.GET("/group", func(w http.ResponseWriter, r *http.Request) {
2024-10-12 14:10:54 +00:00
w.Write([]byte("i am route /group"))
2024-10-12 14:04:17 +00:00
})
})
2024-10-12 14:10:54 +00:00
// catches all
2024-11-04 05:30:02 +00:00
r.GET("/", func(w http.ResponseWriter, r *http.Request) {
2024-10-12 14:04:17 +00:00
w.Write([]byte("hello there"))
})
2024-10-12 14:10:54 +00:00
// Serve allows graceful shutdown, you can use it
2024-10-12 14:04:17 +00:00
r.Serve(func(srv *http.Server) error {
srv.Addr = ":3001"
// srv.ReadTimeout = time.Minute
// srv.WriteTimeout = time.Minute
slog.Info("listening on http://localhost" + srv.Addr)
return srv.ListenAndServe()
})
}
func middleware1(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
slog.Info("i am middleware 1")
h.ServeHTTP(w, r)
})
}
func middleware2(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
slog.Info("i am middleware 2")
h.ServeHTTP(w, r)
})
}
func mwInline(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
slog.Info("i am inline middleware")
h.ServeHTTP(w, r)
})
}
func mwGroup(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
slog.Info("i am group middleware")
h.ServeHTTP(w, r)
})
}
```