queues-go/main.go
Egor Matveev bc411ea644
All checks were successful
Deploy Dev / Build (pull_request) Successful in 38s
Deploy Dev / Push (pull_request) Successful in 24s
Deploy Dev / Deploy dev (pull_request) Successful in 7s
fix
2025-01-06 13:33:28 +03:00

41 lines
1011 B
Go

package main
import (
"encoding/json"
"log"
"net/http"
"queues-go/app/routers"
client "queues-go/app/storage/mongo"
"sync"
"time"
)
func handlerWrapper(f func(*http.Request) (interface{}, int)) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
response, status := f(r)
if response != nil {
data, err := json.Marshal(response)
if err != nil {
log.Printf("Error marshalling response")
status = http.StatusInternalServerError
}
w.WriteHeader(status)
w.Write(data)
} else {
w.WriteHeader(status)
}
log.Printf("%s %d %s", r.URL, status, time.Since(start))
}
}
func main() {
client.Connect()
routers.MutexMap = make(map[string]*sync.Mutex)
http.HandleFunc("/api/v1/take", handlerWrapper(routers.Take))
http.HandleFunc("/api/v1/finish", handlerWrapper(routers.Finish))
http.HandleFunc("/api/v1/put", handlerWrapper(routers.Put))
log.Printf("Server started")
http.ListenAndServe(":1239", nil)
}