From 875d5b75a737cdf2b8ac7805076d794ca3b9f7cc Mon Sep 17 00:00:00 2001 From: Ophestra Umiker <cat@ophivana.moe> Date: Mon, 14 Oct 2024 22:50:23 +0900 Subject: [PATCH] refactor: move captcha route into separate file Signed-off-by: Ophestra Umiker <cat@ophivana.moe> --- captcha.go | 34 ++++++++++++++++++++++++++++++++++ register.go | 30 ++---------------------------- 2 files changed, 36 insertions(+), 28 deletions(-) create mode 100644 captcha.go diff --git a/captcha.go b/captcha.go new file mode 100644 index 0000000..edb3cd2 --- /dev/null +++ b/captcha.go @@ -0,0 +1,34 @@ +package main + +import ( + "log" + + "github.com/gofiber/fiber/v3" +) + +// Route to expose hCaptcha site key +func routeHCaptchaSiteKey(app *fiber.App) { + app.Get("/hcaptcha-site-key", func(c fiber.Ctx) error { + if conf[hCaptchaSiteKey] == "unset" { + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "message": "hCaptcha site key not configured", + }) + } + return c.JSON(fiber.Map{ + "hcaptcha_site_key": conf[hCaptchaSiteKey], + }) + }) +} + +// Middleware to conditionally apply hCaptcha +func conditionalCaptcha(captcha fiber.Handler) fiber.Handler { + return func(c fiber.Ctx) error { + if conf[hCaptchaSecret] == "unset" { + if verbose { + log.Printf("Captcha bypassed for %q", c.IP()) + } + return c.Next() + } + return captcha(c) + } +} diff --git a/register.go b/register.go index 84a00f9..109b9ec 100644 --- a/register.go +++ b/register.go @@ -3,8 +3,9 @@ package main import ( "log" "regexp" - "github.com/gofiber/fiber/v3" + "github.com/gofiber/contrib/hcaptcha" + "github.com/gofiber/fiber/v3" "github.com/syndtr/goleveldb/leveldb" ) @@ -14,19 +15,6 @@ type registration struct { Email string `json:"email"` } -// Middleware to conditionally apply hCaptcha -func conditionalCaptcha(captcha fiber.Handler) fiber.Handler { - return func(c fiber.Ctx) error { - if conf[hCaptchaSecret] == "unset" { - if verbose { - log.Printf("Captcha bypassed for %q", c.IP()) - } - return c.Next() - } - return captcha(c) - } -} - // Waitlist registration route func routeRegister(app *fiber.App, db *leveldb.DB) { @@ -86,17 +74,3 @@ func routeRegister(app *fiber.App, db *leveldb.DB) { }) }) } - -// Route to expose hCaptcha site key -func routeHCaptchaSiteKey(app *fiber.App) { - app.Get("/hcaptcha-site-key", func(c fiber.Ctx) error { - if conf[hCaptchaSiteKey] == "unset" { - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ - "message": "hCaptcha site key not configured", - }) - } - return c.JSON(fiber.Map{ - "hcaptcha_site_key": conf[hCaptchaSiteKey], - }) - }) -} \ No newline at end of file -- GitLab