-
Notifications
You must be signed in to change notification settings - Fork 0
/
tdserver.go
57 lines (44 loc) · 1.33 KB
/
tdserver.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
package main
import (
"fmt"
"net/http"
"io/ioutil"
)
func main(){
http.HandleFunc("/", serveIndex)
http.HandleFunc("/api", serveApi)
http.ListenAndServe(":8080", nil)
}
func serveIndex(w http.ResponseWriter, r *http.Request){
// Determine if we are fetching a specific file or just requesting index
var data []byte
switch {
case r.URL.Path[1:] == "":
data, _ = ioutil.ReadFile("index.html")
w.Header().Set("Content-Type", "text/html")
case r.URL.Path[1:] == "jquery.js":
data, _ = ioutil.ReadFile("jquery.js")
w.Header().Set("Content-Type", "application/javascript")
case r.URL.Path[1:] == "jquery.min.js":
data, _ = ioutil.ReadFile("jquery.min.js")
w.Header().Set("Content-Type", "application/javascript")
case r.URL.Path[1:] == "game.js":
data, _ = ioutil.ReadFile("game.js")
w.Header().Set("Content-Type", "application/javascript")
case r.URL.Path[1:] == "astar.js":
data, _ = ioutil.ReadFile("astar.js")
w.Header().Set("Content-Type", "application/javascript")
case r.URL.Path[1:] == "favicon.ico":
return
}
//fmt.Println(r.URL.Path[1:]) // Log the requested file
w.Write(data)
}
func serveApi(w http.ResponseWriter, r *http.Request){
r.ParseForm()
postVars := r.Form
switch {
case postVars.Get("action") == "log":
fmt.Println("API logged: " + postVars.Get("message"))
}
}