tsk-4: Adding CORS support (#53)

Closes #4

Reviewed-on: phoenix/textsender-api#53
Co-authored-by: phoenix <kundeng00@pm.me>
Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
phoenix
2025-12-21 00:28:17 +00:00
committed by phoenix
parent 69b5f4ff89
commit f5f753fe02
5 changed files with 14 additions and 1 deletions
+1
View File
@@ -9,3 +9,4 @@ TWILIO_AUTH_SID=9M438C93R943U4329MCU43C34U
TWILIO_SERVICE_SID=9M4J3X8439U398NUVT3342MC349C348T TWILIO_SERVICE_SID=9M4J3X8439U398NUVT3342MC349C348T
TWILIO_AUTH_TOKEN="f4a1f2b0b79ea3735078c2d8ee9684e1" TWILIO_AUTH_TOKEN="f4a1f2b0b79ea3735078c2d8ee9684e1"
TWILIO_PHONE_NUMBER=+10123456789 TWILIO_PHONE_NUMBER=+10123456789
ALLOWED_ORIGINS="http://textsender.com"
+1
View File
@@ -9,3 +9,4 @@ TWILIO_AUTH_SID=9M438C93R943U4329MCU43C34U
TWILIO_SERVICE_SID=9M4J3X8439U398NUVT3342MC349C348T TWILIO_SERVICE_SID=9M4J3X8439U398NUVT3342MC349C348T
TWILIO_AUTH_TOKEN="f4a1f2b0b79ea3735078c2d8ee9684e1" TWILIO_AUTH_TOKEN="f4a1f2b0b79ea3735078c2d8ee9684e1"
TWILIO_PHONE_NUMBER=+10123456789 TWILIO_PHONE_NUMBER=+10123456789
ALLOWED_ORIGINS="http://textsender.com"
+1
View File
@@ -107,6 +107,7 @@ jobs:
echo "TWILIO_SERVICE_SID=9M4J3X8439U398NUVT3342MC349C348T" >> .env echo "TWILIO_SERVICE_SID=9M4J3X8439U398NUVT3342MC349C348T" >> .env
echo "TWILIO_AUTH_TOKEN=f4a1f2b0b79ea3735078c2d8ee9684e1" >> .env echo "TWILIO_AUTH_TOKEN=f4a1f2b0b79ea3735078c2d8ee9684e1" >> .env
echo "TWILIO_PHONE_NUMBER=10123456789" >> .env echo "TWILIO_PHONE_NUMBER=10123456789" >> .env
echo "ALLOWED_ORIGINS=http://localhost:5173" >> .env
echo "Initializing config" echo "Initializing config"
mkdir -p ~/.ssh mkdir -p ~/.ssh
+1 -1
View File
@@ -95,7 +95,7 @@ func main() {
// Configure CORS // Configure CORS
router.Use(cors.Handler(cors.Options{ router.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{fmt.Sprintf("http://localhost:%s", config.PORT), "http://localhost:5173", "https://textsender.com"}, AllowedOrigins: cfg.AllowedOrigins,
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"}, AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"}, AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
ExposedHeaders: []string{"Link", "X-Total-Count"}, ExposedHeaders: []string{"Link", "X-Total-Count"},
+10
View File
@@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"os" "os"
"strconv" "strconv"
"strings"
"git.kundeng.us/phoenix/textsender-models/tx0/config" "git.kundeng.us/phoenix/textsender-models/tx0/config"
"github.com/joho/godotenv" "github.com/joho/godotenv"
@@ -18,6 +19,7 @@ type Config struct {
ResetDB bool ResetDB bool
JWTSecret string `env:"JWT_SECRET" required:"true"` JWTSecret string `env:"JWT_SECRET" required:"true"`
TwilioConfig *config.TwiloConfig TwilioConfig *config.TwiloConfig
AllowedOrigins []string
} }
type ConnectionInfo struct { type ConnectionInfo struct {
@@ -68,11 +70,13 @@ func Load() (*Config, *config.TwiloConfig, error) {
if cfg, err := TwilioConfig(); err != nil { if cfg, err := TwilioConfig(); err != nil {
return nil, nil, err return nil, nil, err
} else { } else {
allowedOrigins := unpackAllowedOrigins()
return &Config{ return &Config{
DBConnString: dbConnString, DBConnString: dbConnString,
ServerPort: *port, ServerPort: *port,
ResetDB: *resetDb, ResetDB: *resetDb,
JWTSecret: os.Getenv("JWT_SECRET"), JWTSecret: os.Getenv("JWT_SECRET"),
AllowedOrigins: allowedOrigins,
}, cfg, nil }, cfg, nil
} }
} }
@@ -128,6 +132,12 @@ func UnpackDBConnString() (connInfo ConnectionInfo) {
return return
} }
func unpackAllowedOrigins() []string {
allowedOriginsRaw := os.Getenv("ALLOWED_ORIGINS")
allowedOriginsSplit := strings.Split(allowedOriginsRaw, ",")
return allowedOriginsSplit
}
func TwilioConfig() (*config.TwiloConfig, error) { func TwilioConfig() (*config.TwiloConfig, error) {
authSid := os.Getenv("TWILIO_AUTH_SID") authSid := os.Getenv("TWILIO_AUTH_SID")
serviceSid := os.Getenv("TWILIO_SERVICE_SID") serviceSid := os.Getenv("TWILIO_SERVICE_SID")