-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathmain.go
150 lines (116 loc) · 3.57 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 (
"flag"
"embed"
"io/fs"
"log"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
"time"
"github.com/NYTimes/gziphandler"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/ilyakaznacheev/cleanenv"
)
// Global variables
var appConfig AppConfig
var appData AppData
var args Args
type Args struct {
ConfigPath string
DevicesPath string
}
//go:embed static
var staticFiles embed.FS
func main() {
processArgs()
setWorkingDir()
loadConfig()
loadData()
setupWebServer()
}
func setWorkingDir() {
thisApp, err := os.Executable()
if err != nil {
log.Fatalf("Error determining the directory. \"%s\"", err)
}
appPath := filepath.Dir(thisApp)
os.Chdir(appPath)
log.Printf("Set working directory: %s", appPath)
}
func loadConfig() {
err := cleanenv.ReadConfig(args.ConfigPath, &appConfig)
if err != nil {
log.Fatalf("Error loading config.json file. \"%s\"", err)
}
log.Printf("Application configuratrion loaded from config.json")
}
func setupWebServer() {
// Init HTTP Router - mux
router := mux.NewRouter()
// Define base path. Keep it empty when VDir is just "/" to avoid redirect loops
// Add trailing slash if basePath is not empty
basePath := ""
if appConfig.VDir != "/" {
basePath = appConfig.VDir
router.HandleFunc(basePath, redirectToHomePage).Methods("GET")
}
// map directory to server static files
var staticFS = fs.FS(staticFiles)
staticFS, err := fs.Sub(staticFS, "static")
if err != nil {
panic(err)
}
router.PathPrefix(basePath + "/static/").Handler(http.StripPrefix(basePath+"/static/", CacheControlWrapper(http.FileServer(http.FS(staticFS)))))
// Define Home Route
router.HandleFunc(basePath+"/", renderHomePage).Methods("GET")
// Define Wakeup functions with a Device Name
router.HandleFunc(basePath+"/wake/{deviceName}", wakeUpWithDeviceName).Methods("GET")
router.HandleFunc(basePath+"/wake/{deviceName}/", wakeUpWithDeviceName).Methods("GET")
if appConfig.ReadOnly == false {
// Define Data save Api function
router.HandleFunc(basePath+"/data/save", saveData).Methods("POST")
}
// Define Data get Api function
router.HandleFunc(basePath+"/data/get", getData).Methods("GET")
// Define health check function
router.HandleFunc(basePath+"/health", checkHealth).Methods("GET")
// Setup Webserver
httpListen := net.ParseIP(appConfig.Host).String() + ":" + strconv.Itoa(appConfig.Port)
log.Printf("Startup Webserver on \"%s\"", httpListen)
srv := &http.Server{
Handler: gziphandler.GzipHandler(handlers.RecoveryHandler(handlers.PrintRecoveryStack(true))(router)),
Addr: httpListen,
// Good practice: enforce timeouts for servers you create!
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}
func CacheControlWrapper(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "max-age=31536000")
h.ServeHTTP(w, r)
})
}
func processArgs() {
var configPath string
var devicesPath string
f := flag.NewFlagSet("wolweb", 1)
f.StringVar(&configPath, "c", "config.json", "Path to configuration file")
f.StringVar(&devicesPath, "d", "devices.json", "Path to devices file")
f.Parse(os.Args[1:])
configPath, err := filepath.Abs(configPath)
if err != nil {
log.Fatal(err)
}
devicesPath, err = filepath.Abs(devicesPath)
if err != nil {
log.Fatal(err)
}
args.ConfigPath = configPath
args.DevicesPath = devicesPath
}