-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
101 lines (87 loc) · 1.94 KB
/
server.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
package main
import (
"context"
"net/http"
"time"
"github.com/gin-gonic/gin"
rt "github.com/wailsapp/wails/v2/pkg/runtime"
)
type DataType struct {
Days string `json:"days"`
Age string `json:"age"`
Text string `json:"text"`
IP string `json:"ip"`
}
type VariableType struct {
Name string `json:"name" binding:"required"`
Value DataType `json:"value" bindings:"required"`
}
func RunServer(a *App, ctx context.Context) {
//
// This will have the web server backend for BulletinBoard.
//
r := gin.Default()
r.Use(gin.Recovery())
//
// Define the message route. The message is given on the URI string and in the body.
//
r.GET("/api/getvar/:variable", func(c *gin.Context) {
variable := c.Param("variable")
//
// Send the request
//
rt.EventsEmit(ctx, "getvariable", variable)
//
// Get the return.
//
running := true
rt.EventsOn(ctx, "returnvariable", func(optionalData ...interface{}) {
c.JSON(http.StatusOK, optionalData)
running = false
rt.EventsOff(ctx, "returnvariable")
})
for running {
time.Sleep(time.Millisecond)
}
})
//
// Add route for listing the variables.
//
r.GET("/api/getvar/list", func(c *gin.Context) {
//
// Send the request
//
rt.EventsEmit(ctx, "listvariables", nil)
//
// Get the return.
//
running := true
rt.EventsOn(ctx, "returnvariablelist", func(optionalData ...interface{}) {
c.JSON(http.StatusOK, optionalData)
running = false
rt.EventsOff(ctx, "returnvariablelist")
})
for running {
time.Sleep(time.Millisecond)
}
})
//
// Add the route for setting a variable value.
//
r.PUT("/api/setvar/:variable", func(c *gin.Context) {
var json VariableType
if err := c.ShouldBindJSON(&json); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
//
// Send it to the frontend.
//
rt.EventsEmit(ctx, "setvariable", json)
c.JSON(http.StatusOK, "okay")
})
//
// Run the server.
//
r.Run(":9696")
}