move DB credentials to environment variables

This commit is contained in:
ari melody 2024-11-09 23:36:18 +00:00
parent 96cc64464f
commit 1667921f5b
Signed by: ari
GPG key ID: CF99829C92678188
4 changed files with 38 additions and 8 deletions

View file

@ -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.

View file

@ -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.

View file

@ -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"
}

32
main.go
View file

@ -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)))
}