Files
crowsnest/golang/cmd/frontend/routes.go

34 lines
834 B
Go
Raw Normal View History

2025-01-02 16:21:02 +01:00
package main
import (
2025-01-11 01:35:25 +01:00
"log"
2025-01-02 16:21:02 +01:00
"net/http"
2025-01-11 01:35:25 +01:00
"time"
2025-01-02 16:21:02 +01:00
)
2025-01-11 01:35:25 +01:00
// LoggingMiddleware logs details about each incoming HTTP request.
func LoggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
// Call the next handler
next.ServeHTTP(w, r)
log.Printf("[request] %s %s from %s (%v)", r.URL.Path, r.Method, r.RemoteAddr, time.Since(start))
})
}
2025-01-02 16:21:02 +01:00
func (app *App) routes() http.Handler {
2025-01-11 01:35:25 +01:00
mux := http.NewServeMux()
2025-01-02 16:21:02 +01:00
2025-01-11 01:35:25 +01:00
// dynamic routes
mux.Handle("GET /", LoggingMiddleware(http.HandlerFunc(app.Index)))
mux.Handle("POST /up/search", LoggingMiddleware(http.HandlerFunc(app.UpSearch)))
2025-01-02 16:21:02 +01:00
2025-01-11 01:35:25 +01:00
// serve files from the "static" directory
2025-01-02 16:21:02 +01:00
fs := http.FileServer(http.Dir("assets/static"))
mux.Handle("GET /static/", http.StripPrefix("/", fs))
2025-01-11 01:35:25 +01:00
return mux
2025-01-02 16:21:02 +01:00
}