-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
188 lines (145 loc) · 3.74 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"strings"
)
func main() {
fmt.Println("")
defer fmt.Println()
c, e := ReadConfig()
if e!=nil {
log.Print(e)
return
}
InitializeDatabase(c.GetDatabaseFileLocation())
//MigrateMongoToSqlLite()
http.HandleFunc("/", handler)
http.HandleFunc("/view/", viewHandler)
http.HandleFunc("/json/", jsonHandler)
http.HandleFunc("/js/", jsHandler)
http.HandleFunc("/css/", cssHandler)
http.HandleFunc("/directives/", directivesHandler)
httpPort := fmt.Sprintf(":%d", c.HttpPort)
log.Printf("Waiting for connections on port %s", httpPort)
e = http.ListenAndServe(httpPort, nil)
if e!=nil {
log.Fatal(e)
}
}
func handler(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/view", http.StatusFound)
}
func viewHandler(w http.ResponseWriter, r *http.Request) {
var routes = map[string]string {
"/view/": "index.html",
"/view/addgame": "addgame.html",
"/view/allgames": "allgames.html",
"/view/editgame": "editgame.html",
}
htmlFilePath, ok := routes[r.URL.Path]
if ok {
fileContent, err := ioutil.ReadFile(fmt.Sprintf("views/%s", htmlFilePath))
if err!=nil {
log.Print(err)
http.Error(w, "", 500)
return
}
fmt.Fprintf(w, "%s", fileContent)
return
}
log.Print(r.URL.Path)
http.NotFound(w, r)
}
func jsonHandler(w http.ResponseWriter, r *http.Request) {
requestBody := getRequestBody(r)
urlToLower := strings.ToLower(r.URL.Path)
if strings.HasPrefix(urlToLower, "/json/getgame/") {
urlSplit := strings.Split(urlToLower, "/")
gameId := urlSplit[len(urlSplit)-1]
responseString, err := GetGameById(gameId)
if err!=nil {
http.Error(w, fmt.Sprintf("%s", err), 500)
return
}
fmt.Fprintf(w, responseString)
return
}
var dispatchFunc func([]byte) (string, error)
switch urlToLower {
case "/json/savegame":
dispatchFunc = SaveGameFromJson
case "/json/deletegame":
dispatchFunc = DeleteGame
case "/json/getplatforms":
dispatchFunc = GetAllPlatforms
case "/json/getgenres":
dispatchFunc = GetAllGenres
case "/json/gethardwaretypes":
dispatchFunc = GetAllHardwareTypes
case "/json/getgames":
dispatchFunc = GetAllGames
default:
http.NotFound(w, r)
return
}
responseString, err := dispatchFunc(requestBody)
if err!=nil {
http.Error(w, fmt.Sprintf("%s", err), 500)
return
}
fmt.Fprint(w, responseString)
}
func getRequestBody(r *http.Request) []byte {
requestBody := make([]byte, r.ContentLength)
_, err := r.Body.Read(requestBody)
if err!=nil && err!=io.EOF {
log.Print(err)
return requestBody
}
return requestBody
}
func jsHandler(w http.ResponseWriter, r *http.Request) {
rootDir := http.Dir("js/")
assetHandler(rootDir, w, r)
}
func cssHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/css")
rootDir := http.Dir("css/")
assetHandler(rootDir, w, r)
}
func directivesHandler(w http.ResponseWriter, r *http.Request) {
rootDir := http.Dir("directives/")
assetHandler(rootDir, w, r)
}
func assetHandler(rootDir http.Dir, w http.ResponseWriter, r *http.Request) {
filePath := strings.TrimPrefix(r.URL.Path, fmt.Sprintf("/%s", rootDir))
fileToServe, err := rootDir.Open(filePath)
if err!=nil {
log.Print(err)
http.NotFound(w, r)
return
}
defer fileToServe.Close()
var fileContent []byte
for {
buffer := make([]byte, 2048)
_, err = fileToServe.Read(buffer)
if err==io.EOF {
break
}
if err!=nil {
log.Fatal(err)
}
for _, b := range(buffer) {
if b==0 {
break
}
fileContent = append(fileContent, b)
}
}
fmt.Fprintf(w, "%s", fileContent)
}