/
server.go
126 lines (95 loc) · 2.68 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
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
package services
import (
"apertoire.net/unbalance/server/dto"
"apertoire.net/unbalance/server/model"
"github.com/gin-gonic/contrib/static"
"github.com/gin-gonic/gin"
"github.com/jbrodriguez/mlog"
"github.com/jbrodriguez/pubsub"
"path/filepath"
)
const apiVersion string = "/api/v1"
type Server struct {
bus *pubsub.PubSub
config *model.Config
engine *gin.Engine
socket *Socket
}
func NewServer(bus *pubsub.PubSub, config *model.Config, socket *Socket) *Server {
server := &Server{bus: bus, config: config, socket: socket}
return server
}
func (s *Server) Start() {
mlog.Info("starting service Server ...")
s.engine = gin.New()
s.engine.Use(gin.Recovery())
// s.engine.Use(helper.Logging())
root1, err := filepath.Abs("./")
if err != nil {
panic(err)
}
mlog.Info("absolute for: %s", root1)
root2, err := filepath.Abs("/")
if err != nil {
panic(err)
}
mlog.Info("absolute for: %s", root2)
s.engine.Use(static.Serve("/", static.LocalFile("./", true)))
s.engine.NoRoute(static.Serve("/", static.LocalFile("./", true)))
// websocket handler
s.engine.GET("/ws", func(c *gin.Context) {
s.socket.handler(c.Writer, c.Request)
})
api := s.engine.Group(apiVersion)
{
api.GET("/config", s.getConfig)
api.PUT("/config", s.saveConfig)
api.GET("/storage", s.getStorageInfo)
api.POST("/storage/bestfit", s.calculateBestFit)
api.POST("/storage/move", s.move)
}
mlog.Info("started listening on :6237")
go s.engine.Run(":6237")
}
func (s *Server) Stop() {
mlog.Info("stopped service Server ...")
}
func (s *Server) getConfig(c *gin.Context) {
msg := &pubsub.Message{Reply: make(chan interface{})}
s.bus.Pub(msg, "cmd.getConfig")
reply := <-msg.Reply
resp := reply.(*model.Config)
c.JSON(200, &resp)
}
func (s *Server) saveConfig(c *gin.Context) {
var config model.Config
c.Bind(&config)
msg := &pubsub.Message{Payload: &config, Reply: make(chan interface{})}
s.bus.Pub(msg, "cmd.saveConfig")
reply := <-msg.Reply
resp := reply.(*model.Config)
c.JSON(200, &resp)
}
func (s *Server) getStorageInfo(c *gin.Context) {
msg := &pubsub.Message{Reply: make(chan interface{})}
s.bus.Pub(msg, "cmd.getStorageInfo")
reply := <-msg.Reply
resp := reply.(*model.Unraid)
c.JSON(200, &resp)
}
func (s *Server) calculateBestFit(c *gin.Context) {
var bestFit dto.BestFit
c.Bind(&bestFit)
msg := &pubsub.Message{Payload: &bestFit, Reply: make(chan interface{})}
s.bus.Pub(msg, "cmd.calculateBestFit")
reply := <-msg.Reply
resp := reply.(*model.Unraid)
c.JSON(200, &resp)
}
func (s *Server) move(c *gin.Context) {
msg := &pubsub.Message{Reply: make(chan interface{})}
s.bus.Pub(msg, "cmd.move")
reply := <-msg.Reply
resp := reply.([]*dto.Move)
c.JSON(200, &resp)
}