bskyweb: remove waitlist email endpoint (#3127)
parent
f61d1e1f94
commit
7a592d8140
|
@ -1,70 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"context"
|
|
||||||
"crypto/sha256"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Mailmodo struct {
|
|
||||||
httpClient *http.Client
|
|
||||||
APIKey string
|
|
||||||
BaseURL string
|
|
||||||
ListName string
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewMailmodo(apiKey, listName string) *Mailmodo {
|
|
||||||
return &Mailmodo{
|
|
||||||
APIKey: apiKey,
|
|
||||||
BaseURL: "https://api.mailmodo.com/api/v1",
|
|
||||||
httpClient: &http.Client{},
|
|
||||||
ListName: listName,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Mailmodo) request(ctx context.Context, httpMethod string, apiMethod string, data any) error {
|
|
||||||
endpoint := fmt.Sprintf("%s/%s", m.BaseURL, apiMethod)
|
|
||||||
js, err := json.Marshal(data)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Mailmodo JSON encoding failed: %w", err)
|
|
||||||
}
|
|
||||||
req, err := http.NewRequestWithContext(ctx, httpMethod, endpoint, bytes.NewBuffer(js))
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Mailmodo HTTP creating request %s %s failed: %w", httpMethod, apiMethod, err)
|
|
||||||
}
|
|
||||||
req.Header.Set("mmApiKey", m.APIKey)
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
|
||||||
|
|
||||||
res, err := m.httpClient.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Mailmodo HTTP making request %s %s failed: %w", httpMethod, apiMethod, err)
|
|
||||||
}
|
|
||||||
defer res.Body.Close()
|
|
||||||
|
|
||||||
status := struct {
|
|
||||||
Success bool `json:"success"`
|
|
||||||
Message string `json:"message"`
|
|
||||||
}{}
|
|
||||||
if err := json.NewDecoder(res.Body).Decode(&status); err != nil {
|
|
||||||
return fmt.Errorf("Mailmodo HTTP parsing response %s %s failed: %w", httpMethod, apiMethod, err)
|
|
||||||
}
|
|
||||||
if !status.Success {
|
|
||||||
return fmt.Errorf("Mailmodo API response %s %s failed: %s", httpMethod, apiMethod, status.Message)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Mailmodo) AddToList(ctx context.Context, email string) error {
|
|
||||||
return m.request(ctx, "POST", "addToList", map[string]any{
|
|
||||||
"listName": m.ListName,
|
|
||||||
"email": email,
|
|
||||||
"data": map[string]any{
|
|
||||||
"email_hashed": fmt.Sprintf("%x", sha256.Sum256([]byte(email))),
|
|
||||||
},
|
|
||||||
"created_at": time.Now().UTC().Format(time.RFC3339),
|
|
||||||
})
|
|
||||||
}
|
|
|
@ -40,18 +40,6 @@ func run(args []string) {
|
||||||
// retain old PDS env var for easy transition
|
// retain old PDS env var for easy transition
|
||||||
EnvVars: []string{"ATP_APPVIEW_HOST", "ATP_PDS_HOST"},
|
EnvVars: []string{"ATP_APPVIEW_HOST", "ATP_PDS_HOST"},
|
||||||
},
|
},
|
||||||
&cli.StringFlag{
|
|
||||||
Name: "mailmodo-api-key",
|
|
||||||
Usage: "Mailmodo API key",
|
|
||||||
Required: false,
|
|
||||||
EnvVars: []string{"MAILMODO_API_KEY"},
|
|
||||||
},
|
|
||||||
&cli.StringFlag{
|
|
||||||
Name: "mailmodo-list-name",
|
|
||||||
Usage: "Mailmodo contact list to add email addresses to",
|
|
||||||
Required: false,
|
|
||||||
EnvVars: []string{"MAILMODO_LIST_NAME"},
|
|
||||||
},
|
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "http-address",
|
Name: "http-address",
|
||||||
Usage: "Specify the local IP/port to bind to",
|
Usage: "Specify the local IP/port to bind to",
|
||||||
|
|
|
@ -2,11 +2,9 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
@ -29,25 +27,19 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
echo *echo.Echo
|
echo *echo.Echo
|
||||||
httpd *http.Server
|
httpd *http.Server
|
||||||
mailmodo *Mailmodo
|
xrpcc *xrpc.Client
|
||||||
xrpcc *xrpc.Client
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func serve(cctx *cli.Context) error {
|
func serve(cctx *cli.Context) error {
|
||||||
debug := cctx.Bool("debug")
|
debug := cctx.Bool("debug")
|
||||||
httpAddress := cctx.String("http-address")
|
httpAddress := cctx.String("http-address")
|
||||||
appviewHost := cctx.String("appview-host")
|
appviewHost := cctx.String("appview-host")
|
||||||
mailmodoAPIKey := cctx.String("mailmodo-api-key")
|
|
||||||
mailmodoListName := cctx.String("mailmodo-list-name")
|
|
||||||
|
|
||||||
// Echo
|
// Echo
|
||||||
e := echo.New()
|
e := echo.New()
|
||||||
|
|
||||||
// Mailmodo client.
|
|
||||||
mailmodo := NewMailmodo(mailmodoAPIKey, mailmodoListName)
|
|
||||||
|
|
||||||
// create a new session (no auth)
|
// create a new session (no auth)
|
||||||
xrpcc := &xrpc.Client{
|
xrpcc := &xrpc.Client{
|
||||||
Client: cliutil.NewHttpClient(),
|
Client: cliutil.NewHttpClient(),
|
||||||
|
@ -77,9 +69,8 @@ func serve(cctx *cli.Context) error {
|
||||||
// server
|
// server
|
||||||
//
|
//
|
||||||
server := &Server{
|
server := &Server{
|
||||||
echo: e,
|
echo: e,
|
||||||
mailmodo: mailmodo,
|
xrpcc: xrpcc,
|
||||||
xrpcc: xrpcc,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the HTTP server.
|
// Create the HTTP server.
|
||||||
|
@ -221,9 +212,6 @@ func serve(cctx *cli.Context) error {
|
||||||
e.GET("/profile/:handleOrDID/post/:rkey/liked-by", server.WebGeneric)
|
e.GET("/profile/:handleOrDID/post/:rkey/liked-by", server.WebGeneric)
|
||||||
e.GET("/profile/:handleOrDID/post/:rkey/reposted-by", server.WebGeneric)
|
e.GET("/profile/:handleOrDID/post/:rkey/reposted-by", server.WebGeneric)
|
||||||
|
|
||||||
// Mailmodo
|
|
||||||
e.POST("/api/waitlist", server.apiWaitlist)
|
|
||||||
|
|
||||||
// Start the server.
|
// Start the server.
|
||||||
log.Infof("starting server address=%s", httpAddress)
|
log.Infof("starting server address=%s", httpAddress)
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -398,36 +386,3 @@ func (srv *Server) WebProfile(c echo.Context) error {
|
||||||
data["requestHost"] = req.Host
|
data["requestHost"] = req.Host
|
||||||
return c.Render(http.StatusOK, "profile.html", data)
|
return c.Render(http.StatusOK, "profile.html", data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srv *Server) apiWaitlist(c echo.Context) error {
|
|
||||||
type jsonError struct {
|
|
||||||
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
|
|
||||||
}
|
|
||||||
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 := srv.mailmodo.AddToList(c.Request().Context(), 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})
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue