From 1667921f5b552eaf8efa508e4d1a5e7a25f6e153 Mon Sep 17 00:00:00 2001 From: ari melody Date: Sat, 9 Nov 2024 23:36:18 +0000 Subject: [PATCH] move DB credentials to environment variables --- README.md | 6 +++++- docker-compose.yml | 6 +++++- global/data.go | 2 +- main.go | 32 +++++++++++++++++++++++++++----- 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 7ac1702..64266ec 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,11 @@ easy! just `git clone` this repo and `go build` from the root. `arimelody-web(.e the webserver depends on some environment variables (don't worry about forgetting some; it'll be sure to bug you about them): -- `HTTP_DOMAIN`: the domain the webserver will use for generating oauth redirect URIs (default `https://arimelody.me`) +- `ARIMELODY_HTTP_DOMAIN`: the domain the webserver will use for generating oauth redirect URIs (default `https://arimelody.me`) +- `ARIMELODY_DB_HOST`: the host address of a postgres database. +- `ARIMELODY_DB_NAME`: the name of the database. +- `ARIMELODY_DB_USER`: the username for the database. +- `ARIMELODY_DB_PASS`: the password for the database. - `DISCORD_ADMIN`[^1]: the user ID of your discord account (discord auth is intended to be temporary, and will be replaced with its own auth system later) - `DISCORD_CLIENT`[^1]: the client ID of your discord OAuth application. - `DISCORD_SECRET`[^1]: the client secret of your discord OAuth application. diff --git a/docker-compose.yml b/docker-compose.yml index e29b006..3f3e4a4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,8 +7,12 @@ services: volumes: - ./uploads:/app/uploads environment: - HTTP_DOMAIN: "https://arimelody.me" + ARIMELODY_PORT: 8080 + ARIMELODY_HTTP_DOMAIN: "https://arimelody.me" ARIMELODY_DB_HOST: db + ARIMELODY_DB_NAME: arimelody + ARIMELODY_DB_USER: arimelody + ARIMELODY_DB_PASS: fuckingpassword DISCORD_ADMIN: # your discord user ID. DISCORD_CLIENT: # your discord OAuth client ID. DISCORD_SECRET: # your discord OAuth secret. diff --git a/global/data.go b/global/data.go index fb641fc..4f56a68 100644 --- a/global/data.go +++ b/global/data.go @@ -35,7 +35,7 @@ var Args = func() map[string]string { }() var HTTP_DOMAIN = func() string { - domain := os.Getenv("HTTP_DOMAIN") + domain := os.Getenv("ARIMELODY_HTTP_DOMAIN") if domain == "" { return "https://arimelody.me" } diff --git a/main.go b/main.go index f87d36e..a783421 100644 --- a/main.go +++ b/main.go @@ -7,27 +7,46 @@ import ( "net/http" "os" "path/filepath" + "strconv" "time" "arimelody-web/admin" "arimelody-web/api" "arimelody-web/global" - "arimelody-web/view" "arimelody-web/templates" + "arimelody-web/view" "github.com/jmoiron/sqlx" _ "github.com/lib/pq" ) -const DEFAULT_PORT int = 8080 +const DEFAULT_PORT int64 = 8080 func main() { // initialise database connection var dbHost = os.Getenv("ARIMELODY_DB_HOST") - if dbHost == "" { dbHost = "127.0.0.1" } + var dbName = os.Getenv("ARIMELODY_DB_NAME") + var dbUser = os.Getenv("ARIMELODY_DB_USER") + var dbPass = os.Getenv("ARIMELODY_DB_PASS") + if dbHost == "" { + fmt.Fprintf(os.Stderr, "FATAL: ARIMELODY_DB_HOST not provided! Exiting...\n") + os.Exit(1) + } + if dbName == "" { + fmt.Fprintf(os.Stderr, "FATAL: ARIMELODY_DB_NAME not provided! Exiting...\n") + os.Exit(1) + } + if dbUser == "" { + fmt.Fprintf(os.Stderr, "FATAL: ARIMELODY_DB_USER not provided! Exiting...\n") + os.Exit(1) + } + if dbPass == "" { + fmt.Fprintf(os.Stderr, "FATAL: ARIMELODY_DB_PASS not provided! Exiting...\n") + os.Exit(1) + } var err error - global.DB, err = sqlx.Connect("postgres", "host=" + dbHost + " user=arimelody dbname=arimelody password=fuckingpassword sslmode=disable") + global.DB, err = sqlx.Connect("postgres", fmt.Sprintf("host=%s user=%s dbname=%s password=%s sslmode=disable", dbHost, dbUser, dbName, dbPass)) if err != nil { fmt.Fprintf(os.Stderr, "FATAL: Unable to create database connection pool: %v\n", err) os.Exit(1) @@ -39,7 +58,10 @@ func main() { // start the web server! mux := createServeMux() - port := DEFAULT_PORT + port, err := strconv.ParseInt(os.Getenv("ARIMELODY_PORT"), 10, 0) + if err != nil { + port = DEFAULT_PORT + } fmt.Printf("Now serving at http://127.0.0.1:%d\n", port) log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), global.HTTPLog(mux))) }