From f21cffacc308a9d43efca0185adce274d69e9d4d Mon Sep 17 00:00:00 2001 From: Blaster4385 Date: Tue, 23 Jul 2024 16:13:38 +0530 Subject: refactor: get required variables from env - We are finally going to production --- server/main.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/server/main.go b/server/main.go index ad79bcf..8060b41 100644 --- a/server/main.go +++ b/server/main.go @@ -9,9 +9,11 @@ import ( "embed" "encoding/hex" "errors" + "flag" "fmt" "io" "net/http" + "os" "time" "github.com/labstack/echo/v4" @@ -27,6 +29,8 @@ const ( var db *sql.DB +var port string + //go:embed all:dist var dist embed.FS @@ -47,6 +51,8 @@ func registerHandlers(e *echo.Echo) { } func main() { + flag.StringVar(&port, "port", "8080", "HTTP server port") + flag.Parse() var err error db, err = initDB() if err != nil { @@ -56,11 +62,18 @@ func main() { e := echo.New() registerHandlers(e) - e.Logger.Fatal(e.Start(":8080")) + e.Logger.Fatal(e.Start(":" + port)) } func initDB() (*sql.DB, error) { - db, err := sql.Open("postgres", "postgres://file:password@localhost/filedb?sslmode=disable") + user := os.Getenv("POSTGRES_USER") + password := os.Getenv("POSTGRES_PASSWORD") + dbname := os.Getenv("POSTGRES_DB") + + dbURL := fmt.Sprintf("postgres://%s:%s@localhost/%s?sslmode=disable", user, password, dbname) + // print dbURL for debugging + fmt.Println(dbURL) + db, err := sql.Open("postgres", dbURL) if err != nil { return nil, err } -- cgit v1.2.3-73-gaa49b