initial
This commit is contained in:
42
app/routers/take.go
Normal file
42
app/routers/take.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package routers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
tasks "queues-go/app/storage/mongo/collections"
|
||||
)
|
||||
|
||||
type TaskResponse struct {
|
||||
Id string `json:"id"`
|
||||
Attempt int `json:"attempt"`
|
||||
Payload interface{} `json:"payload"`
|
||||
}
|
||||
|
||||
type TakeResponse struct {
|
||||
Task *TaskResponse `json:"task"`
|
||||
}
|
||||
|
||||
func Take(w http.ResponseWriter, r *http.Request) {
|
||||
queue := r.Header.Get("queue")
|
||||
task, err := tasks.Take(queue)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
var response TakeResponse
|
||||
if task == nil {
|
||||
response.Task = nil
|
||||
} else {
|
||||
response.Task = &TaskResponse{
|
||||
Id: task.Id.Hex(),
|
||||
Attempt: task.Attempts,
|
||||
Payload: task.Payload,
|
||||
}
|
||||
}
|
||||
data, err := json.Marshal(response)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Write(data)
|
||||
}
|
||||
Reference in New Issue
Block a user