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

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