@@ -2,6 +2,7 @@ package server
22
33import (
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
1616type Server struct {
@@ -22,20 +22,40 @@ type Server struct {
2222
2323func 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
0 commit comments