Skip to content
Snippets Groups Projects
Commit 9a0c93f0 authored by Levatax's avatar Levatax
Browse files

Merge branch 'api-fixup' into 'main'

Fix up API paths and responses

See merge request !5
parents 9abc425a 5376d04a
Branches
Tags
1 merge request!5Fix up API paths and responses
Pipeline #962 failed
...@@ -54,11 +54,8 @@ func serve(sig chan os.Signal, db *leveldb.DB) error { ...@@ -54,11 +54,8 @@ func serve(sig chan os.Signal, db *leveldb.DB) error {
confEnv[hCaptchaSiteKey][0], confEnv[hCaptchaSecretKey][0]) confEnv[hCaptchaSiteKey][0], confEnv[hCaptchaSecretKey][0])
} }
// /register routeHCaptchaSiteKey(app, "/api", !hCaptchaEnable, conf[hCaptchaSiteKey])
routeRegister(app, db, captcha) routeRegister(app, "/api/register", db, captcha)
// /hcaptcha-site-key
routeHCaptchaSiteKey(app, !hCaptchaEnable, conf[hCaptchaSiteKey])
// graceful shutdown // graceful shutdown
go func() { go func() {
......
...@@ -12,7 +12,7 @@ type respHSiteKey struct { ...@@ -12,7 +12,7 @@ type respHSiteKey struct {
// Route to expose hCaptcha site key. // Route to expose hCaptcha site key.
// Returns a constant pre-generated response // Returns a constant pre-generated response
// to avoid unnecessary allocations or serialisations // to avoid unnecessary allocations or serialisations
func routeHCaptchaSiteKey(app *fiber.App, stub bool, siteKey string) { func routeHCaptchaSiteKey(app *fiber.App, p string, stub bool, siteKey string) {
var resp string var resp string
if stub { if stub {
resp = mustConstResp(newMessage(false, "hCaptcha is not enabled on this instance.")) resp = mustConstResp(newMessage(false, "hCaptcha is not enabled on this instance."))
...@@ -20,7 +20,7 @@ func routeHCaptchaSiteKey(app *fiber.App, stub bool, siteKey string) { ...@@ -20,7 +20,7 @@ func routeHCaptchaSiteKey(app *fiber.App, stub bool, siteKey string) {
resp = mustConstResp(respHSiteKey{true, siteKey}) resp = mustConstResp(respHSiteKey{true, siteKey})
} }
app.Get("/captcha", func(c fiber.Ctx) error { app.Get(p, func(c fiber.Ctx) error {
c.Set("Content-Type", "application/json; charset=utf-8") c.Set("Content-Type", "application/json; charset=utf-8")
return c.SendString(resp) return c.SendString(resp)
}) })
......
...@@ -15,18 +15,16 @@ type registration struct { ...@@ -15,18 +15,16 @@ type registration struct {
} }
// Waitlist registration route // Waitlist registration route
func routeRegister(app *fiber.App, db *leveldb.DB, captcha fiber.Handler) { func routeRegister(app *fiber.App, p string, db *leveldb.DB, captcha fiber.Handler) {
app.Post("/register", func(c fiber.Ctx) error { app.Post(p, func(c fiber.Ctx) error {
req := new(registration) req := new(registration)
// Parse and validate the request // Parse and validate the request
if err := c.Bind().Body(req); err != nil { if err := c.Bind().Body(req); err != nil {
if verbose { if verbose {
log.Printf("invalid request from %q: %v", c.IP(), err) log.Printf("invalid json from %q: %v", c.IP(), err)
} }
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ return c.Status(fiber.StatusBadRequest).JSON(newMessage(false, "Invalid request"))
"message": "Invalid request",
})
} }
// Validate email format // Validate email format
...@@ -34,37 +32,27 @@ func routeRegister(app *fiber.App, db *leveldb.DB, captcha fiber.Handler) { ...@@ -34,37 +32,27 @@ func routeRegister(app *fiber.App, db *leveldb.DB, captcha fiber.Handler) {
if verbose { if verbose {
log.Printf("invalid email from %q: %s", c.IP(), req.Email) log.Printf("invalid email from %q: %s", c.IP(), req.Email)
} }
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ return c.Status(fiber.StatusBadRequest).JSON(newMessage(false, "Invalid email address"))
"message": "Invalid email format",
})
} }
// Check if email is already registered // Check if email is already registered
if ok, err := db.Has([]byte(req.Email), nil); err != nil { if ok, err := db.Has([]byte(req.Email), nil); err != nil {
log.Printf("cannot check for existence of email %q: %v", req.Email, err) log.Printf("cannot check for existence of email %q: %v", req.Email, err)
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ return c.Status(fiber.StatusInternalServerError).JSON(newMessage(false, "Cannot check registration status"))
"message": "Error checking registration status",
})
} else if ok { } else if ok {
if verbose { if verbose {
log.Printf("duplicate email from %q: %s", c.IP(), req.Email) log.Printf("duplicate email from %q: %s", c.IP(), req.Email)
} }
return c.Status(fiber.StatusConflict).JSON(fiber.Map{ return c.Status(fiber.StatusConflict).JSON(newMessage(false, "Email already registered"))
"message": "Email already registered",
})
} }
// Save the email to the waitlist // Save the email to the waitlist
if err := db.Put([]byte(req.Email), []byte{'x'}, nil); err != nil { if err := db.Put([]byte(req.Email), []byte{'x'}, nil); err != nil {
log.Printf("cannot register email %q: %v", req.Email, err) log.Printf("cannot register email %q: %v", req.Email, err)
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ return c.Status(fiber.StatusInternalServerError).JSON(newMessage(false, "Cannot register email"))
"message": "Error registering email",
})
} }
log.Printf("registered email %q", req.Email) log.Printf("registered email %q", req.Email)
return c.JSON(fiber.Map{ return c.JSON(newMessage(true, "Email registered successfully"))
"message": "Email registered successfully",
})
}, captcha) }, captcha)
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment