-
Notifications
You must be signed in to change notification settings - Fork 25
/
servehttp.go
67 lines (53 loc) · 1.61 KB
/
servehttp.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
package gpserver
import (
"io"
"net/http"
"os"
"strconv"
"strings"
"github.com/kentonh/gPanel/pkg/routing"
)
func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) {
path := req.URL.Path[1:]
if len(path) == 0 {
path = (con.Directory + con.DocumentRoot + "index.html")
} else {
path = (con.Directory + con.DocumentRoot + path)
}
if strings.HasSuffix(path, "index.html") {
if con.checkAuth(res, req) == true {
http.Redirect(res, req, "gPanel.html", http.StatusFound)
}
}
if reqAuth(path) {
if !con.checkAuth(res, req) {
con.ServerLogger.Println(path + "::" + strconv.Itoa(http.StatusUnauthorized) + "::" + http.StatusText(http.StatusUnauthorized))
http.Error(res, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
}
isApi, _ := con.apiHandler(res, req)
if isApi {
// API methods handle HTTP logic from here
return
}
f, err := os.Open(path)
if err != nil {
con.ServerLogger.Println(path + "::" + strconv.Itoa(http.StatusNotFound) + "::" + err.Error())
routing.HttpThrowStatus(http.StatusNotFound, res)
return
}
contentType, err := routing.GetContentType(path)
if err != nil {
con.ServerLogger.Println(path + "::" + strconv.Itoa(http.StatusUnsupportedMediaType) + "::" + err.Error())
routing.HttpThrowStatus(http.StatusUnsupportedMediaType, res)
return
}
res.Header().Add("Content-Type", contentType)
_, err = io.Copy(res, f)
if err != nil {
con.ServerLogger.Println(path + "::" + strconv.Itoa(http.StatusInternalServerError) + "::" + err.Error())
routing.HttpThrowStatus(http.StatusInternalServerError, res)
return
}
}