From 4babe59e9c57d8b2d0540a3cc5cdbed4e1641193 Mon Sep 17 00:00:00 2001 From: Aaron Date: Mon, 27 Nov 2017 21:59:45 -0600 Subject: [PATCH] resolves issue #71 --- pkg/api/bundle/create.go | 13 +++++++++++-- pkg/api/bundle/list.go | 7 ++++++- pkg/api/log/delete.go | 11 +++++++++-- pkg/api/log/read.go | 11 +++++++++-- pkg/api/server/maintenance.go | 5 ++++- pkg/api/server/restart.go | 7 ++++++- pkg/api/server/shutdown.go | 7 ++++++- pkg/api/server/start.go | 6 +++++- pkg/api/server/status.go | 4 +++- pkg/api/user/auth.go | 12 +++++++++++- pkg/api/user/logout.go | 6 +++++- pkg/api/user/register.go | 11 ++++++++++- pkg/gpaccount/apihandler.go | 20 ++++++++++---------- pkg/gpaccount/gpaccount.go | 5 ++++- pkg/gpserver/apihandler.go | 28 ++++++++++++++-------------- pkg/gpserver/gpserver.go | 33 +++++++++++++++++++-------------- 16 files changed, 132 insertions(+), 54 deletions(-) diff --git a/pkg/api/bundle/create.go b/pkg/api/bundle/create.go index 1a73c15..fd71d83 100644 --- a/pkg/api/bundle/create.go +++ b/pkg/api/bundle/create.go @@ -3,6 +3,7 @@ package bundle import ( "encoding/json" "errors" + "log" "net" "net/http" "strconv" @@ -12,8 +13,9 @@ import ( "github.com/Ennovar/gPanel/pkg/gpaccount" ) -func Create(res http.ResponseWriter, req *http.Request, bundles map[string]*gpaccount.Controller) bool { +func Create(res http.ResponseWriter, req *http.Request, logger *log.Logger, bundles map[string]*gpaccount.Controller) bool { if req.Method != "POST" { + logger.Println(req.URL.Path + "::" + req.Method + "::" + strconv.Itoa(http.StatusMethodNotAllowed) + "::" + http.StatusText(http.StatusMethodNotAllowed)) http.Error(res, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return false } @@ -26,12 +28,14 @@ func Create(res http.ResponseWriter, req *http.Request, bundles map[string]*gpac err := json.NewDecoder(req.Body).Decode(&createBundleRequestData) if err != nil { + logger.Println(req.URL.Path + "::" + err.Error()) http.Error(res, err.Error(), http.StatusBadRequest) return false } check, err := net.Listen("tcp", ":"+strconv.Itoa(createBundleRequestData.AccPort)) if err != nil { + logger.Println(req.URL.Path + "::" + "a service is already listening on port " + strconv.Itoa(createBundleRequestData.AccPort) + "::" + err.Error()) http.Error(res, "A service is already listening on port "+strconv.Itoa(createBundleRequestData.AccPort), http.StatusInternalServerError) return false } @@ -39,6 +43,7 @@ func Create(res http.ResponseWriter, req *http.Request, bundles map[string]*gpac check, err = net.Listen("tcp", ":"+strconv.Itoa(createBundleRequestData.PubPort)) if err != nil { + logger.Println(req.URL.Path + "::" + "a service is already listening on port " + strconv.Itoa(createBundleRequestData.PubPort) + "::" + err.Error()) http.Error(res, "A service is already listening on port "+strconv.Itoa(createBundleRequestData.PubPort), http.StatusInternalServerError) return false } @@ -61,6 +66,7 @@ func Create(res http.ResponseWriter, req *http.Request, bundles map[string]*gpac } if err != nil { + logger.Println(req.URL.Path + "::" + err.Error()) http.Error(res, err.Error(), http.StatusBadRequest) return false } @@ -68,12 +74,14 @@ func Create(res http.ResponseWriter, req *http.Request, bundles map[string]*gpac newBundle := "bundles/bundle_" + createBundleRequestData.Name err = file.CopyDir("bundles/default_bundle", newBundle) if err != nil { + logger.Println(req.URL.Path + "::" + err.Error()) http.Error(res, err.Error(), http.StatusInternalServerError) return false } ds, err := database.Open(newBundle + "/" + database.DB_MAIN) if err != nil { + logger.Println(req.URL.Path + "::" + err.Error()) http.Error(res, err.Error(), http.StatusBadRequest) return false } @@ -88,11 +96,12 @@ func Create(res http.ResponseWriter, req *http.Request, bundles map[string]*gpac err = ds.Put(database.BUCKET_PORTS, []byte("bundle_ports"), databaseBundlePorts) if err != nil { + logger.Println(req.URL.Path + "::" + err.Error()) http.Error(res, err.Error(), http.StatusBadRequest) return false } - bundles[createBundleRequestData.Name] = gpaccount.New(newBundle+"/", databaseBundlePorts.Account, databaseBundlePorts.Public) + bundles[createBundleRequestData.Name] = gpaccount.New(newBundle+"/", databaseBundlePorts.Account, databaseBundlePorts.Public, logger) _ = bundles[createBundleRequestData.Name].Start() _ = bundles[createBundleRequestData.Name].Public.Start() diff --git a/pkg/api/bundle/list.go b/pkg/api/bundle/list.go index 92176b7..b8d744b 100644 --- a/pkg/api/bundle/list.go +++ b/pkg/api/bundle/list.go @@ -2,18 +2,22 @@ package bundle import ( "encoding/json" + "log" "net/http" + "strconv" "github.com/Ennovar/gPanel/pkg/gpaccount" ) -func List(res http.ResponseWriter, req *http.Request, bundles map[string]*gpaccount.Controller) bool { +func List(res http.ResponseWriter, req *http.Request, logger *log.Logger, bundles map[string]*gpaccount.Controller) bool { if req.Method != "GET" { + logger.Println(req.URL.Path + "::" + req.Method + "::" + strconv.Itoa(http.StatusNotFound) + "::" + http.StatusText(http.StatusMethodNotAllowed)) http.Error(res, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return false } if len(bundles) <= 0 { + logger.Println("no bundles :: http response returns no content") res.WriteHeader(http.StatusNoContent) return true } @@ -25,6 +29,7 @@ func List(res http.ResponseWriter, req *http.Request, bundles map[string]*gpacco jsonData, err := json.Marshal(keys) if err != nil { + logger.Println(err) http.Error(res, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return false } diff --git a/pkg/api/log/delete.go b/pkg/api/log/delete.go index d3ed143..9ccca7a 100644 --- a/pkg/api/log/delete.go +++ b/pkg/api/log/delete.go @@ -1,17 +1,20 @@ -// Package logs is a child of package api to handle api calls concerning log files +// Package log is a child of package api to handle api calls concerning log files package log import ( "encoding/json" + "log" "net/http" + "strconv" "github.com/Ennovar/gPanel/pkg/file" ) // 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, dir string) bool { +func Delete(res http.ResponseWriter, req *http.Request, logger *log.Logger, dir string) bool { if req.Method != "UPDATE" { + logger.Println(req.URL.Path + "::" + req.Method + "::" + strconv.Itoa(http.StatusMethodNotAllowed) + "::" + http.StatusText(http.StatusMethodNotAllowed)) http.Error(res, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return false } @@ -22,6 +25,7 @@ func Delete(res http.ResponseWriter, req *http.Request, dir string) bool { err := json.NewDecoder(req.Body).Decode(&deleteLogRequestData) if err != nil { + logger.Println(req.URL.Path + "::" + err.Error()) http.Error(res, err.Error(), http.StatusBadRequest) return false } @@ -37,18 +41,21 @@ func Delete(res http.ResponseWriter, req *http.Request, dir string) bool { case "server_errors": log = file.LOG_SERVER_ERRORS default: + logger.Println(req.URL.Path + "::unknown log type requested") http.Error(res, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) return false } handle, err := file.Open(log, true) if err != nil { + logger.Println(req.URL.Path + "::" + err.Error()) http.Error(res, err.Error(), http.StatusBadRequest) return false } closeErr, deleteErr := handle.Close(true) if closeErr != nil || deleteErr != nil { + logger.Println(req.URL.Path + "::" + closeErr.Error() + " AND " + deleteErr.Error()) http.Error(res, closeErr.Error()+" AND "+deleteErr.Error(), http.StatusInternalServerError) return false } diff --git a/pkg/api/log/read.go b/pkg/api/log/read.go index bce194b..c19b904 100644 --- a/pkg/api/log/read.go +++ b/pkg/api/log/read.go @@ -1,17 +1,20 @@ -// Package logs is a child of package api to handle api calls concerning log files +// Package log is a child of package api to handle api calls concerning log files package log import ( "encoding/json" + "log" "net/http" + "strconv" "github.com/Ennovar/gPanel/pkg/file" ) // 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, dir string) bool { +func Read(res http.ResponseWriter, req *http.Request, logger *log.Logger, dir string) bool { if req.Method != "POST" { + logger.Println(req.URL.Path + "::" + req.Method + "::" + strconv.Itoa(http.StatusMethodNotAllowed) + "::" + http.StatusText(http.StatusMethodNotAllowed)) http.Error(res, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return false } @@ -22,6 +25,7 @@ func Read(res http.ResponseWriter, req *http.Request, dir string) bool { err := json.NewDecoder(req.Body).Decode(&readLogRequestData) if err != nil { + logger.Println(req.URL.Path + "::" + err.Error()) http.Error(res, err.Error(), http.StatusBadRequest) return false } @@ -37,12 +41,14 @@ func Read(res http.ResponseWriter, req *http.Request, dir string) bool { case "server_errors": log = file.LOG_SERVER_ERRORS default: + logger.Println(req.URL.Path + "::unknown log type requested") http.Error(res, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) return false } handle, err := file.Open(log, true) if err != nil { + logger.Println(req.URL.Path + "::" + err.Error()) http.Error(res, err.Error(), http.StatusBadRequest) return false } @@ -50,6 +56,7 @@ func Read(res http.ResponseWriter, req *http.Request, dir string) bool { data, err := handle.Read() if err != nil { + logger.Println(req.URL.Path + "::" + err.Error()) http.Error(res, err.Error(), http.StatusBadRequest) return false } diff --git a/pkg/api/server/maintenance.go b/pkg/api/server/maintenance.go index d09327f..3890aa1 100644 --- a/pkg/api/server/maintenance.go +++ b/pkg/api/server/maintenance.go @@ -2,15 +2,18 @@ package server import ( + "log" "net/http" + "strconv" "github.com/Ennovar/gPanel/pkg/public" ) // Maintenance function is called from /api/server/maintenance and will place the public server into // maintenance mode. -func Maintenance(res http.ResponseWriter, req *http.Request, publicServer *public.Controller) bool { +func Maintenance(res http.ResponseWriter, req *http.Request, logger *log.Logger, publicServer *public.Controller) bool { if req.Method != "UPDATE" { + logger.Println(req.URL.Path + "::" + req.Method + "::" + strconv.Itoa(http.StatusMethodNotAllowed) + "::" + http.StatusText(http.StatusMethodNotAllowed)) http.Error(res, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return false } diff --git a/pkg/api/server/restart.go b/pkg/api/server/restart.go index 6e553ad..8c6df6e 100644 --- a/pkg/api/server/restart.go +++ b/pkg/api/server/restart.go @@ -3,15 +3,18 @@ package server import ( "encoding/json" + "log" "net/http" + "strconv" "github.com/Ennovar/gPanel/pkg/public" ) // Restart function is called from /api/server/restart and will attempt to shutdown, either gracefully // or not gracefully (contingent on request data), and then turn back on the public server. -func Restart(res http.ResponseWriter, req *http.Request, publicServer *public.Controller) bool { +func Restart(res http.ResponseWriter, req *http.Request, logger *log.Logger, publicServer *public.Controller) bool { if req.Method != "UPDATE" { + logger.Println(req.URL.Path + "::" + req.Method + "::" + strconv.Itoa(http.StatusMethodNotAllowed) + "::" + http.StatusText(http.StatusMethodNotAllowed)) http.Error(res, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return false } @@ -22,12 +25,14 @@ func Restart(res http.ResponseWriter, req *http.Request, publicServer *public.Co err := json.NewDecoder(req.Body).Decode(&restartRequestData) if err != nil { + logger.Println(req.URL.Path + "::" + err.Error()) http.Error(res, err.Error(), http.StatusBadRequest) return false } err = publicServer.Restart(restartRequestData.graceful) if err != nil { + logger.Println(req.URL.Path + "::" + err.Error()) http.Error(res, err.Error(), http.StatusBadRequest) return false } diff --git a/pkg/api/server/shutdown.go b/pkg/api/server/shutdown.go index 3d6224d..f131f84 100644 --- a/pkg/api/server/shutdown.go +++ b/pkg/api/server/shutdown.go @@ -3,15 +3,18 @@ package server import ( "encoding/json" + "log" "net/http" + "strconv" "github.com/Ennovar/gPanel/pkg/public" ) // Shutdown function is called from /api/server/shutdown and will attempt to shutdown, either gracefully // or not gracefully (contingent on request data). -func Shutdown(res http.ResponseWriter, req *http.Request, publicServer *public.Controller) bool { +func Shutdown(res http.ResponseWriter, req *http.Request, logger *log.Logger, publicServer *public.Controller) bool { if req.Method != "UPDATE" { + logger.Println(req.URL.Path + "::" + req.Method + "::" + strconv.Itoa(http.StatusMethodNotAllowed) + "::" + http.StatusText(http.StatusMethodNotAllowed)) http.Error(res, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return false } @@ -22,12 +25,14 @@ func Shutdown(res http.ResponseWriter, req *http.Request, publicServer *public.C err := json.NewDecoder(req.Body).Decode(&shutdownRequestData) if err != nil { + logger.Println(req.URL.Path + "::" + err.Error()) http.Error(res, err.Error(), http.StatusBadRequest) return false } err = publicServer.Stop(shutdownRequestData.Graceful) if err != nil { + logger.Println(req.URL.Path + "::" + err.Error()) http.Error(res, err.Error(), http.StatusBadRequest) return false } diff --git a/pkg/api/server/start.go b/pkg/api/server/start.go index 6ca1d17..dfc1b2b 100644 --- a/pkg/api/server/start.go +++ b/pkg/api/server/start.go @@ -2,20 +2,24 @@ package server import ( + "log" "net/http" + "strconv" "github.com/Ennovar/gPanel/pkg/public" ) // Start function is called from /api/server/start and turn the public server on. -func Start(res http.ResponseWriter, req *http.Request, publicServer *public.Controller) bool { +func Start(res http.ResponseWriter, req *http.Request, logger *log.Logger, publicServer *public.Controller) bool { if req.Method != "UPDATE" { + logger.Println(req.URL.Path + "::" + req.Method + "::" + strconv.Itoa(http.StatusMethodNotAllowed) + "::" + http.StatusText(http.StatusMethodNotAllowed)) http.Error(res, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return false } err := publicServer.Start() if err != nil { + logger.Println(req.URL.Path + "::" + "public server was not able to start" + "::" + err.Error()) // TODO: should log server name? http.Error(res, err.Error(), http.StatusConflict) return false } diff --git a/pkg/api/server/status.go b/pkg/api/server/status.go index abb4d3a..0abc868 100644 --- a/pkg/api/server/status.go +++ b/pkg/api/server/status.go @@ -2,6 +2,7 @@ package server import ( + "log" "net/http" "strconv" @@ -10,8 +11,9 @@ import ( // Status function is called from api/server/status and will return the current status of // the public server. -func Status(res http.ResponseWriter, req *http.Request, publicServer *public.Controller) bool { +func Status(res http.ResponseWriter, req *http.Request, logger *log.Logger, publicServer *public.Controller) bool { if req.Method != "GET" && req.Method != "POST" { + logger.Println(req.URL.Path + "::" + req.Method + "::" + strconv.Itoa(http.StatusMethodNotAllowed) + "::" + http.StatusText(http.StatusMethodNotAllowed)) http.Error(res, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return false } diff --git a/pkg/api/user/auth.go b/pkg/api/user/auth.go index e636abe..d9d08f7 100644 --- a/pkg/api/user/auth.go +++ b/pkg/api/user/auth.go @@ -3,7 +3,9 @@ package user import ( "encoding/json" + "log" "net/http" + "strconv" "strings" "time" @@ -16,8 +18,9 @@ import ( // Auth function is accessed by an API call from the webhost root // by accessing /user_auth and sending it a post request with userRequestData // struct in JSON format. -func Auth(res http.ResponseWriter, req *http.Request, dir string) bool { +func Auth(res http.ResponseWriter, req *http.Request, logger *log.Logger, dir string) bool { if req.Method != "POST" { + logger.Println(req.URL.Path + "::" + req.Method + "::" + strconv.Itoa(http.StatusMethodNotAllowed) + "::" + http.StatusText(http.StatusMethodNotAllowed)) http.Error(res, req.Method+" HTTP method is unsupported for this API.", http.StatusMethodNotAllowed) return false } @@ -29,12 +32,14 @@ func Auth(res http.ResponseWriter, req *http.Request, dir string) bool { err := json.NewDecoder(req.Body).Decode(&userRequestData) if err != nil { + logger.Println(req.URL.Path + "::" + err.Error()) http.Error(res, err.Error(), http.StatusBadRequest) return false } ds, err := database.Open(dir + database.DB_MAIN) if err != nil || ds == nil { + logger.Println(req.URL.Path + "::" + err.Error()) http.Error(res, err.Error(), http.StatusInternalServerError) return false } @@ -48,12 +53,14 @@ func Auth(res http.ResponseWriter, req *http.Request, dir string) bool { err = ds.Get(database.BUCKET_USERS, []byte(userRequestData.User), &userDatabaseData) if err == database.ErrKeyNotExist { + logger.Println(req.URL.Path + "::user does not exist.") http.Error(res, "User does not exist.", http.StatusUnauthorized) return false } err = encryption.CheckPassword([]byte(userDatabaseData.Pass), []byte(userRequestData.Pass)) if err != nil { + logger.Println(req.URL.Path + "::" + err.Error()) http.Error(res, err.Error(), http.StatusUnauthorized) return false } @@ -63,6 +70,7 @@ func Auth(res http.ResponseWriter, req *http.Request, dir string) bool { err = ds.Put(database.BUCKET_USERS, []byte(userRequestData.User), userDatabaseData) if err != nil { + logger.Println(req.URL.Path + "::" + err.Error()) http.Error(res, err.Error(), http.StatusBadRequest) return false } @@ -77,6 +85,7 @@ func Auth(res http.ResponseWriter, req *http.Request, dir string) bool { token, err := jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString([]byte(secret)) if err != nil { + logger.Println(req.URL.Path + "::" + err.Error()) http.Error(res, err.Error(), http.StatusInternalServerError) return false } @@ -91,6 +100,7 @@ func Auth(res http.ResponseWriter, req *http.Request, dir string) bool { err = store.Set(res, req, "token", token, (60 * 60 * 24)) err2 := store.Set(res, req, "user", userRequestData.User, (60 * 60 * 24)) if err != nil || err2 != nil { + logger.Println(req.URL.Path + "::" + err.Error() + "::" + err2.Error()) http.Error(res, http.StatusText(500), http.StatusInternalServerError) return false } diff --git a/pkg/api/user/logout.go b/pkg/api/user/logout.go index 19f94b4..17db221 100644 --- a/pkg/api/user/logout.go +++ b/pkg/api/user/logout.go @@ -2,7 +2,9 @@ package user import ( + "log" "net/http" + "strconv" "strings" "github.com/Ennovar/gPanel/pkg/networking" @@ -11,8 +13,9 @@ import ( // Logout function is accessed by an API call from the webhost root // by accessing /user_logout and sending it an empty POST request. This function will // delete the user-auth cookie session store -func Logout(res http.ResponseWriter, req *http.Request, dir string) bool { +func Logout(res http.ResponseWriter, req *http.Request, logger *log.Logger, dir string) bool { if req.Method != "POST" { + logger.Println(req.URL.Path + "::" + req.Method + "::" + strconv.Itoa(http.StatusMethodNotAllowed) + "::" + http.StatusText(http.StatusMethodNotAllowed)) http.Error(res, req.Method+" HTTP method is unsupported for this API.", http.StatusMethodNotAllowed) return false } @@ -27,6 +30,7 @@ func Logout(res http.ResponseWriter, req *http.Request, dir string) bool { err := store.Delete(res, req) if err != nil { + logger.Println(req.URL.Path + "::" + err.Error()) http.Error(res, http.StatusText(500), http.StatusInternalServerError) return false } diff --git a/pkg/api/user/register.go b/pkg/api/user/register.go index 0fec4b6..a666727 100644 --- a/pkg/api/user/register.go +++ b/pkg/api/user/register.go @@ -3,7 +3,9 @@ package user import ( "encoding/json" + "log" "net/http" + "strconv" "github.com/Ennovar/gPanel/pkg/database" "github.com/Ennovar/gPanel/pkg/encryption" @@ -12,8 +14,9 @@ import ( // Register function is accessed by an API call from the webhost root // by accessing /user_register and sending it a post request with userRequestData // struct in JSON format. -func Register(res http.ResponseWriter, req *http.Request, dir string) bool { +func Register(res http.ResponseWriter, req *http.Request, logger *log.Logger, dir string) bool { if req.Method != "POST" { + logger.Println(req.URL.Path + "::" + req.Method + "::" + strconv.Itoa(http.StatusMethodNotAllowed) + "::" + http.StatusText(http.StatusMethodNotAllowed)) http.Error(res, req.Method+" HTTP method is unsupported for this API.", http.StatusMethodNotAllowed) return false } @@ -25,15 +28,18 @@ func Register(res http.ResponseWriter, req *http.Request, dir string) bool { err := json.NewDecoder(req.Body).Decode(&userRequestData) if err != nil { + logger.Println(req.URL.Path + "::" + err.Error()) http.Error(res, err.Error(), http.StatusBadRequest) return false } else if len(userRequestData.User) == 0 || len(userRequestData.Pass) == 0 { + logger.Println(req.URL.Path + "::username or password field cannot be blank") http.Error(res, "Username or password field cannot be blank", http.StatusBadRequest) return false } ds, err := database.Open(dir + database.DB_MAIN) if err != nil || ds == nil { + logger.Println(req.URL.Path + "::" + err.Error()) http.Error(res, err.Error(), http.StatusInternalServerError) return false } @@ -46,12 +52,14 @@ func Register(res http.ResponseWriter, req *http.Request, dir string) bool { err = ds.Get(database.BUCKET_USERS, []byte(userRequestData.User), &userDatabaseData) if err != database.ErrKeyNotExist { + logger.Println(req.URL.Path + "::username already exists in the database") http.Error(res, "Username already exists in the database", http.StatusBadRequest) return false } userDatabaseData.Pass, err = encryption.HashPassword(userRequestData.Pass) if err != nil { + logger.Println(req.URL.Path + "::" + err.Error()) http.Error(res, err.Error(), http.StatusInternalServerError) return false } @@ -60,6 +68,7 @@ func Register(res http.ResponseWriter, req *http.Request, dir string) bool { err = ds.Put(database.BUCKET_USERS, []byte(userRequestData.User), userDatabaseData) if err != nil { + logger.Println(req.URL.Path + "::" + err.Error()) http.Error(res, err.Error(), http.StatusInternalServerError) return false } diff --git a/pkg/gpaccount/apihandler.go b/pkg/gpaccount/apihandler.go index 9012d2e..c0e7eb4 100644 --- a/pkg/gpaccount/apihandler.go +++ b/pkg/gpaccount/apihandler.go @@ -23,25 +23,25 @@ func (con *Controller) apiHandler(res http.ResponseWriter, req *http.Request) (b switch suspectApi { case "/user/auth": - return true, user.Auth(res, req, con.Directory) + return true, user.Auth(res, req, con.APILogger, con.Directory) case "/user/register": - return true, user.Register(res, req, con.Directory) + return true, user.Register(res, req, con.APILogger, con.Directory) case "/user/logout": - return true, user.Logout(res, req, con.Directory) + return true, user.Logout(res, req, con.APILogger, con.Directory) case "/server/status": - return true, server.Status(res, req, con.Public) + return true, server.Status(res, req, con.APILogger, con.Public) case "/server/start": - return true, server.Start(res, req, con.Public) + return true, server.Start(res, req, con.APILogger, con.Public) case "/server/shutdown": - return true, server.Shutdown(res, req, con.Public) + return true, server.Shutdown(res, req, con.APILogger, con.Public) case "/server/restart": - return true, server.Restart(res, req, con.Public) + return true, server.Restart(res, req, con.APILogger, con.Public) case "/server/maintenance": - return true, server.Maintenance(res, req, con.Public) + return true, server.Maintenance(res, req, con.APILogger, con.Public) case "/log/read": - return true, log.Read(res, req, con.Directory) + return true, log.Read(res, req, con.APILogger, con.Directory) case "/log/delete": - return true, log.Delete(res, req, con.Directory) + return true, log.Delete(res, req, con.APILogger, con.Directory) default: return false, false } diff --git a/pkg/gpaccount/gpaccount.go b/pkg/gpaccount/gpaccount.go index 15f5c49..0f81d17 100644 --- a/pkg/gpaccount/gpaccount.go +++ b/pkg/gpaccount/gpaccount.go @@ -4,6 +4,7 @@ package gpaccount import ( "fmt" "io" + "log" "net/http" "os" "strconv" @@ -22,13 +23,14 @@ type Controller struct { GracefulShutdownTimeout time.Duration Status int AccountLogger *file.Handler + APILogger *log.Logger } var controller Controller var httpserver http.Server // New returns a new Controller reference. -func New(dir string, accPort int, pubPort int) *Controller { +func New(dir string, accPort int, pubPort int, apiLogger *log.Logger) *Controller { accountErrorLogger, err := file.Open(dir+"logs/"+file.LOG_ACCOUNT_ERRORS, true) if err != nil { fmt.Errorf("Error whilst trying to start account logging instance: %v\n", err.Error()) @@ -42,6 +44,7 @@ func New(dir string, accPort int, pubPort int) *Controller { GracefulShutdownTimeout: 5 * time.Second, Status: 0, AccountLogger: accountErrorLogger, + APILogger: apiLogger, } httpserver = http.Server{ diff --git a/pkg/gpserver/apihandler.go b/pkg/gpserver/apihandler.go index c2d5253..9f30dfb 100644 --- a/pkg/gpserver/apihandler.go +++ b/pkg/gpserver/apihandler.go @@ -46,19 +46,19 @@ func (con *Controller) apiHandler(res http.ResponseWriter, req *http.Request) (b if specific, ok := con.Bundles[bundleRequestData.BName]; ok { switch suspectApi { case "/server/status": - return true, server.Status(res, req, specific.Public) + return true, server.Status(res, req, con.APILogger, specific.Public) case "/server/start": - return true, server.Start(res, req, specific.Public) + return true, server.Start(res, req, con.APILogger, specific.Public) case "/server/shutdown": - return true, server.Shutdown(res, req, specific.Public) + return true, server.Shutdown(res, req, con.APILogger, specific.Public) case "/server/maintenance": - return true, server.Maintenance(res, req, specific.Public) + return true, server.Maintenance(res, req, con.APILogger, specific.Public) case "/server/restart": - return true, server.Restart(res, req, specific.Public) + return true, server.Restart(res, req, con.APILogger, specific.Public) case "/log/read": - return true, log.Read(res, req, specific.Directory) + return true, log.Read(res, req, con.APILogger, specific.Directory) case "/log/delete": - return true, log.Delete(res, req, specific.Directory) + return true, log.Delete(res, req, con.APILogger, specific.Directory) default: return false, false } @@ -67,19 +67,19 @@ func (con *Controller) apiHandler(res http.ResponseWriter, req *http.Request) (b switch suspectApi { case "/user/auth": - return true, user.Auth(res, req, con.Directory) + return true, user.Auth(res, req, con.APILogger, con.Directory) case "/user/register": - return true, user.Register(res, req, con.Directory) + return true, user.Register(res, req, con.APILogger, con.Directory) case "/user/logout": - return true, user.Logout(res, req, con.Directory) + return true, user.Logout(res, req, con.APILogger, con.Directory) case "/bundle/create": - return true, bundle.Create(res, req, con.Bundles) + return true, bundle.Create(res, req, con.APILogger, con.Bundles) case "/bundle/list": - return true, bundle.List(res, req, con.Bundles) + return true, bundle.List(res, req, con.APILogger, con.Bundles) case "/log/read": - return true, log.Read(res, req, con.Directory) + return true, log.Read(res, req, con.APILogger, con.Directory) case "/log/delete": - return true, log.Delete(res, req, con.Directory) + return true, log.Delete(res, req, con.APILogger, con.Directory) default: return false, false } diff --git a/pkg/gpserver/gpserver.go b/pkg/gpserver/gpserver.go index 9c2e20e..c0d72ad 100644 --- a/pkg/gpserver/gpserver.go +++ b/pkg/gpserver/gpserver.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "io/ioutil" + "log" "net/http" "os" "strconv" @@ -20,7 +21,8 @@ type Controller struct { Directory string DocumentRoot string Bundles map[string]*gpaccount.Controller - ServerLogger *file.Handler + ServerLogger *log.Logger + APILogger *log.Logger } func New() *Controller { @@ -28,9 +30,16 @@ func New() *Controller { dirs, err := ioutil.ReadDir("bundles/") if err != nil { - fmt.Errorf("Error finding bundles: %v\n", err.Error()) + fmt.Println("error finding bundles:", err.Error()) } + serverErrorLogger, err := os.OpenFile(file.LOG_SERVER_ERRORS, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) + if err != nil { + fmt.Println("error whilst trying to start server logging instance:", err.Error()) + } + + apiLogger := log.New(serverErrorLogger, "API :: ", 3) + for _, dir := range dirs { if dir.Name() == "default_bundle" || !dir.IsDir() { continue @@ -40,28 +49,24 @@ func New() *Controller { dirPath := "bundles/" + dir.Name() + "/" err, accPort, pubPort := bundle.GetPorts(dirPath) - curBundle := gpaccount.New(dirPath, accPort, pubPort) + curBundle := gpaccount.New(dirPath, accPort, pubPort, apiLogger) err = curBundle.Start() err2 := curBundle.Public.Start() if err != nil || err2 != nil { - fmt.Errorf("Error starting bundle: %v\n", dir.Name()) + fmt.Println("error starting bundle:", dir.Name()) } bundles[strings.Replace(dir.Name(), "bundle_", "", 1)] = curBundle } } - serverErrorLogger, err := file.Open(file.LOG_SERVER_ERRORS, true) - if err != nil { - fmt.Errorf("Error whilst trying to start server logging instance: %v\n", err.Error()) - } - return &Controller{ Directory: "server/", DocumentRoot: "document_root/", Bundles: bundles, - ServerLogger: serverErrorLogger, + ServerLogger: log.New(serverErrorLogger, "SERVER :: ", 3), + APILogger: apiLogger, } } @@ -75,7 +80,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.Println(path + "::" + strconv.Itoa(http.StatusUnauthorized) + "::" + http.StatusText(http.StatusUnauthorized)) http.Error(res, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) return } @@ -91,7 +96,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.Println(path + "::" + strconv.Itoa(http.StatusNotFound) + "::" + err.Error()) routing.HttpThrowStatus(http.StatusNotFound, res) return } @@ -99,7 +104,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.Println(path + "::" + strconv.Itoa(http.StatusUnsupportedMediaType) + "::" + err.Error()) routing.HttpThrowStatus(http.StatusUnsupportedMediaType, res) return } @@ -108,7 +113,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.Println(path + "::" + strconv.Itoa(http.StatusInternalServerError) + "::" + err.Error()) routing.HttpThrowStatus(http.StatusInternalServerError, res) return }