From 21a7519c0f59c4398c5471c0e49366b88f67fb81 Mon Sep 17 00:00:00 2001 From: George Shaw Date: Tue, 21 Nov 2017 14:51:34 -0600 Subject: [PATCH] fixed logging --- account/assets/js/panelHandlers/log/view.js | 2 +- pkg/api/log/delete.go | 24 +++++++++--------- pkg/api/log/read.go | 24 +++++++++--------- pkg/file/file.go | 12 +++------ pkg/gpaccount/apihandler.go | 4 +-- pkg/gpaccount/gpaccount.go | 17 +++++++------ pkg/public/public.go | 27 +++++++++++---------- 7 files changed, 53 insertions(+), 57 deletions(-) diff --git a/account/assets/js/panelHandlers/log/view.js b/account/assets/js/panelHandlers/log/view.js index 423892e..0dd89fd 100644 --- a/account/assets/js/panelHandlers/log/view.js +++ b/account/assets/js/panelHandlers/log/view.js @@ -24,7 +24,7 @@ jQuery('._js_diagnostics-view-log').on('click', function(e){ jQuery(logModal).find('._js_diagnostics-clear-log').attr('data', logName); var requestData = {}; - requestData["name"] = logName+".log"; + requestData["name"] = logName; var xhr = new XMLHttpRequest(); xhr.open('POST', 'api/log/read', true); diff --git a/pkg/api/log/delete.go b/pkg/api/log/delete.go index fc5957f..5d7e11d 100644 --- a/pkg/api/log/delete.go +++ b/pkg/api/log/delete.go @@ -10,7 +10,7 @@ import ( // Delete function is accessed from api/logs/delete and will attempt to // delete a given log based off of request data. -func Delete(res http.ResponseWriter, req *http.Request) bool { +func Delete(res http.ResponseWriter, req *http.Request, dir string) bool { if req.Method != "UPDATE" { http.Error(res, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return false @@ -26,20 +26,20 @@ func Delete(res http.ResponseWriter, req *http.Request) bool { return false } - valid := false - for _, log := range file.KNOWN_LOGS { - if log == deleteLogRequestData.Name { - valid = true - break - } - } - - if !valid { - http.Error(res, "Attempting to delete an unknown log file.", http.StatusBadRequest) + var log string + switch deleteLogRequestData.Name { + case "client_errors": + log = dir + "logs/" + file.LOG_CLIENT_ERRORS + case "load_time": + log = dir + "logs/" + file.LOG_LOADTIME + case "server_errors": + log = file.LOG_SERVER_ERRORS + default: + http.Error(res, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) return false } - handle, err := file.Open(deleteLogRequestData.Name, true, true) + handle, err := file.Open(log, true) if err != nil { http.Error(res, err.Error(), http.StatusBadRequest) return false diff --git a/pkg/api/log/read.go b/pkg/api/log/read.go index 74346d7..d4a5e2f 100644 --- a/pkg/api/log/read.go +++ b/pkg/api/log/read.go @@ -10,7 +10,7 @@ import ( // Read function is accessed from api/logs/read and will attempt to read // a given log based off of the request data. -func Read(res http.ResponseWriter, req *http.Request) bool { +func Read(res http.ResponseWriter, req *http.Request, dir string) bool { if req.Method != "POST" { http.Error(res, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return false @@ -26,20 +26,20 @@ func Read(res http.ResponseWriter, req *http.Request) bool { return false } - valid := false - for _, log := range file.KNOWN_LOGS { - if log == readLogRequestData.Name { - valid = true - break - } - } - - if !valid { - http.Error(res, "Attempting to read from an unknown log file.", http.StatusBadRequest) + var log string + switch readLogRequestData.Name { + case "client_errors": + log = dir + "logs/" + file.LOG_CLIENT_ERRORS + case "load_time": + log = dir + "logs/" + file.LOG_LOADTIME + case "server_errors": + log = file.LOG_SERVER_ERRORS + default: + http.Error(res, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) return false } - handle, err := file.Open(readLogRequestData.Name, true, true) + handle, err := file.Open(log, true) if err != nil { http.Error(res, err.Error(), http.StatusBadRequest) return false diff --git a/pkg/file/file.go b/pkg/file/file.go index 5ca6845..7c94097 100644 --- a/pkg/file/file.go +++ b/pkg/file/file.go @@ -9,12 +9,10 @@ import ( const ( LOG_CLIENT_ERRORS = "client_errors.log" - LOG_SERVER_ERRORS = "server_errors.log" + LOG_SERVER_ERRORS = "server/logs/server_errors.log" LOG_LOADTIME = "load_time.log" ) -var KNOWN_LOGS = [...]string{LOG_CLIENT_ERRORS, LOG_SERVER_ERRORS, LOG_LOADTIME} - type Handler struct { fileHandle *os.File path string @@ -25,17 +23,13 @@ type Handler struct { // This function takes a parameter append which if set to false will truncate the // file upon writing to it. The log parameter denotes whether or not to look inside // of the log folder when attempting to open a given file. -func Open(file string, append bool, log bool) (*Handler, error) { +func Open(file string, append bool) (*Handler, error) { var err error var absPath string var f *os.File // Generate file path - if log { - absPath, err = filepath.Abs("logs/" + file) - } else { - absPath, err = filepath.Abs(file) - } + absPath, err = filepath.Abs(file) // Handle file path errors if err != nil { diff --git a/pkg/gpaccount/apihandler.go b/pkg/gpaccount/apihandler.go index bc0c346..9012d2e 100644 --- a/pkg/gpaccount/apihandler.go +++ b/pkg/gpaccount/apihandler.go @@ -39,9 +39,9 @@ func (con *Controller) apiHandler(res http.ResponseWriter, req *http.Request) (b case "/server/maintenance": return true, server.Maintenance(res, req, con.Public) case "/log/read": - return true, log.Read(res, req) + return true, log.Read(res, req, con.Directory) case "/log/delete": - return true, log.Delete(res, req) + return true, log.Delete(res, req, con.Directory) default: return false, false } diff --git a/pkg/gpaccount/gpaccount.go b/pkg/gpaccount/gpaccount.go index 23f9455..a6bc4a0 100644 --- a/pkg/gpaccount/gpaccount.go +++ b/pkg/gpaccount/gpaccount.go @@ -8,6 +8,7 @@ import ( "strconv" "time" + "github.com/Ennovar/gPanel/pkg/file" "github.com/Ennovar/gPanel/pkg/public" "github.com/Ennovar/gPanel/pkg/routing" ) @@ -19,7 +20,7 @@ type Controller struct { Public *public.Controller GracefulShutdownTimeout time.Duration Status int - // ServerLogger *file.Handler + ServerLogger *file.Handler } var controller Controller @@ -27,16 +28,16 @@ var httpserver http.Server // New returns a new Controller reference. func New(dir string, accPort int, pubPort int) *Controller { - // serverErrorLogger, _ := file.Open(file.LOG_SERVER_ERRORS, true, true) + serverErrorLogger, _ := file.Open(file.LOG_SERVER_ERRORS, true) controller = Controller{ Directory: dir, DocumentRoot: "account/", Port: accPort, - Public: public.New(dir+"public/", pubPort), + Public: public.New(dir, pubPort), GracefulShutdownTimeout: 5 * time.Second, Status: 0, - // ServerLogger: serverErrorLogger, + ServerLogger: serverErrorLogger, } httpserver = http.Server{ @@ -62,7 +63,7 @@ func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) { if reqAuth(path) { if !con.checkAuth(res, req) { - // con.ServerLogger.Write(path + "::" + strconv.Itoa(http.StatusUnauthorized) + "::" + http.StatusText(http.StatusUnauthorized)) + con.ServerLogger.Write(path + "::" + strconv.Itoa(http.StatusUnauthorized) + "::" + http.StatusText(http.StatusUnauthorized)) http.Error(res, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) return } @@ -78,7 +79,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()) + con.ServerLogger.Write(path + "::" + strconv.Itoa(http.StatusNotFound) + "::" + err.Error()) routing.HttpThrowStatus(http.StatusNotFound, res) return } @@ -86,7 +87,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()) + con.ServerLogger.Write(path + "::" + strconv.Itoa(http.StatusUnsupportedMediaType) + "::" + err.Error()) routing.HttpThrowStatus(http.StatusUnsupportedMediaType, res) return } @@ -95,7 +96,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()) + con.ServerLogger.Write(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 1264186..a6b6481 100644 --- a/pkg/public/public.go +++ b/pkg/public/public.go @@ -9,6 +9,7 @@ import ( "strconv" "time" + "github.com/Ennovar/gPanel/pkg/file" "github.com/Ennovar/gPanel/pkg/routing" ) @@ -17,25 +18,25 @@ type Controller struct { Port int GracefulShutdownTimeout time.Duration Status int - // ClientLogger *file.Handler - // LoadTimeLogger *file.Handler + ClientLogger *file.Handler + LoadTimeLogger *file.Handler } var controller Controller var server http.Server // New function returns a new PublicWeb type. -func New(root string, port int) *Controller { - // clientLogHandler, _ := file.Open(file.LOG_CLIENT_ERRORS, true, true) - // loadLogHandler, _ := file.Open(file.LOG_LOADTIME, true, true) +func New(dir string, port int) *Controller { + clientLogHandler, _ := file.Open(dir+"logs/"+file.LOG_CLIENT_ERRORS, true) + loadLogHandler, _ := file.Open(dir+"logs/"+file.LOG_LOADTIME, true) controller = Controller{ - DocumentRoot: root, + DocumentRoot: dir + "public/", Port: port, GracefulShutdownTimeout: 5 * time.Second, Status: 0, - // ClientLogger: clientLogHandler, - // LoadTimeLogger: loadLogHandler, + ClientLogger: clientLogHandler, + LoadTimeLogger: loadLogHandler, } server = http.Server{ @@ -52,7 +53,7 @@ func New(root string, port int) *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() + startTime := time.Now() switch con.Status { case 0: // This will actually never show because this function won't run if the server is off @@ -78,7 +79,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()) + con.ClientLogger.Write(path + "::" + strconv.Itoa(http.StatusNotFound) + "::" + err.Error()) routing.HttpThrowStatus(http.StatusNotFound, res) return } @@ -86,7 +87,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()) + con.ClientLogger.Write(path + "::" + strconv.Itoa(http.StatusUnsupportedMediaType) + "::" + err.Error()) routing.HttpThrowStatus(http.StatusUnsupportedMediaType, res) return } @@ -100,6 +101,6 @@ func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) { return } - // elapsedTime := time.Since(startTime) - // con.LoadTimeLogger.Write(path + " rendered in " + strconv.FormatFloat(elapsedTime.Seconds(), 'f', 6, 64) + " seconds") + elapsedTime := time.Since(startTime) + con.LoadTimeLogger.Write(path + " rendered in " + strconv.FormatFloat(elapsedTime.Seconds(), 'f', 6, 64) + " seconds") }