-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
70 lines (53 loc) · 1.3 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
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"os"
"github.com/gorilla/mux"
"github.com/livingston/cod4-server-dashboard/parser"
"github.com/spf13/viper"
)
type dashboard struct {
Title string
Game map[string]string
Server parser.Server
}
func loadConfig() {
viper.SetConfigName("config")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
panic(err)
}
}
func handler(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("layout.html"))
game, server, err := parser.Parse(viper.GetString("gameserver.server_location") + "serverstatus.xml")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
data := dashboard{
Title: "Dashboard",
Game: game,
Server: server,
}
tmpl.Execute(w, data)
}
func newRouter() *mux.Router {
r := mux.NewRouter()
r.HandleFunc("/", handler).Methods("GET")
staticFileDirectory := http.Dir("./static/")
staticFileHandler := http.StripPrefix("/assets/", http.FileServer(staticFileDirectory))
r.PathPrefix("/assets/").Handler(staticFileHandler).Methods("GET")
return r
}
func main() {
loadConfig()
appAddress := viper.GetString("app.ip") + ":" + viper.GetString("app.port")
r := newRouter()
if err := http.ListenAndServe(appAddress, r); err != nil {
log.Fatal("ListenAndServe:", err)
}
}