a little chi demo

This commit is contained in:
xkm
2026-03-25 20:46:45 +08:00
parent 9f6cf16e90
commit 04f433c7f3
5 changed files with 107 additions and 1 deletions

34
cmd/api/main.go Normal file
View File

@@ -0,0 +1,34 @@
package main
import (
"log"
"net/http"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/render"
)
func main() {
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(middleware.Timeout(60 * time.Second))
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
render.JSON(w, r, map[string]interface{}{
"code": http.StatusOK,
"message": "Hello World",
"data": struct{}{},
})
})
err := http.ListenAndServe(":8080", r)
if err != nil {
log.Fatal(err)
}
}