From de809e2837b0a71996d02847747d7e9871e677ac Mon Sep 17 00:00:00 2001 From: Odyssey Date: Sat, 7 Jan 2023 17:44:25 +0100 Subject: [PATCH] some changes Signed-off-by: Odyssey --- go.mod | 3 +++ go.sum | 6 ++++++ main.go | 11 ++++++++++- pages/signup.go | 11 +++++++++++ utils/port.go | 14 -------------- 5 files changed, 30 insertions(+), 15 deletions(-) delete mode 100644 utils/port.go diff --git a/go.mod b/go.mod index 4e56040..e2c548c 100644 --- a/go.mod +++ b/go.mod @@ -16,5 +16,8 @@ require ( github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasthttp v1.43.0 // indirect github.com/valyala/tcplisten v1.0.0 // indirect + go.uber.org/atomic v1.10.0 // indirect + go.uber.org/multierr v1.9.0 // indirect + go.uber.org/zap v1.24.0 // indirect golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect ) diff --git a/go.sum b/go.sum index 3a52fcd..832e2f0 100644 --- a/go.sum +++ b/go.sum @@ -151,8 +151,14 @@ github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= +go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ= +go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= +go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= +go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= +go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60= +go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= diff --git a/main.go b/main.go index 9a45159..d73e7e1 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,10 @@ package main import ( + "os" "os/exec" "github.com/ProjectSegfault/publapi/pages" - "github.com/ProjectSegfault/publapi/utils" "github.com/gofiber/fiber/v2" ) @@ -36,3 +36,12 @@ func main() { app.Listen(utils.GetPort()) } + +// GetPort returns the port to listen on +func GetPort() string { + port := os.Getenv("PUBLAPI_PORT") + if port == "" { + port = "3000" + } + return ":" + port +} diff --git a/pages/signup.go b/pages/signup.go index a8159ee..5fdd755 100644 --- a/pages/signup.go +++ b/pages/signup.go @@ -9,25 +9,34 @@ import ( "github.com/sethvargo/go-password/password" "github.com/containrrr/shoutrrr" + + "go.uber.org/zap" ) // SignupPage is the signup page handler func SignupPage(c *fiber.Ctx) error { + // set up logger + logger, _ := zap.NewProduction() + defer logger.Sync() + username := c.FormValue("username") email := c.FormValue("email") if username == "" || email == "" { + logger.Error("username or email is empty", zap.String("username", username), zap.String("email", email)) return c.SendStatus(fiber.StatusBadRequest) } // generate password pass, err := password.Generate(30, 10, 10, false, false) if err != nil { + logger.Error("failed to generate password", zap.Error(err)) return c.SendStatus(fiber.StatusInternalServerError) } // create user file f, err := os.Create("/var/publapi/users/" + username + ".sh") if err != nil { + logger.Error("failed to create user file", zap.Error(err)) return c.SendStatus(fiber.StatusInternalServerError) } defer f.Close() @@ -44,12 +53,14 @@ func SignupPage(c *fiber.Ctx) error { // write to file _, err = f.WriteString(bashscript) if err != nil { + logger.Error("failed to write to user file", zap.Error(err)) return c.SendStatus(fiber.StatusInternalServerError) } // send notification to admins err = shoutrrr.Send(os.Getenv("PUBLAPI_SHOUTRRRURL"), "New user signup! Please review /var/publapi/users/"+username+".sh to approve or deny the user.") if err != nil { + logger.Error("failed to send notification to admins", zap.Error(err)) return c.SendStatus(fiber.StatusInternalServerError) } return c.JSON(fiber.Map{ diff --git a/utils/port.go b/utils/port.go deleted file mode 100644 index 8380fa9..0000000 --- a/utils/port.go +++ /dev/null @@ -1,14 +0,0 @@ -package utils - -import ( - "os" -) - -// GetPort returns the port to listen on -func GetPort() string { - port := os.Getenv("PUBLAPI_PORT") - if port == "" { - port = "3000" - } - return ":" + port -}