gPanel/pkg/gpaccount/servehttp.go
George Shaw b9f7c5d065 Fix import paths
* github.com/Ennovar/gPanel -> github.com/kentonh/gPanel
2018-09-21 20:13:49 -05:00

69 lines
1.8 KiB
Go

package gpaccount
import (
"io"
"net/http"
"os"
"strconv"
"strings"
"github.com/kentonh/gPanel/pkg/routing"
)
// 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 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.AccountLogger.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.AccountLogger.Println(path + "::" + strconv.Itoa(http.StatusNotFound) + "::" + err.Error())
routing.HttpThrowStatus(http.StatusNotFound, res)
return
}
contentType, err := routing.GetContentType(path)
if err != nil {
con.AccountLogger.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.AccountLogger.Println(path + "::" + strconv.Itoa(http.StatusInternalServerError) + "::" + err.Error())
routing.HttpThrowStatus(http.StatusInternalServerError, res)
return
}
}