-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
40 lines (31 loc) · 789 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"fmt"
"net/http"
"time"
"github.com/aniketxpawar/go-task-manager/manager"
"github.com/gin-gonic/gin"
)
func main() {
dispatcher := manager.NewDispatcher(3)
dispatcher.Start()
router := gin.Default()
router.POST("/submit", func (c *gin.Context){
var payload struct {
Message string `json:"message"`
}
if err := c.ShouldBindJSON(&payload); err != nil{
c.JSON(http.StatusBadRequest, gin.H{"error":"Invalid Payload"})
return
}
task := manager.Task{
ID: int(time.Now().UnixNano()),
Payload: payload.Message,
SubmittedAt: time.Now(),
}
dispatcher.TaskQueue.Enqueue(task)
c.JSON(http.StatusCreated, gin.H{"message":fmt.Sprintf("Task %d submitted", task.ID)})
})
fmt.Println("Server is running...")
router.Run(":8080")
}