diff options
author | Saumit Dinesan <justsaumit@protonmail.com> | 2023-12-25 16:16:11 +0530 |
---|---|---|
committer | Saumit Dinesan <justsaumit@protonmail.com> | 2023-12-25 16:16:11 +0530 |
commit | 704d51df9a371f39c3137fcf7dcfc164b3150983 (patch) | |
tree | 3bc8098ca62b11e1d0d7adb778934bb88ee3e8c8 /server.go | |
parent | e41148638c2783fae9e7756c04781d59d83e3444 (diff) |
server.go & env.example: remove unused variables, explicit checks for cert and key path in prod environment
Diffstat (limited to 'server.go')
-rw-r--r-- | server.go | 46 |
1 files changed, 25 insertions, 21 deletions
@@ -10,28 +10,17 @@ import ( ) const ( - defaultPort = "3000" - defaultDomain = "localhost" - defaultAPIEndpointURL = "http://localhost:3000" + defaultPort = "3000" ) func main() { - err := godotenv.Load() // Load .env file - if err != nil { - log.Fatal("Error loading .env file", err) - } + + godotenv.Load() port := os.Getenv("PORT") if port == "" { port = defaultPort - } - - domain := os.Getenv("DOMAIN") - api_endpoint_url := os.Getenv("API_ENDPOINT_URL") - if domain == "" || api_endpoint_url == "" { - log.Println("Warning: DOMAIN and API_ENDPOINT_URL environment variable not set. Using defaults") - domain = defaultDomain - api_endpoint_url = defaultAPIEndpointURL + log.Println("No port specified; running on the default port 3000") } e := echo.New() @@ -40,14 +29,29 @@ func main() { environment := os.Getenv("ENVIRONMENT") switch environment { - case "development": - e.Logger.Fatal(e.Start(":" + port)) case "production": - certPath := os.Getenv("CERTPATH") - keyPath := os.Getenv("KEYPATH") - e.Logger.Fatal(e.StartTLS(":"+port, certPath, keyPath)) + startProductionServer(e, port) + case "development": + startDevelopmentServer(e, port) default: - log.Printf("Unknown environment '%s', starting on default port %s\n", environment, port) + log.Printf("Unknown environment '%s', running without TLS encryption\n", environment) e.Logger.Fatal(e.Start(":" + port)) } } + +func startProductionServer(e *echo.Echo, port string) { + certPath := os.Getenv("CERTPATH") + keyPath := os.Getenv("KEYPATH") + + if certPath == "" || keyPath == "" { + log.Fatal("Certificate or key path not provided for production environment") + } + + log.Printf("Starting production server on port %s\n", port) + e.Logger.Fatal(e.StartTLS(":"+port, certPath, keyPath)) +} + +func startDevelopmentServer(e *echo.Echo, port string) { + log.Printf("Starting development server on port %s\n", port) + e.Logger.Fatal(e.Start(":" + port)) +} |