tsk-10: CORS support (#28)

Closes #10

Reviewed-on: phoenix/textsender-auth#28
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:08 +00:00
committed by phoenix
parent 223b7919f9
commit 636b0280c7
5 changed files with 27 additions and 6 deletions
+1
View File
@@ -6,3 +6,4 @@ DB_HOST=auth_db
DB_PORT=5432
DB_SSLMODE=disable
ENABLE_REGISTRATION=true
ALLOWED_ORIGINS="http://textsender.com"
+1
View File
@@ -6,3 +6,4 @@ DB_HOST=localhost
DB_PORT=5432
DB_SSLMODE=disable
ENABLE_REGISTRATION=true
ALLOWED_ORIGINS="http://textsender.com"
+1
View File
@@ -107,6 +107,7 @@ jobs:
echo "DB_PORT=$DB_PORT" >> .env
echo "DB_SSLMODE=$DB_SSLMODE" >> .env
echo "ENABLE_REGISTRATION=true" >> .env
echo "ALLOWED_ORIGINS=http://localhost:9080" >> .env
echo "Initializing config"
mkdir -p ~/.ssh
+1 -1
View File
@@ -85,7 +85,7 @@ func main() {
// Configure CORS
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"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
ExposedHeaders: []string{"Link", "X-Total-Count"},
+23 -5
View File
@@ -19,6 +19,7 @@ type Config struct {
ServerPort string
ResetDB bool
EnableRegistration bool
AllowedOrigins []string
}
type ConnectionInfo struct {
@@ -67,12 +68,23 @@ func Load() *Config {
unpackedConnString := UnpackDBConnString()
dbConnString := unpackedConnString.Parse()
allowedOrigins := unpackAllowedOrigin()
return &Config{
DBConnString: dbConnString,
ServerPort: *port,
ResetDB: *resetDb,
EnableRegistration: CheckRegistration(),
if len(allowedOrigins) > 0 {
return &Config{
DBConnString: dbConnString,
ServerPort: *port,
ResetDB: *resetDb,
EnableRegistration: CheckRegistration(),
AllowedOrigins: allowedOrigins,
}
} else {
return &Config{
DBConnString: dbConnString,
ServerPort: *port,
ResetDB: *resetDb,
EnableRegistration: CheckRegistration(),
}
}
}
@@ -131,6 +143,12 @@ func UnpackDBConnString() (connInfo ConnectionInfo) {
return
}
func unpackAllowedOrigin() []string {
allowedOriginsRaw := os.Getenv("ALLOWED_ORIGINS")
allowedOriginsSplit := strings.Split(allowedOriginsRaw, ",")
return allowedOriginsSplit
}
func CheckRegistration() bool {
if flagValue := os.Getenv("ENABLE_REGISTRATION"); flagValue != "" {
if val := strings.ToUpper(flagValue); val == "TRUE" {