mirror of
https://github.com/donl/gPanel.git
synced 2026-06-30 06:12:06 -06:00
added capability to write to logs
This commit is contained in:
parent
e1d33d5b7d
commit
f5f2ff0f54
3 changed files with 41 additions and 11 deletions
|
|
@ -15,12 +15,12 @@ const (
|
|||
|
||||
var KNOWN_LOGS = [...]string{LOG_CLIENT_ERRORS, LOG_SERVER_ERRORS, LOG_LOADTIME}
|
||||
|
||||
type handler struct {
|
||||
type Handler struct {
|
||||
fileHandle *os.File
|
||||
path string
|
||||
}
|
||||
|
||||
func Open(file string, append bool, log bool) (*handler, error) {
|
||||
func Open(file string, append bool, log bool) (*Handler, error) {
|
||||
var err error
|
||||
var absPath string
|
||||
var f *os.File
|
||||
|
|
@ -50,13 +50,13 @@ func Open(file string, append bool, log bool) (*handler, error) {
|
|||
}
|
||||
|
||||
// Upon success, return the handler
|
||||
return &handler{
|
||||
return &Handler{
|
||||
fileHandle: f,
|
||||
path: absPath,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (h *handler) Read() ([]byte, error) {
|
||||
func (h *Handler) Read() ([]byte, error) {
|
||||
data, err := ioutil.ReadAll(h.fileHandle)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -65,8 +65,8 @@ func (h *handler) Read() ([]byte, error) {
|
|||
return data, err
|
||||
}
|
||||
|
||||
func (h *handler) Write(data string) (int, error) {
|
||||
written, err := h.fileHandle.Write([]byte(data))
|
||||
func (h *Handler) Write(data string) (int, error) {
|
||||
written, err := h.fileHandle.Write([]byte(data + "\n"))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
|
@ -74,7 +74,7 @@ func (h *handler) Write(data string) (int, error) {
|
|||
return written, err
|
||||
}
|
||||
|
||||
func (h *handler) Close(delete bool) (error, error) {
|
||||
func (h *Handler) Close(delete bool) (error, error) {
|
||||
closeErr := h.fileHandle.Close()
|
||||
|
||||
if delete {
|
||||
|
|
|
|||
|
|
@ -8,8 +8,10 @@ import (
|
|||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/Ennovar/gPanel/pkg/file"
|
||||
"github.com/Ennovar/gPanel/pkg/routing"
|
||||
)
|
||||
|
||||
|
|
@ -17,6 +19,9 @@ type Controller struct {
|
|||
Directory string
|
||||
GracefulShutdownTimeout time.Duration
|
||||
Status int
|
||||
ClientLogger *file.Handler
|
||||
ServerLogger *file.Handler
|
||||
LoadTimeLogger *file.Handler
|
||||
}
|
||||
|
||||
var controller Controller
|
||||
|
|
@ -24,10 +29,17 @@ var server http.Server
|
|||
|
||||
// New function returns a new PublicWeb type.
|
||||
func New() *Controller {
|
||||
clientLogHandler, _ := file.Open(file.LOG_CLIENT_ERRORS, true, true)
|
||||
serverLogHandler, _ := file.Open(file.LOG_CLIENT_ERRORS, true, true)
|
||||
loadLogHandler, _ := file.Open(file.LOG_LOADTIME, true, true)
|
||||
|
||||
controller = Controller{
|
||||
Directory: "document_roots/public/",
|
||||
GracefulShutdownTimeout: 5 * time.Second,
|
||||
Status: 0,
|
||||
ClientLogger: clientLogHandler,
|
||||
ServerLogger: serverLogHandler,
|
||||
LoadTimeLogger: loadLogHandler,
|
||||
}
|
||||
|
||||
server = http.Server{
|
||||
|
|
@ -111,6 +123,8 @@ func (con *Controller) Maintenance() {
|
|||
// 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)
|
||||
|
|
@ -135,6 +149,7 @@ func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
|||
f, err := os.Open(path)
|
||||
|
||||
if err != nil {
|
||||
con.ClientLogger.Write(path + "::" + strconv.Itoa(http.StatusNotFound) + "::" + err.Error())
|
||||
routing.HttpThrowStatus(http.StatusNotFound, res)
|
||||
return
|
||||
}
|
||||
|
|
@ -142,6 +157,7 @@ func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
|||
contentType, err := routing.GetContentType(path)
|
||||
|
||||
if err != nil {
|
||||
con.ClientLogger.Write(path + "::" + strconv.Itoa(http.StatusUnsupportedMediaType) + "::" + err.Error())
|
||||
routing.HttpThrowStatus(http.StatusUnsupportedMediaType, res)
|
||||
return
|
||||
}
|
||||
|
|
@ -150,7 +166,11 @@ func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
|||
_, err = io.Copy(res, f)
|
||||
|
||||
if err != nil {
|
||||
con.ServerLogger.Write(path + "::" + strconv.Itoa(http.StatusInternalServerError) + "::" + err.Error())
|
||||
routing.HttpThrowStatus(http.StatusInternalServerError, res)
|
||||
return
|
||||
}
|
||||
|
||||
elapsedTime := time.Since(startTime)
|
||||
con.LoadTimeLogger.Write(path + " rendered in " + strconv.FormatFloat(elapsedTime.Seconds(), 'f', 6, 64) + " seconds")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,22 +5,28 @@ import (
|
|||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/Ennovar/gPanel/pkg/api"
|
||||
"github.com/Ennovar/gPanel/pkg/file"
|
||||
"github.com/Ennovar/gPanel/pkg/public"
|
||||
"github.com/Ennovar/gPanel/pkg/routing"
|
||||
)
|
||||
|
||||
type Controller struct {
|
||||
Directory string
|
||||
Public *public.Controller
|
||||
Directory string
|
||||
Public *public.Controller
|
||||
ServerLogger *file.Handler
|
||||
}
|
||||
|
||||
// New returns a new PrivateHost type.
|
||||
func New() Controller {
|
||||
serverErrorLogger, _ := file.Open(file.LOG_SERVER_ERRORS, true, true)
|
||||
|
||||
return Controller{
|
||||
Directory: "document_roots/webhost/",
|
||||
Public: public.New(),
|
||||
Directory: "document_roots/webhost/",
|
||||
Public: public.New(),
|
||||
ServerLogger: serverErrorLogger,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -36,6 +42,7 @@ func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
|||
|
||||
if reqAuth(path) {
|
||||
if !checkAuth(res, req) {
|
||||
con.ServerLogger.Write(path + "::" + strconv.Itoa(http.StatusUnauthorized) + "::" + http.StatusText(http.StatusUnauthorized))
|
||||
http.Error(res, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
|
@ -51,6 +58,7 @@ func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
|||
f, err := os.Open(path)
|
||||
|
||||
if err != nil {
|
||||
con.ServerLogger.Write(path + "::" + strconv.Itoa(http.StatusNotFound) + "::" + err.Error())
|
||||
routing.HttpThrowStatus(http.StatusNotFound, res)
|
||||
return
|
||||
}
|
||||
|
|
@ -58,6 +66,7 @@ func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
|||
contentType, err := routing.GetContentType(path)
|
||||
|
||||
if err != nil {
|
||||
con.ServerLogger.Write(path + "::" + strconv.Itoa(http.StatusUnsupportedMediaType) + "::" + err.Error())
|
||||
routing.HttpThrowStatus(http.StatusUnsupportedMediaType, res)
|
||||
return
|
||||
}
|
||||
|
|
@ -66,6 +75,7 @@ func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
|||
_, err = io.Copy(res, f)
|
||||
|
||||
if err != nil {
|
||||
con.ServerLogger.Write(path + "::" + strconv.Itoa(http.StatusInternalServerError) + "::" + err.Error())
|
||||
routing.HttpThrowStatus(http.StatusInternalServerError, res)
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue