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

34 lines
834 B
Go

package main
import (
"log"
"net/http"
"time"
)
// 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))
})
}
func (app *App) routes() http.Handler {
mux := http.NewServeMux()
// dynamic routes
mux.Handle("GET /", LoggingMiddleware(http.HandlerFunc(app.Index)))
mux.Handle("POST /up/search", LoggingMiddleware(http.HandlerFunc(app.UpSearch)))
// serve files from the "static" directory
fs := http.FileServer(http.Dir("assets/static"))
mux.Handle("GET /static/", http.StripPrefix("/", fs))
return mux
}