Merge pull request #91 from george-e-shaw-iv/master

moved all servehttp functions into their own file
This commit is contained in:
George Shaw 2017-11-28 16:52:47 -06:00 committed by GitHub
commit 625fa87c2c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 186 additions and 164 deletions

View file

@ -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
}
}

View file

@ -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
}
}

View file

@ -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
}
}

59
pkg/gpserver/servehttp.go Normal file
View file

@ -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
}
}

View file

@ -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")
}

66
pkg/public/servehttp.go Normal file
View file

@ -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")
}