Compare commits
No commits in common. "3504e132addbab430db2690c2d7d7805ca27a1b8" and "43e476d8973bdce08e9c7a53252cd2ce3396fe36" have entirely different histories.
3504e132ad
...
43e476d897
@ -10,18 +10,20 @@ type FinishRequestBody struct {
|
||||
Id string `json:"id"`
|
||||
}
|
||||
|
||||
func Finish(r *http.Request) (interface{}, int) {
|
||||
func Finish(w http.ResponseWriter, r *http.Request) {
|
||||
d := json.NewDecoder(r.Body)
|
||||
body := FinishRequestBody{}
|
||||
err := d.Decode(&body)
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = tasks.Finish(body.Id)
|
||||
if err != nil {
|
||||
return nil, http.StatusInternalServerError
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
return nil, http.StatusAccepted
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
@ -15,12 +15,13 @@ type PutRequestBody struct {
|
||||
Delay *int `json:"delay"`
|
||||
}
|
||||
|
||||
func Put(r *http.Request) (interface{}, int) {
|
||||
func Put(w http.ResponseWriter, r *http.Request) {
|
||||
d := json.NewDecoder(r.Body)
|
||||
body := PutRequestBody{}
|
||||
err := d.Decode(&body)
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
queue := r.Header.Get("queue")
|
||||
@ -43,8 +44,9 @@ func Put(r *http.Request) (interface{}, int) {
|
||||
|
||||
err = tasks.Add(task)
|
||||
if err != nil {
|
||||
return nil, http.StatusInternalServerError
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
return nil, http.StatusAccepted
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package routers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
tasks "queues-go/app/storage/mongo/collections"
|
||||
)
|
||||
@ -15,11 +16,13 @@ type TakeResponse struct {
|
||||
Task *TaskResponse `json:"task"`
|
||||
}
|
||||
|
||||
func Take(r *http.Request) (interface{}, int) {
|
||||
func Take(w http.ResponseWriter, r *http.Request) {
|
||||
queue := r.Header.Get("queue")
|
||||
task, err := tasks.Take(queue)
|
||||
if err != nil {
|
||||
return nil, http.StatusInternalServerError
|
||||
println("Error taking")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
var response TakeResponse
|
||||
@ -33,5 +36,12 @@ func Take(r *http.Request) (interface{}, int) {
|
||||
}
|
||||
}
|
||||
|
||||
return response, http.StatusOK
|
||||
data, err := json.Marshal(response)
|
||||
if err != nil {
|
||||
println("Error marshalling")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Write(data)
|
||||
}
|
||||
|
29
main.go
29
main.go
@ -1,38 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"queues-go/app/routers"
|
||||
client "queues-go/app/storage/mongo"
|
||||
"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()
|
||||
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.HandleFunc("/api/v1/take", routers.Take)
|
||||
http.HandleFunc("/api/v1/finish", routers.Finish)
|
||||
http.HandleFunc("/api/v1/put", routers.Put)
|
||||
http.ListenAndServe(":1239", nil)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user