resolves issue #71

This commit is contained in:
Aaron 2017-11-27 21:59:45 -06:00
parent 61cbdb0f7f
commit 4babe59e9c
16 changed files with 132 additions and 54 deletions

View file

@ -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()

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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