-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
114 lines (95 loc) · 2.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
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright (C) 2017 Next Thing Co. <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>
package main
import (
"context"
"errors"
"github.com/gin-gonic/gin"
"net/http"
"time"
)
var serverInitialized = false
var serverRunning = false
var router *gin.Engine
var server *http.Server
var credentialsChannel chan Credentials
var viewLocation = "./view/*"
var staticLocation = "./static"
var defaultPort = ":8080"
func initializeServer() error {
if serverInitialized {
return errors.New("Server already initialized")
}
credentialsChannel = make(chan Credentials)
// Set to production mode if not debugging
if !debugMode {
gin.SetMode(gin.ReleaseMode)
}
// Create router with default middleware (log and recovery)
router = gin.Default()
// Serve static files
router.Static("/static", staticLocation)
router.StaticFile("/hotspot.html", "./static/hotspot.html")
// Load view templates and init routes
router.LoadHTMLGlob(viewLocation)
// Initialize router routes
initRoutes()
server = &http.Server{
Addr: defaultPort,
Handler: router,
}
serverInitialized = true
return nil
}
func startServer() (Credentials, error) {
if !serverInitialized {
initializeServer()
}
if serverRunning {
return Credentials{}, errors.New("Server already running")
}
serverRunning = true
go func() {
Debug.Println("startServer: listening on", server.Addr)
if err := server.ListenAndServe(); err != nil {
Debug.Println("startServer:", err)
}
Debug.Println("startServer thread completed")
}()
Debug.Println("startServer: listening on credentials channel")
// Wait until someone pushes data to the credentialsChannel
credentials = <-credentialsChannel
return credentials, nil
}
func stopServer() {
// Give the server 5 seconds to shutdown, then kill it
Debug.Println("Received prompt to shut down server!")
Info.Println("Shutting down server in 5 seconds")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
Error.Println("Server Shutdown:", err)
}
serverRunning = false
Debug.Println("Server down")
}
func render(context *gin.Context, data gin.H, templateName string) {
switch context.Request.Header.Get("Accept") {
case "application/json":
context.JSON(http.StatusOK, data["pipeline"])
default:
context.HTML(http.StatusOK, templateName, data)
}
}