From 638b3cd2443e13feb68c90b87a2039bb16ca9a27 Mon Sep 17 00:00:00 2001 From: George Shaw Date: Tue, 28 Nov 2017 16:52:13 -0600 Subject: [PATCH] moved all servehttp functions into their own file --- pkg/gpaccount/gpaccount.go | 53 ------------------------------ pkg/gpaccount/servehttp.go | 61 +++++++++++++++++++++++++++++++++++ pkg/gpserver/gpserver.go | 53 ------------------------------ pkg/gpserver/servehttp.go | 59 ++++++++++++++++++++++++++++++++++ pkg/public/public.go | 58 --------------------------------- pkg/public/servehttp.go | 66 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 186 insertions(+), 164 deletions(-) create mode 100644 pkg/gpaccount/servehttp.go create mode 100644 pkg/gpserver/servehttp.go create mode 100644 pkg/public/servehttp.go diff --git a/pkg/gpaccount/gpaccount.go b/pkg/gpaccount/gpaccount.go index 7d2f445..bde0078 100644 --- a/pkg/gpaccount/gpaccount.go +++ b/pkg/gpaccount/gpaccount.go @@ -3,7 +3,6 @@ package gpaccount import ( "fmt" - "io" "log" "net/http" "os" @@ -11,7 +10,6 @@ import ( "time" "github.com/Ennovar/gPanel/pkg/public" - "github.com/Ennovar/gPanel/pkg/routing" ) type Controller struct { @@ -59,54 +57,3 @@ func New(dir string, accPort int, pubPort int) *Controller { 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.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 - } -} diff --git a/pkg/gpaccount/servehttp.go b/pkg/gpaccount/servehttp.go new file mode 100644 index 0000000..09b7419 --- /dev/null +++ b/pkg/gpaccount/servehttp.go @@ -0,0 +1,61 @@ +package gpaccount + +import ( + "io" + "net/http" + "os" + "strconv" + + "github.com/Ennovar/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 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 + } +} diff --git a/pkg/gpserver/gpserver.go b/pkg/gpserver/gpserver.go index 75319b2..4cdb579 100644 --- a/pkg/gpserver/gpserver.go +++ b/pkg/gpserver/gpserver.go @@ -3,17 +3,13 @@ package gpserver import ( "fmt" - "io" "io/ioutil" "log" - "net/http" "os" - "strconv" "strings" "github.com/Ennovar/gPanel/pkg/api/bundle" "github.com/Ennovar/gPanel/pkg/gpaccount" - "github.com/Ennovar/gPanel/pkg/routing" ) type Controller struct { @@ -68,52 +64,3 @@ func New() *Controller { APILogger: apiLogger, } } - -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 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 - } -} diff --git a/pkg/gpserver/servehttp.go b/pkg/gpserver/servehttp.go new file mode 100644 index 0000000..b6580b8 --- /dev/null +++ b/pkg/gpserver/servehttp.go @@ -0,0 +1,59 @@ +package gpserver + +import ( + "io" + "net/http" + "os" + "strconv" + + "github.com/Ennovar/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 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 + } +} diff --git a/pkg/public/public.go b/pkg/public/public.go index 69b6e92..c3ee523 100644 --- a/pkg/public/public.go +++ b/pkg/public/public.go @@ -3,14 +3,11 @@ package public import ( "fmt" - "io" "log" "net/http" "os" "strconv" "time" - - "github.com/Ennovar/gPanel/pkg/routing" ) type Controller struct { @@ -59,58 +56,3 @@ func New(dir string, port int) *Controller { return &controller } - -// ServeHTTP function routes all requests for the public web server. It is used in the main -// function inside of the http.ListenAndServe() function for the public host. -func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) { - startTime := time.Now() - - switch con.Status { - case 0: // This will actually never show because this function won't run if the server is off - http.Error(res, "The server is currently down and not serving requests.", http.StatusServiceUnavailable) - return - case 1: // Normal - break - case 2: // Maintenance mode - http.Error(res, "The server is currently maintenance mode and not serving requests.", http.StatusServiceUnavailable) - return - case 3: // This will actually never show because this function won't run if the server is off - http.Error(res, "The server is currently restarting.", http.StatusServiceUnavailable) - return - } - - path := req.URL.Path[1:] - if len(path) == 0 { - path = (con.DocumentRoot + "index.html") - } else { - path = (con.DocumentRoot + path) - } - - f, err := os.Open(path) - - if err != nil { - con.PublicLogger.Println(path + "::" + strconv.Itoa(http.StatusNotFound) + "::" + err.Error()) - routing.HttpThrowStatus(http.StatusNotFound, res) - return - } - - contentType, err := routing.GetContentType(path) - - if err != nil { - con.PublicLogger.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.PublicLogger.Println(path + "::" + strconv.Itoa(http.StatusInternalServerError) + "::" + err.Error()) - routing.HttpThrowStatus(http.StatusInternalServerError, res) - return - } - - elapsedTime := time.Since(startTime) - con.LoadTimeLogger.Println(path + " rendered in " + strconv.FormatFloat(elapsedTime.Seconds(), 'f', 6, 64) + " seconds") -} diff --git a/pkg/public/servehttp.go b/pkg/public/servehttp.go new file mode 100644 index 0000000..0e24b05 --- /dev/null +++ b/pkg/public/servehttp.go @@ -0,0 +1,66 @@ +package public + +import ( + "io" + "net/http" + "os" + "strconv" + "time" + + "github.com/Ennovar/gPanel/pkg/routing" +) + +// ServeHTTP function routes all requests for the public web server. It is used in the main +// function inside of the http.ListenAndServe() function for the public host. +func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) { + startTime := time.Now() + + switch con.Status { + case 0: // This will actually never show because this function won't run if the server is off + http.Error(res, "The server is currently down and not serving requests.", http.StatusServiceUnavailable) + return + case 1: // Normal + break + case 2: // Maintenance mode + http.Error(res, "The server is currently maintenance mode and not serving requests.", http.StatusServiceUnavailable) + return + case 3: // This will actually never show because this function won't run if the server is off + http.Error(res, "The server is currently restarting.", http.StatusServiceUnavailable) + return + } + + path := req.URL.Path[1:] + if len(path) == 0 { + path = (con.DocumentRoot + "index.html") + } else { + path = (con.DocumentRoot + path) + } + + f, err := os.Open(path) + + if err != nil { + con.PublicLogger.Println(path + "::" + strconv.Itoa(http.StatusNotFound) + "::" + err.Error()) + routing.HttpThrowStatus(http.StatusNotFound, res) + return + } + + contentType, err := routing.GetContentType(path) + + if err != nil { + con.PublicLogger.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.PublicLogger.Println(path + "::" + strconv.Itoa(http.StatusInternalServerError) + "::" + err.Error()) + routing.HttpThrowStatus(http.StatusInternalServerError, res) + return + } + + elapsedTime := time.Since(startTime) + con.LoadTimeLogger.Println(path + " rendered in " + strconv.FormatFloat(elapsedTime.Seconds(), 'f', 6, 64) + " seconds") +}