summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSaumit Dinesan <justsaumit@protonmail.com>2023-11-28 20:48:41 +0530
committerSaumit Dinesan <justsaumit@protonmail.com>2023-11-28 20:48:41 +0530
commit9b0dc8e75b6e361e27414cde76d9a8958fe3dc1e (patch)
tree86c5768a0e60d881ad2bb40c37840c6a25758c87
parent0233b382c6cd207de5f2df49a8bf75e4c57b2416 (diff)
Integrating go dotenv for environment configuration and improve server setup
-rw-r--r--go.mod1
-rw-r--r--go.sum2
-rw-r--r--server.go40
3 files changed, 41 insertions, 2 deletions
diff --git a/go.mod b/go.mod
index f0c921e..2049064 100644
--- a/go.mod
+++ b/go.mod
@@ -3,6 +3,7 @@ module github.com/justsaumit/go-fis-api
go 1.21.4
require (
+ github.com/joho/godotenv v1.5.1
github.com/labstack/echo/v4 v4.11.3
github.com/mattn/go-sqlite3 v1.14.18
golang.org/x/crypto v0.16.0
diff --git a/go.sum b/go.sum
index 0dd1b3b..a08aae4 100644
--- a/go.sum
+++ b/go.sum
@@ -1,6 +1,8 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
+github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/labstack/echo/v4 v4.11.3 h1:Upyu3olaqSHkCjs1EJJwQ3WId8b8b1hxbogyommKktM=
github.com/labstack/echo/v4 v4.11.3/go.mod h1:UcGuQ8V6ZNRmSweBIJkPvGfwCMIlFmiqrPqiEBfPYws=
github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8=
diff --git a/server.go b/server.go
index 6720b41..b03b9b0 100644
--- a/server.go
+++ b/server.go
@@ -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))
+ }
}