/
daemon.go
58 lines (52 loc) · 1.4 KB
/
daemon.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
/*
This file handles daemon and services related tasks.
By using cvpm daemon install, it will install a system service under current user.
You can uninstall that service by using cvpm daemon uninstall
*/
package main
import (
"github.com/fatih/color"
"github.com/gin-gonic/gin"
"net/http"
)
// Default Running Port
const DaemonPort = "10590"
// Definition of Running Repos
var RunningRepos []Repository
// Struct of a Request to Run Repo
type RunRepoRequest struct {
Name string `json:name`
Vendor string `json:vendor`
Solver string `json:solver`
Port string `json:port`
}
// Handle Post Request -> Run a Repo
func PostRepoHandler(c *gin.Context) {
var runRepoRequest RunRepoRequest
c.BindJSON(&runRepoRequest)
go runRepo(runRepoRequest.Vendor, runRepoRequest.Name, runRepoRequest.Solver, runRepoRequest.Port)
c.JSON(http.StatusOK, gin.H{
"code": "success",
})
}
// Handle Get Request -> Get Running Repo
func GetReposHandler(c *gin.Context) {
c.JSON(http.StatusOK, RunningRepos)
}
/* Run the Server and Do Mount Endpoint
/status -> Get to fetch System Status
/repo -> Post to Run a Repo
/repos -> Get to fetch Running Repos
*/
func runServer(port string) {
color.Red("Initiating")
r := gin.Default()
r.GET("/status", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"daemon": "running",
})
})
r.POST("/repo", PostRepoHandler)
r.GET("/repos", GetReposHandler)
r.Run("127.0.0.1:" + port)
}