diff options
author | Saumit Dinesan <justsaumit@protonmail.com> | 2023-11-28 20:48:41 +0530 |
---|---|---|
committer | Saumit Dinesan <justsaumit@protonmail.com> | 2023-11-28 20:48:41 +0530 |
commit | 9b0dc8e75b6e361e27414cde76d9a8958fe3dc1e (patch) | |
tree | 86c5768a0e60d881ad2bb40c37840c6a25758c87 /server.go | |
parent | 0233b382c6cd207de5f2df49a8bf75e4c57b2416 (diff) |
Integrating go dotenv for environment configuration and improve server setup
Diffstat (limited to 'server.go')
-rw-r--r-- | server.go | 40 |
1 files changed, 38 insertions, 2 deletions
@@ -1,14 +1,50 @@ package main import ( + "github.com/joho/godotenv" "github.com/justsaumit/go-fis-api/handlers" "github.com/labstack/echo/v4" + "log" + "fmt" + "os" ) func main() { + err := godotenv.Load() // Load .env file + if err != nil { + log.Fatal("Error loading .env file") + } + + port := os.Getenv("PORT") + if port == "" { + port = "3000" // default port if not set + } + + 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 localhost as default.") + domain = "localhost" + api_endpoint_url = "http://localhost:3000" + } + fmt.Println("Domain: " + domain) + fmt.Println("API Endpoint URL: " + api_endpoint_url) + + certPath := "/etc/letsencrypt/live/" + domain + "/fullchain.pem" + keyPath := "/etc/letsencrypt/live/" + domain + "/privkey.pem" + e := echo.New() e.POST("/upload", handlers.AddHash) e.POST("/verify", handlers.VerifyHash) - //e.Logger.Fatal(e.Start(":3000")) - e.Logger.Fatal(e.StartTLS(":3000", "/etc/letsencrypt/live/draconyan.xyz/fullchain.pem", "/etc/letsencrypt/live/draconyan.xyz/privkey.pem")) + + environment := os.Getenv("ENVIRONMENT") + switch environment { + case "development": + e.Logger.Fatal(e.Start(":" + port)) + case "production": + e.Logger.Fatal(e.StartTLS(":"+port, certPath, keyPath)) + default: + log.Printf("Unknown environment '%s', starting on default port %s\n", environment, port) + e.Logger.Fatal(e.Start(":" + port)) + } } |