forked from sameerdhoot/wolweb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
72 lines (55 loc) · 1.72 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
package main
import (
"log"
"net/http"
"os"
"path/filepath"
"time"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
// Global variables
var appConfig AppConfig
var appData AppData
func main() {
setWorkingDir()
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 setupWebServer() {
// Init HTTP Router - mux
router := mux.NewRouter()
// map directory to server static files
router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
// Define Home Route
router.HandleFunc("/", renderHomePage).Methods("GET")
// Define Wakeup functions with a Device Name
router.HandleFunc("/wake/{deviceName}", wakeUpWithDeviceName).Methods("GET")
router.HandleFunc("/wake/{deviceName}/", wakeUpWithDeviceName).Methods("GET")
// Define Data save Api function
router.HandleFunc("/data/save", saveData).Methods("POST")
// Define Data get Api function
router.HandleFunc("/data/get", getData).Methods("GET")
// Define health check function
router.HandleFunc("/health", checkHealth).Methods("GET")
// Setup Webserver
httpListen := ":8089"
log.Printf("Startup Webserver on \"%s\"", httpListen)
srv := &http.Server{
Handler: 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())
}