/
daemon.go
127 lines (114 loc) · 3.3 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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
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"
"github.com/googollee/go-socket.io"
"github.com/hpcloud/tail"
"log"
"net/http"
"path/filepath"
)
// Default Running Port
const DaemonPort = "10590"
// Definition of Running Repos
var RunningRepos []Repository
var socketServer *socketio.Server
// 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 GetRunningReposHandler(c *gin.Context) {
c.JSON(http.StatusOK, RunningRepos)
}
// Handle Get Request -> Get All Repos
func GetReposHandler(c *gin.Context) {
config := readConfig()
c.JSON(http.StatusOK, config.Repositories)
}
// Handle Get Repository Meta Info
func GetRepoMetaHandler(c *gin.Context) {
vendor := c.Param("vendor")
name := c.Param("name")
c.JSON(http.StatusOK, GetMetaInfo(vendor, name))
}
// Handle Socket Request
func socketHandler(c *gin.Context) {
socketServer.On("connection", func(so socketio.Socket) {
so.Join("cvpm-webtail")
})
socketServer.ServeHTTP(c.Writer, c.Request)
}
// write log to socket stream
func writeLog(filepath string, server *socketio.Server, eventName string) {
log.Println("Writing Logs")
t, err := tail.TailFile(filepath, tail.Config{Follow: true})
if err != nil {
panic(err)
}
for line := range t.Lines {
server.BroadcastTo("cvpm-webtail", eventName, line.Text)
}
}
// watched log source
func watchLogs(server *socketio.Server) {
// System Log
cvpmLogsLocation := getLogsLocation()
go writeLog(filepath.Join(cvpmLogsLocation, "system.log"), server, "system")
go writeLog(filepath.Join(cvpmLogsLocation, "package.log"), server, "package")
}
// global header
func BeforeResponse() gin.HandlerFunc {
return func(c *gin.Context) {
origin := c.GetHeader("Origin")
c.Writer.Header().Set("cvpm-version", "0.0.3@alpha")
c.Writer.Header().Set("Access-Control-Allow-Origin", origin)
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
}
}
/* 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")
var err error
socketServer, err = socketio.NewServer(nil)
if err != nil {
panic(err)
}
r := gin.Default()
r.Use(BeforeResponse())
watchLogs(socketServer)
r.GET("/status", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"daemon": "running",
})
})
r.GET("/repo/meta/:vendor/:name", GetRepoMetaHandler)
r.POST("/repo", PostRepoHandler)
r.GET("/repos", GetReposHandler)
r.GET("/socket.io/", socketHandler)
r.POST("/socket.io/", socketHandler)
r.Handle("WS", "/socket.io/", socketHandler)
r.Handle("WSS", "/socket.io/", socketHandler)
r.Run("0.0.0.0:" + port)
}