forked from MagicPhoenix/EIS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
150 lines (121 loc) · 3.09 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"EIP/model"
"EIP/service"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/websocket"
"log"
"strconv"
"strings"
"time"
)
var unixSocket string
var klipperPath string
var messageChan = make(chan string)
var conns []*websocket.Conn
func main() {
getConfig()
ws := websocket.New(websocket.DefaultGorillaUpgrader, websocket.Events{
websocket.OnNativeMessage: func(nsConn *websocket.NSConn, msg websocket.Message) error {
return nil
},
})
ws.OnConnect = func(c *websocket.Conn) error {
conns = append(conns, c)
for {
message := <-messageChan
mg := websocket.Message{
Body: []byte(message),
IsNative: true,
}
for _, conn := range conns {
go conn.Write(mg)
}
}
return nil
}
app := iris.New()
tmpl := iris.HTML("./views", ".html")
// Set custom delimeters.
tmpl.Delims("{{", "}}")
//tmpl.Reload(true)
app.RegisterView(tmpl)
app.Get("/", home)
app.Get("/report/{ctime:int64}", report)
app.Get("/ws", websocket.Handler(ws))
app.Post("/config/socket", socketConfig)
app.Post("/config/path", pathConfig)
app.Post("/report/rename", reName)
app.Post("/report/del", del)
app.Post("/newCalibrate", calibrate)
app.HandleDir("/static", iris.Dir("./static"))
app.Listen(":8080")
}
func home(ctx iris.Context) {
records := model.GetRecords()
if len(records) > 0 {
ctx.ViewData("records", records)
ctx.ViewData("cTime", records[len(records)-1].Time)
}
if err := ctx.View("index.html"); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
}
}
func report(ctx iris.Context) {
cTime, err := ctx.Params().GetInt64("ctime")
if err != nil {
ctx.StatusCode(500)
}
// query
records := model.GetRecords()
new := true
for i := range records {
if records[i].Time == cTime {
new = false
break
}
}
ctx.ViewData("records", records)
ctx.ViewData("cTime", cTime)
ctx.ViewData("cTimeStr", time.Unix(cTime, 0).Format("2006-01-02 15:04"))
ctx.ViewData("new", new)
if err := ctx.View("index.html"); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
}
}
func socketConfig(ctx iris.Context) {
unixSocket = ctx.PostValue("input")
log.Println(unixSocket)
model.UpdateSocket(unixSocket)
ctx.JSON(iris.Map{"message": "OK"})
}
func pathConfig(ctx iris.Context) {
klipperPath = ctx.PostValue("input")
klipperPath = strings.TrimRight(klipperPath, "/")
if !strings.HasSuffix(klipperPath, "/") {
klipperPath += "/"
}
log.Println(klipperPath)
model.UpdatePath(klipperPath)
ctx.JSON(iris.Map{"message": "OK"})
}
func reName(ctx iris.Context) {
newName := ctx.PostValue("name")
cTime, _ := ctx.PostValueInt64("ctime")
model.UpdateName(newName, cTime)
//ctx.Redirect("/record/" + strconv.FormatInt(cTime, 10))
ctx.JSON(iris.Map{"message": "OK"})
}
func del(ctx iris.Context) {
cTime, _ := ctx.PostValueInt64("ctime")
model.DelRecord(cTime)
ctx.Redirect("/")
}
func calibrate(ctx iris.Context) {
cTime := time.Now().Unix()
service.NewCalibrate(unixSocket, klipperPath, messageChan, cTime)
ctx.Redirect("/report/"+strconv.FormatInt(cTime, 10), iris.StatusFound)
}
func getConfig() {
unixSocket, klipperPath = model.GetConfig()
}