mirror of
https://github.com/NotAShelf/catApi.git
synced 2025-02-23 11:59:47 +00:00
log API requests
This commit is contained in:
parent
b7319e6bfc
commit
fee1ae20ed
1 changed files with 22 additions and 8 deletions
30
main.go
30
main.go
|
@ -48,15 +48,19 @@ func main() {
|
||||||
|
|
||||||
images = getImages()
|
images = getImages()
|
||||||
|
|
||||||
http.HandleFunc("/", homeHandler)
|
// Add request logging middleware
|
||||||
http.HandleFunc("/api/id", idHandler)
|
mux := http.NewServeMux()
|
||||||
http.HandleFunc("/api/list", listHandler)
|
mux.HandleFunc("/", homeHandler)
|
||||||
http.HandleFunc("/api/random", randomHandler)
|
mux.HandleFunc("/api/id", idHandler)
|
||||||
|
mux.HandleFunc("/api/list", listHandler)
|
||||||
http.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("/api/random", randomHandler)
|
||||||
|
mux.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Error(w, "Invalid API path", http.StatusNotFound)
|
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.Println("Server started at port", port)
|
||||||
log.Fatal(http.ListenAndServe(":"+port, nil))
|
log.Fatal(http.ListenAndServe(":"+port, nil))
|
||||||
}
|
}
|
||||||
|
@ -67,11 +71,11 @@ func getImages() []string {
|
||||||
logger.WithError(err).Fatal("Error reading images directory")
|
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")
|
logger.Warn("No images found in the images directory")
|
||||||
}
|
}
|
||||||
|
|
||||||
var images []string
|
var images []string
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
images = append(images, file.Name())
|
images = append(images, file.Name())
|
||||||
logger.Info("Loaded image:", file.Name())
|
logger.Info("Loaded image:", file.Name())
|
||||||
|
@ -125,6 +129,16 @@ func listHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write(jsonData)
|
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) {
|
func randomHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
i := rand.Intn(len(images))
|
i := rand.Intn(len(images))
|
||||||
|
|
Loading…
Add table
Reference in a new issue