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