summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBlaster4385 <venkatesh@tablaster.dev>2024-07-23 16:13:38 +0530
committerBlaster4385 <venkatesh@tablaster.dev>2024-07-25 17:15:57 +0530
commitf21cffacc308a9d43efca0185adce274d69e9d4d (patch)
tree04c0cf160c9a21f3d8cacf89966e9f783094792c
parent8fd9fc1ff951541808a86d8882715d06b03cdf30 (diff)
refactor: get required variables from env
- We are finally going to production
-rw-r--r--server/main.go17
1 files 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
}