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:
@@ -6,3 +6,4 @@ DB_HOST=auth_db
|
|||||||
DB_PORT=5432
|
DB_PORT=5432
|
||||||
DB_SSLMODE=disable
|
DB_SSLMODE=disable
|
||||||
ENABLE_REGISTRATION=true
|
ENABLE_REGISTRATION=true
|
||||||
|
ALLOWED_ORIGINS="http://textsender.com"
|
||||||
|
|||||||
@@ -6,3 +6,4 @@ DB_HOST=localhost
|
|||||||
DB_PORT=5432
|
DB_PORT=5432
|
||||||
DB_SSLMODE=disable
|
DB_SSLMODE=disable
|
||||||
ENABLE_REGISTRATION=true
|
ENABLE_REGISTRATION=true
|
||||||
|
ALLOWED_ORIGINS="http://textsender.com"
|
||||||
|
|||||||
@@ -107,6 +107,7 @@ jobs:
|
|||||||
echo "DB_PORT=$DB_PORT" >> .env
|
echo "DB_PORT=$DB_PORT" >> .env
|
||||||
echo "DB_SSLMODE=$DB_SSLMODE" >> .env
|
echo "DB_SSLMODE=$DB_SSLMODE" >> .env
|
||||||
echo "ENABLE_REGISTRATION=true" >> .env
|
echo "ENABLE_REGISTRATION=true" >> .env
|
||||||
|
echo "ALLOWED_ORIGINS=http://localhost:9080" >> .env
|
||||||
|
|
||||||
echo "Initializing config"
|
echo "Initializing config"
|
||||||
mkdir -p ~/.ssh
|
mkdir -p ~/.ssh
|
||||||
|
|||||||
+1
-1
@@ -85,7 +85,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"},
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ type Config struct {
|
|||||||
ServerPort string
|
ServerPort string
|
||||||
ResetDB bool
|
ResetDB bool
|
||||||
EnableRegistration bool
|
EnableRegistration bool
|
||||||
|
AllowedOrigins []string
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConnectionInfo struct {
|
type ConnectionInfo struct {
|
||||||
@@ -67,12 +68,23 @@ func Load() *Config {
|
|||||||
|
|
||||||
unpackedConnString := UnpackDBConnString()
|
unpackedConnString := UnpackDBConnString()
|
||||||
dbConnString := unpackedConnString.Parse()
|
dbConnString := unpackedConnString.Parse()
|
||||||
|
allowedOrigins := unpackAllowedOrigin()
|
||||||
|
|
||||||
return &Config{
|
if len(allowedOrigins) > 0 {
|
||||||
DBConnString: dbConnString,
|
return &Config{
|
||||||
ServerPort: *port,
|
DBConnString: dbConnString,
|
||||||
ResetDB: *resetDb,
|
ServerPort: *port,
|
||||||
EnableRegistration: CheckRegistration(),
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func unpackAllowedOrigin() []string {
|
||||||
|
allowedOriginsRaw := os.Getenv("ALLOWED_ORIGINS")
|
||||||
|
allowedOriginsSplit := strings.Split(allowedOriginsRaw, ",")
|
||||||
|
return allowedOriginsSplit
|
||||||
|
}
|
||||||
|
|
||||||
func CheckRegistration() bool {
|
func CheckRegistration() bool {
|
||||||
if flagValue := os.Getenv("ENABLE_REGISTRATION"); flagValue != "" {
|
if flagValue := os.Getenv("ENABLE_REGISTRATION"); flagValue != "" {
|
||||||
if val := strings.ToUpper(flagValue); val == "TRUE" {
|
if val := strings.ToUpper(flagValue); val == "TRUE" {
|
||||||
|
|||||||
Reference in New Issue
Block a user