Skip to content

Commit ce85827

Browse files
committed
[Fix] Fix the environment config and PORT number
Signed-off-by: Hsiu-Chi Liu (Tomlord) <aa123593465@gmail.com>
1 parent 1c6249c commit ce85827

6 files changed

Lines changed: 67 additions & 10 deletions

File tree

cmd/template/dbdriver/files/service/postgres.tmpl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"time"
1010

1111
"github.com/jackc/pgx/v5/pgxpool"
12-
_ "github.com/joho/godotenv/autoload"
1312
)
1413

1514
// Service represents a service that interacts with a database.
@@ -33,7 +32,7 @@ var (
3332
username = os.Getenv("SYMPHONY_DB_USERNAME")
3433
port = os.Getenv("SYMPHONY_DB_PORT")
3534
host = os.Getenv("SYMPHONY_DB_HOST")
36-
schema = os.Getenv("SYMPHONY_DB_SCHEMA")
35+
schema = os.Getenv("SYMPHONY_DB_SCHEMA")
3736
dbInstance *service
3837
)
3938

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
PORT=8080
22
APP_ENV=local
3+
FRONT_END_URL=http://localhost:5173

cmd/template/framework/files/main/main.go.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"os/signal"
99
"syscall"
1010
"time"
11-
11+
_ "github.com/joho/godotenv/autoload"
1212
"{{.ProjectName}}/internal/server"
1313
)
1414

cmd/template/framework/files/routes/gin.go.tmpl

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package server
22

33
import (
44
"net/http"
5+
"os"
56
{{if .AdvancedOptions.websocket}}
67
"log"
78
"fmt"
@@ -15,10 +16,37 @@ import (
1516
)
1617

1718
func (s *Server) RegisterRoutes() http.Handler {
19+
appEnv := os.Getenv("APP_ENV")
20+
if appEnv == "" {
21+
appEnv = "local"
22+
}
23+
24+
// Set Gin mode based on environment
25+
if appEnv == "production" {
26+
gin.SetMode(gin.ReleaseMode)
27+
} else {
28+
gin.SetMode(gin.DebugMode)
29+
}
30+
1831
r := gin.Default()
1932

33+
// Configure CORS based on environment
34+
var allowedOrigins []string
35+
frontendURL := os.Getenv("FRONT_END_URL")
36+
if frontendURL == "" {
37+
frontendURL = "http://localhost:5173" // Default fallback
38+
}
39+
40+
if appEnv == "production" {
41+
// In production, use the configured frontend URL
42+
allowedOrigins = []string{frontendURL}
43+
} else {
44+
// In development, allow configured frontend URL plus common development servers
45+
allowedOrigins = []string{frontendURL, "http://localhost:3000", "http://localhost:8080"}
46+
}
47+
2048
r.Use(cors.New(cors.Config{
21-
AllowOrigins: []string{"http://localhost:5173", "http://localhost:3000"}, // Add your frontend URL
49+
AllowOrigins: allowedOrigins,
2250
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"},
2351
AllowHeaders: []string{"Accept", "Authorization", "Content-Type"},
2452
AllowCredentials: true, // Enable cookies/auth

cmd/template/framework/files/server/gin.go.tmpl

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package server
22

33
import (
44
"fmt"
5+
"log"
56
"net/http"
67
"os"
78
"strconv"
@@ -10,7 +11,6 @@ import (
1011
{{- if ne .DBDriver "none" }}
1112
"{{.ProjectName}}/internal/database"
1213
{{- end }}
13-
_ "github.com/joho/godotenv/autoload"
1414
)
1515

1616
type Server struct {
@@ -22,20 +22,40 @@ type Server struct {
2222

2323
func NewServer() *http.Server {
2424
port, _ := strconv.Atoi(os.Getenv("PORT"))
25+
appEnv := os.Getenv("APP_ENV")
26+
if appEnv == "" {
27+
appEnv = "local"
28+
}
29+
30+
// Log the current environment
31+
log.Printf("Starting server in %s environment", appEnv)
32+
2533
NewServer := &Server{
2634
port: port,
2735
{{- if ne .DBDriver "none" }}
2836
db: database.New(),
2937
{{- end }}
3038
}
3139

40+
// Configure timeouts based on environment
41+
var readTimeout, writeTimeout, idleTimeout time.Duration
42+
if appEnv == "production" {
43+
readTimeout = 30 * time.Second
44+
writeTimeout = 60 * time.Second
45+
idleTimeout = 5 * time.Minute
46+
} else {
47+
readTimeout = 10 * time.Second
48+
writeTimeout = 30 * time.Second
49+
idleTimeout = time.Minute
50+
}
51+
3252
// Declare Server config
3353
server := &http.Server{
3454
Addr: fmt.Sprintf(":%d", NewServer.port),
3555
Handler: NewServer.RegisterRoutes(),
36-
IdleTimeout: time.Minute,
37-
ReadTimeout: 10 * time.Second,
38-
WriteTimeout: 30 * time.Second,
56+
IdleTimeout: idleTimeout,
57+
ReadTimeout: readTimeout,
58+
WriteTimeout: writeTimeout,
3959
}
4060

4161
return server

cmd/template/supabase/supabase.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package supabase
22

33
import (
4+
"bytes"
45
_ "embed"
56
"fmt"
67
"io"
@@ -11,6 +12,7 @@ import (
1112
"text/template"
1213

1314
"github.com/Tomlord1122/go-symphony/cmd/flags"
15+
tpl "github.com/Tomlord1122/go-symphony/cmd/template"
1416
)
1517

1618
//go:embed files/supabase_env.tmpl
@@ -133,7 +135,7 @@ func (sm *SupabaseManager) GetMigrationPath() string {
133135
return filepath.Join(sm.ProjectPath, "supabase", "migrations")
134136
}
135137

136-
// GenerateSupabaseEnv creates .env file with Supabase configuration
138+
// GenerateSupabaseEnv creates .env file with Supabase configuration including global environment variables
137139
func (sm *SupabaseManager) GenerateSupabaseEnv() error {
138140
envPath := filepath.Join(sm.ProjectPath, ".env")
139141

@@ -143,7 +145,14 @@ func (sm *SupabaseManager) GenerateSupabaseEnv() error {
143145
}
144146
defer file.Close()
145147

146-
_, err = file.Write(SupabaseEnvTemplate)
148+
// Combine global environment template with Supabase-specific template
149+
envBytes := [][]byte{
150+
tpl.GlobalEnvTemplate(),
151+
SupabaseEnvTemplate,
152+
}
153+
154+
combinedTemplate := bytes.Join(envBytes, []byte("\n"))
155+
_, err = file.Write(combinedTemplate)
147156
if err != nil {
148157
return fmt.Errorf("failed to write .env file: %w", err)
149158
}

0 commit comments

Comments
 (0)