fixed issues #87, #88

This commit is contained in:
George Shaw 2017-11-28 16:24:55 -06:00
parent cc6d19c435
commit 853a5311bd
10 changed files with 64 additions and 59 deletions

View file

@ -101,7 +101,7 @@ func Create(res http.ResponseWriter, req *http.Request, logger *log.Logger, bund
return false
}
bundles[createBundleRequestData.Name] = gpaccount.New(newBundle+"/", databaseBundlePorts.Account, databaseBundlePorts.Public, logger)
bundles[createBundleRequestData.Name] = gpaccount.New(newBundle+"/", databaseBundlePorts.Account, databaseBundlePorts.Public)
_ = bundles[createBundleRequestData.Name].Start()
_ = bundles[createBundleRequestData.Name].Public.Start()

9
pkg/api/log/log.go Normal file
View file

@ -0,0 +1,9 @@
// Package log is a child of package api to handle api calls concerning log files
package log
const (
LOG_PUBLIC_ERRORS = "public_errors.log"
LOG_ACCOUNT_ERRORS = "account_errors.log"
LOG_SERVER_ERRORS = "server/logs/server_errors.log"
LOG_PUBLIC_LOAD = "public_load_time.log"
)

View file

@ -33,13 +33,13 @@ func Read(res http.ResponseWriter, req *http.Request, logger *log.Logger, dir st
var log string
switch readLogRequestData.Name {
case "public_errors":
log = dir + "logs/" + file.LOG_PUBLIC_ERRORS
log = dir + "logs/" + LOG_PUBLIC_ERRORS
case "account_errors":
log = dir + "logs/" + file.LOG_ACCOUNT_ERRORS
log = dir + "logs/" + LOG_ACCOUNT_ERRORS
case "public_load_time":
log = dir + "logs/" + file.LOG_PUBLIC_LOAD
log = dir + "logs/" + LOG_PUBLIC_LOAD
case "server_errors":
log = file.LOG_SERVER_ERRORS
log = LOG_SERVER_ERRORS
default:
logger.Println(req.URL.Path + "::unknown log type requested")
http.Error(res, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)

View file

@ -5,14 +5,13 @@ import (
"encoding/json"
"log"
"net/http"
"os"
"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, logger *log.Logger, dir string) bool {
func Truncate(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)
@ -33,30 +32,30 @@ func Delete(res http.ResponseWriter, req *http.Request, logger *log.Logger, dir
var log string
switch deleteLogRequestData.Name {
case "public_errors":
log = dir + "logs/" + file.LOG_PUBLIC_ERRORS
log = dir + "logs/" + LOG_PUBLIC_ERRORS
case "account_errors":
log = dir + "logs/" + file.LOG_ACCOUNT_ERRORS
log = dir + "logs/" + LOG_ACCOUNT_ERRORS
case "public_load_time":
log = dir + "logs/" + file.LOG_PUBLIC_LOAD
log = dir + "logs/" + LOG_PUBLIC_LOAD
case "server_errors":
log = file.LOG_SERVER_ERRORS
log = 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)
f, err := os.OpenFile(log, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
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)
err = f.Close()
if err != nil {
logger.Println(req.URL.Path + "::" + err.Error())
http.Error(res, err.Error(), http.StatusInternalServerError)
return false
}

View file

@ -7,13 +7,6 @@ import (
"path/filepath"
)
const (
LOG_PUBLIC_ERRORS = "public_errors.log"
LOG_ACCOUNT_ERRORS = "account_errors.log"
LOG_SERVER_ERRORS = "server/logs/server_errors.log"
LOG_PUBLIC_LOAD = "public_load_time.log"
)
type Handler struct {
fileHandle *os.File
path string

View file

@ -5,7 +5,7 @@ import (
"net/http"
"strings"
"github.com/Ennovar/gPanel/pkg/api/log"
logapi "github.com/Ennovar/gPanel/pkg/api/log"
"github.com/Ennovar/gPanel/pkg/api/server"
"github.com/Ennovar/gPanel/pkg/api/user"
)
@ -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.APILogger, con.Public)
case "/log/read":
return true, log.Read(res, req, con.APILogger, con.Directory)
return true, logapi.Read(res, req, con.APILogger, con.Directory)
case "/log/delete":
return true, log.Delete(res, req, con.APILogger, con.Directory)
return true, logapi.Truncate(res, req, con.APILogger, con.Directory)
default:
return false, false
}

View file

@ -10,7 +10,6 @@ import (
"strconv"
"time"
"github.com/Ennovar/gPanel/pkg/file"
"github.com/Ennovar/gPanel/pkg/public"
"github.com/Ennovar/gPanel/pkg/routing"
)
@ -22,7 +21,7 @@ type Controller struct {
Public *public.Controller
GracefulShutdownTimeout time.Duration
Status int
AccountLogger *file.Handler
AccountLogger *log.Logger
APILogger *log.Logger
}
@ -30,12 +29,15 @@ var controller Controller
var httpserver http.Server
// New returns a new Controller reference.
func New(dir string, accPort int, pubPort int, apiLogger *log.Logger) *Controller {
accountErrorLogger, err := file.Open(dir+"logs/"+file.LOG_ACCOUNT_ERRORS, true)
func New(dir string, accPort int, pubPort int) *Controller {
f, err := os.OpenFile(dir+"logs/account_errors.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
fmt.Errorf("Error whilst trying to start account logging instance: %v\n", err.Error())
fmt.Println("error whilst trying to start server logging instance:", err.Error())
}
apiLogger := log.New(f, "API :: ", 3)
accountLogger := log.New(f, "ACCOUNT :: ", 3)
controller = Controller{
Directory: dir,
DocumentRoot: "account/",
@ -43,7 +45,7 @@ func New(dir string, accPort int, pubPort int, apiLogger *log.Logger) *Controlle
Public: public.New(dir, pubPort),
GracefulShutdownTimeout: 5 * time.Second,
Status: 0,
AccountLogger: accountErrorLogger,
AccountLogger: accountLogger,
APILogger: apiLogger,
}
@ -70,7 +72,7 @@ func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) {
if reqAuth(path) {
if !con.checkAuth(res, req) {
con.AccountLogger.Write(path + "::" + strconv.Itoa(http.StatusUnauthorized) + "::" + http.StatusText(http.StatusUnauthorized))
con.AccountLogger.Println(path + "::" + strconv.Itoa(http.StatusUnauthorized) + "::" + http.StatusText(http.StatusUnauthorized))
http.Error(res, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
@ -86,7 +88,7 @@ func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) {
f, err := os.Open(path)
if err != nil {
con.AccountLogger.Write(path + "::" + strconv.Itoa(http.StatusNotFound) + "::" + err.Error())
con.AccountLogger.Println(path + "::" + strconv.Itoa(http.StatusNotFound) + "::" + err.Error())
routing.HttpThrowStatus(http.StatusNotFound, res)
return
}
@ -94,7 +96,7 @@ func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) {
contentType, err := routing.GetContentType(path)
if err != nil {
con.AccountLogger.Write(path + "::" + strconv.Itoa(http.StatusUnsupportedMediaType) + "::" + err.Error())
con.AccountLogger.Println(path + "::" + strconv.Itoa(http.StatusUnsupportedMediaType) + "::" + err.Error())
routing.HttpThrowStatus(http.StatusUnsupportedMediaType, res)
return
}
@ -103,7 +105,7 @@ func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) {
_, err = io.Copy(res, f)
if err != nil {
con.AccountLogger.Write(path + "::" + strconv.Itoa(http.StatusInternalServerError) + "::" + err.Error())
con.AccountLogger.Println(path + "::" + strconv.Itoa(http.StatusInternalServerError) + "::" + err.Error())
routing.HttpThrowStatus(http.StatusInternalServerError, res)
return
}

View file

@ -9,7 +9,7 @@ import (
"strings"
"github.com/Ennovar/gPanel/pkg/api/bundle"
"github.com/Ennovar/gPanel/pkg/api/log"
logapi "github.com/Ennovar/gPanel/pkg/api/log"
"github.com/Ennovar/gPanel/pkg/api/server"
"github.com/Ennovar/gPanel/pkg/api/user"
)
@ -56,9 +56,9 @@ func (con *Controller) apiHandler(res http.ResponseWriter, req *http.Request) (b
case "/server/restart":
return true, server.Restart(res, req, con.APILogger, specific.Public)
case "/log/read":
return true, log.Read(res, req, con.APILogger, specific.Directory)
case "/log/delete":
return true, log.Delete(res, req, con.APILogger, specific.Directory)
return true, logapi.Read(res, req, con.APILogger, specific.Directory)
case "/log/truncate":
return true, logapi.Truncate(res, req, con.APILogger, specific.Directory)
default:
return false, false
}
@ -77,9 +77,9 @@ func (con *Controller) apiHandler(res http.ResponseWriter, req *http.Request) (b
case "/bundle/list":
return true, bundle.List(res, req, con.APILogger, con.Bundles)
case "/log/read":
return true, log.Read(res, req, con.APILogger, con.Directory)
return true, logapi.Read(res, req, con.APILogger, con.Directory)
case "/log/delete":
return true, log.Delete(res, req, con.APILogger, con.Directory)
return true, logapi.Truncate(res, req, con.APILogger, con.Directory)
default:
return false, false
}

View file

@ -12,7 +12,6 @@ import (
"strings"
"github.com/Ennovar/gPanel/pkg/api/bundle"
"github.com/Ennovar/gPanel/pkg/file"
"github.com/Ennovar/gPanel/pkg/gpaccount"
"github.com/Ennovar/gPanel/pkg/routing"
)
@ -33,12 +32,12 @@ func New() *Controller {
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)
f, err := os.OpenFile("server/logs/server_errors.log", 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)
apiLogger := log.New(f, "API :: ", 3)
for _, dir := range dirs {
if dir.Name() == "default_bundle" || !dir.IsDir() {
@ -49,7 +48,7 @@ func New() *Controller {
dirPath := "bundles/" + dir.Name() + "/"
err, accPort, pubPort := bundle.GetPorts(dirPath)
curBundle := gpaccount.New(dirPath, accPort, pubPort, apiLogger)
curBundle := gpaccount.New(dirPath, accPort, pubPort)
err = curBundle.Start()
err2 := curBundle.Public.Start()
@ -65,7 +64,7 @@ func New() *Controller {
Directory: "server/",
DocumentRoot: "document_root/",
Bundles: bundles,
ServerLogger: log.New(serverErrorLogger, "SERVER :: ", 3),
ServerLogger: log.New(f, "SERVER :: ", 3),
APILogger: apiLogger,
}
}

View file

@ -4,12 +4,12 @@ package public
import (
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/Ennovar/gPanel/pkg/file"
"github.com/Ennovar/gPanel/pkg/routing"
)
@ -18,8 +18,8 @@ type Controller struct {
Port int
GracefulShutdownTimeout time.Duration
Status int
PublicLogger *file.Handler
LoadTimeLogger *file.Handler
PublicLogger *log.Logger
LoadTimeLogger *log.Logger
}
var controller Controller
@ -27,23 +27,26 @@ var server http.Server
// New function returns a new PublicWeb type.
func New(dir string, port int) *Controller {
publicErrorHandler, err := file.Open(dir+"logs/"+file.LOG_PUBLIC_ERRORS, true)
f, err := os.OpenFile(dir+"logs/public_errors.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
fmt.Errorf("Error whilst trying to start public logging instance: %v\n", err.Error())
}
loadLogHandler, err := file.Open(dir+"logs/"+file.LOG_PUBLIC_LOAD, true)
fh, err := os.OpenFile(dir+"logs/public_load_time.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
fmt.Errorf("Error whilst trying to start public load time logging instance: %v\n", err.Error())
}
publicLogger := log.New(f, "PUBLIC :: ", 3)
loadLogger := log.New(fh, "LOAD :: ", 3)
controller = Controller{
DocumentRoot: dir + "public/",
Port: port,
GracefulShutdownTimeout: 5 * time.Second,
Status: 0,
PublicLogger: publicErrorHandler,
LoadTimeLogger: loadLogHandler,
PublicLogger: publicLogger,
LoadTimeLogger: loadLogger,
}
server = http.Server{
@ -86,7 +89,7 @@ func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) {
f, err := os.Open(path)
if err != nil {
con.PublicLogger.Write(path + "::" + strconv.Itoa(http.StatusNotFound) + "::" + err.Error())
con.PublicLogger.Println(path + "::" + strconv.Itoa(http.StatusNotFound) + "::" + err.Error())
routing.HttpThrowStatus(http.StatusNotFound, res)
return
}
@ -94,7 +97,7 @@ func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) {
contentType, err := routing.GetContentType(path)
if err != nil {
con.PublicLogger.Write(path + "::" + strconv.Itoa(http.StatusUnsupportedMediaType) + "::" + err.Error())
con.PublicLogger.Println(path + "::" + strconv.Itoa(http.StatusUnsupportedMediaType) + "::" + err.Error())
routing.HttpThrowStatus(http.StatusUnsupportedMediaType, res)
return
}
@ -103,11 +106,11 @@ func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) {
_, err = io.Copy(res, f)
if err != nil {
con.PublicLogger.Write(path + "::" + strconv.Itoa(http.StatusInternalServerError) + "::" + err.Error())
con.PublicLogger.Println(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")
con.LoadTimeLogger.Println(path + " rendered in " + strconv.FormatFloat(elapsedTime.Seconds(), 'f', 6, 64) + " seconds")
}