Fix /api/waitlist API (#726)

* move /waitlist to /api/waitlist where its expected

* parse waitlist API request as JSON, duh
zio/stable
Jake Gold 2023-05-18 11:56:09 -07:00 committed by GitHub
parent 0ca096138a
commit 211fce47ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 30 additions and 3 deletions

View File

@ -2,8 +2,10 @@ package main
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"io/fs" "io/fs"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"strings" "strings"
@ -132,11 +134,36 @@ func serve(cctx *cli.Context) error {
e.GET("/profile/:handle/post/:rkey/reposted-by", server.WebGeneric) e.GET("/profile/:handle/post/:rkey/reposted-by", server.WebGeneric)
// Mailmodo // Mailmodo
e.POST("/waitlist", func(c echo.Context) error { e.POST("/api/waitlist", func(c echo.Context) error {
email := strings.TrimSpace(c.FormValue("email")) type jsonError struct {
if err := mailmodo.AddToList(c.Request().Context(), mailmodoListName, email); err != nil { Error string `json:"error"`
}
// Read the API request.
type apiRequest struct {
Email string `json:"email"`
}
bodyReader := http.MaxBytesReader(c.Response(), c.Request().Body, 16*1024)
payload, err := ioutil.ReadAll(bodyReader)
if err != nil {
return err return err
} }
var req apiRequest
if err := json.Unmarshal(payload, &req); err != nil {
return c.JSON(http.StatusBadRequest, jsonError{Error: "Invalid API request"})
}
if req.Email == "" {
return c.JSON(http.StatusBadRequest, jsonError{Error: "Please enter a valid email address."})
}
if err := mailmodo.AddToList(c.Request().Context(), mailmodoListName, req.Email); err != nil {
log.Errorf("adding email to waitlist failed: %s", err)
return c.JSON(http.StatusBadRequest, jsonError{
Error: "Storing email in waitlist failed. Please enter a valid email address.",
})
}
return c.JSON(http.StatusOK, map[string]bool{"success": true}) return c.JSON(http.StatusOK, map[string]bool{"success": true})
}) })