forked from kentonh/gPanel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpaccount.go
107 lines (89 loc) · 2.9 KB
/
gpaccount.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
// Package gpaccount handles the logic of the gPanel account server
package gpaccount
import (
"fmt"
"io"
"net/http"
"os"
"strconv"
"time"
"github.com/Ennovar/gPanel/pkg/file"
"github.com/Ennovar/gPanel/pkg/public"
"github.com/Ennovar/gPanel/pkg/routing"
)
type Controller struct {
Directory string
DocumentRoot string
Port int
Public *public.Controller
GracefulShutdownTimeout time.Duration
Status int
AccountLogger *file.Handler
}
var controller Controller
var httpserver http.Server
// New returns a new Controller reference.
func New(dir string, accPort int, pubPort int) *Controller {
accountErrorLogger, err := file.Open(dir+"logs/"+file.LOG_ACCOUNT_ERRORS, true)
if err != nil {
fmt.Errorf("Error whilst trying to start account logging instance: %v\n", err.Error())
}
controller = Controller{
Directory: dir,
DocumentRoot: "account/",
Port: accPort,
Public: public.New(dir, pubPort),
GracefulShutdownTimeout: 5 * time.Second,
Status: 0,
AccountLogger: accountErrorLogger,
}
httpserver = http.Server{
Addr: "localhost:" + strconv.Itoa(controller.Port),
Handler: &controller,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
MaxHeaderBytes: 0,
}
return &controller
}
// ServeHTTP function routes all requests for the private webhost server. It is used in the main
// function inside of the http.ListenAndServe() function for the private webhost host.
func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) {
path := req.URL.Path[1:]
if len(path) == 0 {
path = (con.DocumentRoot + "index.html")
} else {
path = (con.DocumentRoot + path)
}
if reqAuth(path) {
if !con.checkAuth(res, req) {
con.AccountLogger.Write(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.AccountLogger.Write(path + "::" + strconv.Itoa(http.StatusNotFound) + "::" + err.Error())
routing.HttpThrowStatus(http.StatusNotFound, res)
return
}
contentType, err := routing.GetContentType(path)
if err != nil {
con.AccountLogger.Write(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.AccountLogger.Write(path + "::" + strconv.Itoa(http.StatusInternalServerError) + "::" + err.Error())
routing.HttpThrowStatus(http.StatusInternalServerError, res)
return
}
}