-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathserver.go
103 lines (89 loc) · 2.97 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
package main
import (
"encoding/json"
"fmt"
"github.com/cskr/pubsub"
"github.com/gorilla/websocket"
"github.com/jdrews/logstation/api/server/handlers"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/spf13/viper"
"io/fs"
"net/http"
)
var (
// define the websocket upgrader
upgrader = websocket.Upgrader{}
// default to CORS being enabled
disableCORS = false
)
// WebSocketHandler handles incoming websocket connections and serves up log lines to the client
func WebSocketHandler(c echo.Context, pubSub *pubsub.PubSub) error {
if disableCORS {
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
}
ws, err := upgrader.Upgrade(c.Response(), c.Request(), nil)
if err != nil {
return nil
}
defer func(ws *websocket.Conn) {
wsCloseErr := ws.Close()
if wsCloseErr != nil {
panic(wsCloseErr)
}
}(ws)
linesChannel := pubSub.Sub("lines")
defer pubSub.Unsub(linesChannel, "lines")
for line := range linesChannel {
jsonLine, marshalErr := json.Marshal(line)
if marshalErr != nil {
logger.Fatal(marshalErr)
}
// Write
wsErr := ws.WriteMessage(websocket.TextMessage, jsonLine)
if wsErr != nil {
logger.Warn("Lost connection to websocket client! Maybe they're gone? Closing this connection. More info: ")
logger.Warn(wsErr)
break
}
}
return nil
}
// StartWebServer configures and then starts the webserver, which does the following:
// - Serves up the React files for the client
// - Provides a REST API Server for various configurations on the client
// - Starts a WebSocket Server to pass the logfiles and loglines to the client
func StartWebServer(pubSub *pubsub.PubSub) {
// setup web server
e := echo.New()
e.HideBanner = true
e.Use(middleware.Logger())
// disable CORS on the web server if desired
disableCORS = viper.GetBool("server_settings.disablecors")
if disableCORS {
logger.Warn("Running in disabled CORS mode. This is very dangerous! Be careful!")
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"},
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept},
}))
}
c, _ := handlers.NewContainer()
// GetLogstationName - Get Logstation Name
e.GET("/settings/logstation-name", c.GetLogstationName)
// GetSettingsSyntax - Get Syntax Colors
e.GET("/settings/syntax", c.GetSettingsSyntax)
// package up the built web files and serve them to the clients
fsys, err := fs.Sub(webServerFiles, "web/dist")
if err != nil {
panic(fmt.Errorf("error loading the web files into the server. error msg: %s", err))
}
fileHandler := http.FileServer(http.FS(fsys))
e.GET("/*", echo.WrapHandler(fileHandler))
// pass message broker channel into websocket handler
wsHandlerChan := func(c echo.Context) error {
return WebSocketHandler(c, pubSub)
}
e.GET("/ws", wsHandlerChan)
// start the web server
e.Logger.Fatal(e.Start(viper.GetString("server_settings.webserveraddress") + ":" + viper.GetString("server_settings.webserverport")))
}