log API requests

This commit is contained in:
raf 2025-02-08 16:46:07 +03:00
parent b7319e6bfc
commit fee1ae20ed
No known key found for this signature in database
GPG key ID: EED98D11B85A2819

30
main.go
View file

@ -48,15 +48,19 @@ func main() {
images = getImages()
http.HandleFunc("/", homeHandler)
http.HandleFunc("/api/id", idHandler)
http.HandleFunc("/api/list", listHandler)
http.HandleFunc("/api/random", randomHandler)
http.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
// Add request logging middleware
mux := http.NewServeMux()
mux.HandleFunc("/", homeHandler)
mux.HandleFunc("/api/id", idHandler)
mux.HandleFunc("/api/list", listHandler)
mux.HandleFunc("/api/random", randomHandler)
mux.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Invalid API path", http.StatusNotFound)
})
// Wrap the mux with the logging middleware
http.Handle("/", logRequest(mux))
log.Println("Server started at port", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
@ -67,11 +71,11 @@ func getImages() []string {
logger.WithError(err).Fatal("Error reading images directory")
}
if len(files) == 0 {
if len(files) == 0 {
logger.Warn("No images found in the images directory")
}
var images []string
var images []string
for _, file := range files {
images = append(images, file.Name())
logger.Info("Loaded image:", file.Name())
@ -125,6 +129,16 @@ func listHandler(w http.ResponseWriter, r *http.Request) {
w.Write(jsonData)
}
func logRequest(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
logger.Infof("Started %s %s", r.Method, r.URL.Path)
next.ServeHTTP(w, r)
duration := time.Since(start)
logger.Infof("Completed %s %s in %v", r.Method, r.URL.Path, duration)
})
}
func randomHandler(w http.ResponseWriter, r *http.Request) {
rand.Seed(time.Now().UnixNano())
i := rand.Intn(len(images))