41 lines
1011 B
Go
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)
|
|
}
|