mux/example/main.go

113 lines
2.8 KiB
Go
Raw Permalink Normal View History

2024-10-12 14:04:17 +00:00
package main
import (
"log/slog"
"net/http"
"gitserver.in/patialtech/mux"
"gitserver.in/patialtech/mux/middleware"
2024-10-12 14:04:17 +00:00
)
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()
r.Use(middleware.CORS(middleware.CORSOption{
AllowedOrigins: []string{"*"},
MaxAge: 60,
}))
2024-10-12 14:04:17 +00:00
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
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-10-12 14:10:54 +00:00
Get("/hello-2", func(w http.ResponseWriter, r *http.Request) {
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 15:42:23 +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
// PUT /photos/:id
// PATCH /photos/:id
// 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)
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
})
})
// catches all
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
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)
})
}