Init: setup uptrace and swagger

This commit is contained in:
2025-12-07 03:28:07 +08:00
parent a4096bf1a7
commit 56e83b0725
28 changed files with 662 additions and 81 deletions

30
middlewares/cors.go Normal file
View File

@@ -0,0 +1,30 @@
package middlewares
import (
"net/http"
"github.com/spf13/viper"
"github.com/uptrace/bunrouter"
)
func CORSHandler(next bunrouter.HandlerFunc) bunrouter.HandlerFunc {
return func(w http.ResponseWriter, req bunrouter.Request) error {
origin := viper.GetString("cors-origin")
if origin == "" {
return next(w, req)
}
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Set("Access-Control-Allow-Credentials", "true")
if req.Method == http.MethodOptions {
w.Header().Set("Access-Control-Allow-Methods",
"GET,PUT,POST,DELETE,OPTIONS")
w.Header().Set("Access-Control-Allow-Headers",
"authorization,content-type,content-length")
return nil
}
return next(w, req)
}
}