mirror of
https://github.com/woodchen-ink/random-api-go.git
synced 2025-07-18 05:42:01 +08:00
- Updated docker-compose.yml to mount logs directory. - Changed BASE_URL environment variable to point to the new API endpoint. - Refactored main.go to implement a structured App type, improving initialization and graceful shutdown. - Enhanced config management with JSON loading and environment variable support. - Added monitoring capabilities in api_handler for logging request metrics. - Introduced new metrics display in index.html with corresponding CSS styles for better visualization.
26 lines
647 B
Go
26 lines
647 B
Go
package middleware
|
|
|
|
import "net/http"
|
|
|
|
// Chain 用于组合多个中间件
|
|
func Chain(middlewares ...func(http.Handler) http.Handler) func(http.Handler) http.Handler {
|
|
return func(next http.Handler) http.Handler {
|
|
for i := len(middlewares) - 1; i >= 0; i-- {
|
|
next = middlewares[i](next)
|
|
}
|
|
return next
|
|
}
|
|
}
|
|
|
|
// Recovery 中间件用于捕获 panic
|
|
func Recovery(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
defer func() {
|
|
if err := recover(); err != nil {
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
}
|
|
}()
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|