-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
291 lines (268 loc) · 8.79 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package main
import (
"fmt"
"os"
"strings"
"strconv"
"log"
"math/rand"
"time"
"net/http"
"path/filepath"
"html/template"
"encoding/json"
"github.com/joho/godotenv"
//"io/ioutil"
//"github.com/gorilla/websocket"
)
//template for the pages
var ex, exerr = os.Executable()
func getWorkDir() string {
workdir := filepath.Dir(ex)
//for when the app is dockerized, prevent the strings for paths become like "//views/index.html"
if workdir == "/" {
workdir = ""
}
return workdir
}
var workdir = getWorkDir()
var chat_template = template.Must(template.ParseFiles(workdir + "/views/index.html", workdir + "/views/board_template.html", workdir + "/views/footer.html", workdir + "/views/header_all.html"))
//the handler for the index page
func handleIndex(w http.ResponseWriter, r *http.Request) {
//get the stuff needed
contex := getFrontPageContext()
//render template
err := chat_template.ExecuteTemplate(w, "index.html", contex)
if err != nil { //if there is an error
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
//handler for a board's page with rendering the template but no cache
func handleBoard(w http.ResponseWriter, r *http.Request) {
//get the link extention from the url
stention := r.URL.Path[len("/board/"):]
var board string
var page int
//split to get the page number
splits := strings.Split(stention, "/")
if len(splits) == 1 {
board = splits[0]
page = 1
} else {
board = splits[0]
paj, err := strconv.Atoi(splits[1])
if err != nil {
page = 0
} else {
page = paj
}
}
//get the content of the board with the specified page number
content := getBoardContent(board, page)
if content.Posts == nil {
http.Error(w, "can't get board", http.StatusInternalServerError)
}
//execute the template
err := chat_template.ExecuteTemplate(w, "board_template.html", content)
if err != nil { //if there is an error
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
//the handler for the footer
func handleFooter(w http.ResponseWriter, r *http.Request) {
err := chat_template.ExecuteTemplate(w, "footer.html", nil)
if err != nil { //if there is an error
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
//the handler for the header
func handleHeaderAll(w http.ResponseWriter, r *http.Request) {
err := chat_template.ExecuteTemplate(w, "header_all.html", nil)
if err != nil { //if there is an error
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
//function to fetch the client's ip from the http header
func ReadUserIP(r *http.Request) string {
IPAddress := r.Header.Get("X-Real-Ip")
if IPAddress == "" {
IPAddress = r.Header.Get("X-Forwarded-For")
}
if IPAddress == "" {
IPAddress = r.RemoteAddr
}
return IPAddress
}
//the handler function for the ajax request to make a post
func handleAjaxCreatePost(w http.ResponseWriter, r *http.Request) {
var p CreatePost
//fmt.Println("got Request: ", r.Body)
//parse the json in the body text into a Post object
err := json.NewDecoder(r.Body).Decode(&p)
if err != nil {
//fmt.Println("Decoding error: ", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
//get the ip from the request header
ip := ReadUserIP(r)
//the reply object
var res CreatePostRequestResponse
//call the addPost method to add the post to db
res = addPost(p, ip)
// create json response from serializing a CreatePostReply struct
a, err := json.Marshal(res)
if err != nil {
//fmt.Println("Encoding error: ", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(a)
}
//the handler function for the ajax request to make a reply to a post
func handleAjaxCreateReply(w http.ResponseWriter, r *http.Request) {
var rp CreateReply
//fmt.Println("got Request: ", r.Body)
//parse the json in the body text into a Post object
err := json.NewDecoder(r.Body).Decode(&rp)
if err != nil {
//fmt.Println("Decoding error: ", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
//get the ip from the request header
ip := ReadUserIP(r)
//the reply object
var res CreateReplyRequestResponse
//call the addPost method to add the post to db
res = addReply(rp, ip)
// create json response from serializing a CreatePostReply struct
a, err := json.Marshal(res)
if err != nil {
//fmt.Println("Encoding error: ", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(a)
}
//for handling a show replies request
func handleAjaxShowReplies(w http.ResponseWriter, r *http.Request) {
var rp ShowRepliesRequest
//fmt.Println("got Request: ", r.Body)
//parse the json in the body text into a Post object
err := json.NewDecoder(r.Body).Decode(&rp)
if err != nil {
//fmt.Println("Decoding error: ", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
//get the ip from the request header
//ip := ReadUserIP(r)
//the reply object
var res ShowRepliesRequestResponse
//call the addPost method to add the post to db
res = showReplies(rp)
// create json response from serializing a CreatePostReply struct
a, err := json.Marshal(res)
if err != nil {
//fmt.Println("Encoding error: ", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(a)
}
//handler for index but no cache
func handleIndexNocache(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("views/index.html")
err := t.Execute(w, nil)
if err != nil { //if there is an error
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
//handler for a board's page but no cache
func handleBoardNocache(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("views/board.html")
err := t.Execute(w, nil)
if err != nil { //if there is an error
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
//handler for a board's page with rendering the template but no cache
func handleBoardTemplateNocache(w http.ResponseWriter, r *http.Request) {
//get the link extention from the url
stention := r.URL.Path[len("/nocache/board_template/"):]
var board string
var page int
//split to get the page number
splits := strings.Split(stention, "/")
if len(splits) == 1 {
board = splits[0]
page = 1
} else {
board = splits[0]
paj, err := strconv.Atoi(splits[1])
if err != nil {
page = 0
} else {
page = paj
}
}
t, _ := template.ParseFiles("views/board_template.html")
//get the content of the board with the specified page number
content := getBoardContent(board, page)
if content.Posts == nil {
http.Error(w, "can't get board", http.StatusInternalServerError)
}
//execute the template
err := t.Execute(w, content)
if err != nil { //if there is an error
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func main() {
//load the .env file
err := godotenv.Load(workdir + "/.env")
if err != nil {
log.Fatal("Error loading .env file")
}
//setup the database interface
DBAccessSetup()
//set the seed for the standard prng
rand.Seed(time.Now().UnixNano())
//print the current working directory
fmt.Println("directory of this executable: " + workdir)
//define handler functions for pages
http.HandleFunc("/", handleIndex)
http.HandleFunc("/board/", handleBoard)
http.HandleFunc("/footer", handleFooter)
http.HandleFunc("/header_all", handleHeaderAll)
//the handler for ajax requests
http.HandleFunc("/ajax/createpost/", handleAjaxCreatePost)
http.HandleFunc("/ajax/createreply/", handleAjaxCreateReply)
http.HandleFunc("/ajax/showreplies/", handleAjaxShowReplies)
/*
http.HandleFunc("/nocache/", handleIndexNocache)
http.HandleFunc("/nocache/board", handleBoardNocache)
http.HandleFunc("/nocache/board_template/", handleBoardTemplateNocache)
*/
//for handling static css and js files
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir(workdir + "/static/css"))))
http.Handle("/js/", http.StripPrefix("/js/", http.FileServer(http.Dir(workdir + "/static/js"))))
http.Handle("/fonts/", http.StripPrefix("/fonts/", http.FileServer(http.Dir(workdir + "/static/fonts"))))
http.Handle("/img/", http.StripPrefix("/img/", http.FileServer(http.Dir(workdir + "/static/images"))))
//setup the hostname env variable
hostname := os.Getenv("host_name")
if hostname == "" {
hostname = ":8880"
}
//see if there is a ssl cert to be used
ssl_cert := os.Getenv("ssl_cert")
ssl_key := os.Getenv("ssl_key")
if ssl_cert != "" && ssl_key != "" {
//start the server program with ssl
log.Fatal(http.ListenAndServeTLS(hostname, ssl_cert, ssl_key, nil))
} else {
//start the server program
log.Fatal(http.ListenAndServe(hostname, nil))
}
}