-
Notifications
You must be signed in to change notification settings - Fork 2
/
static_server.go
53 lines (44 loc) · 1.05 KB
/
static_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
package main
import (
"fmt"
"net/http"
"strings"
)
func Index(w http.ResponseWriter, r *http.Request) {
param := r.URL.Path[1:]
if strings.HasSuffix(param, "/") {
http.Redirect(w, r, "/"+strings.TrimSuffix(param, "/"), http.StatusPermanentRedirect)
return
}
if param == "" {
param = "index"
}
data, err := HTML.ReadFile(fmt.Sprintf("%s/%s.html", HTMLPath, param))
if err == nil {
w.Header().Set("Content-Type", "text/html")
w.Write(data)
return
}
data, err = HTML.ReadFile(fmt.Sprintf("%s/%s/index.html", HTMLPath, param))
if err == nil {
w.Header().Set("Content-Type", "text/html")
w.Write(data)
return
}
data, err = CONFIGS.ReadFile(fmt.Sprintf("%s/%s", ConfigPath, param))
if err == nil {
w.Write(data)
return
}
serveErrorPage(w)
}
func serveErrorPage(w http.ResponseWriter) {
data, err := CONFIGS.ReadFile("CONFIGS/err.html")
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusNotFound)
w.Write(data)
}