This repository has been archived by the owner on Aug 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (71 loc) · 1.74 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// This file contains main function
// and runs http server with gin
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"strconv"
)
func main() {
//defer profile.Start(profile.CPUProfile).Stop()
// Parsing config
var appConfig Config
err := appConfig.Load("config.json")
if err != nil {
panic(err)
}
// Init arr
var resources Resources
resources.Init(appConfig)
// Starting gin gonic
server := gin.Default()
server.GET("/allocate/:name", func(c *gin.Context) {
httpStatus := 200
var httpMsg string
id, err := resources.tryAllocate(c.Param("name"), appConfig.Workers)
if err != nil {
httpMsg = "Out of resources.\n"
httpStatus = 503
}
httpMsg = "r" + strconv.Itoa(id) + "\n"
c.String(httpStatus, httpMsg)
})
server.GET("/deallocate/r:id", func(c *gin.Context) {
var httpStatus int
var httpMsg string
id, err := strconv.Atoi(c.Param("id"))
if err != nil && id <= len(resources.input) {
httpMsg = "Not allocated\n"
httpStatus = 404
} else {
err := resources.tryDeallocate(id, appConfig.Workers)
if err == nil {
httpMsg = ""
httpStatus = 204
}
}
c.String(httpStatus, httpMsg)
})
server.GET("/list", func(c *gin.Context) {
httpStatus := 200
httpMsg := resources.List()
c.String(httpStatus, httpMsg)
})
server.GET("/list/:name", func(c *gin.Context) {
httpStatus := 200
httpMsg := resources.Search(c.Param("name"))
c.String(httpStatus, httpMsg)
})
server.GET("/reset", func(c *gin.Context) {
httpStatus := 204
httpMsg := ""
resources.Reset(appConfig.Workers)
c.String(httpStatus, httpMsg)
})
server.NoRoute(func(c *gin.Context) {
httpStatus := 400
httpMsg := "Bad request.\n"
c.String(httpStatus, httpMsg)
})
server.Run(fmt.Sprintf(":%d", appConfig.Port))
}