From c3424c1bb3f2ad3f833e317bcb499b4799722b48 Mon Sep 17 00:00:00 2001 From: jabuxas Date: Thu, 17 Oct 2024 15:26:13 -0300 Subject: [PATCH] refactor: set values on Application creation --- abyss.go | 36 ++++++++++++++++++++---------------- handlers.go | 2 +- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/abyss.go b/abyss.go index 32b8ae9..8feca9e 100644 --- a/abyss.go +++ b/abyss.go @@ -11,7 +11,25 @@ import ( ) func main() { - app := new(Application) + err := godotenv.Load() + if err != nil { + slog.Warn("no .env file detected, getting env from running process") + } + + app := &Application{ + auth: struct { + username string + password string + }{ + username: os.Getenv("AUTH_USERNAME"), + password: os.Getenv("AUTH_PASSWORD"), + }, + url: os.Getenv("ABYSS_URL"), + key: os.Getenv("UPLOAD_KEY"), + filesDir: os.Getenv("ABYSS_FILEDIR"), + port: os.Getenv("ABYSS_PORT"), + authUpload: os.Getenv("SHOULD_AUTH"), + } parseEnv(app) @@ -35,20 +53,6 @@ func main() { } func parseEnv(app *Application) { - err := godotenv.Load() - if err != nil { - slog.Warn("no .env file detected, getting env from running process") - } - - app.auth.username = os.Getenv("AUTH_USERNAME") - app.auth.password = os.Getenv("AUTH_PASSWORD") - app.url = os.Getenv("ABYSS_URL") - app.key = os.Getenv("UPLOAD_KEY") - app.filesDir = os.Getenv("ABYSS_FILEDIR") - app.port = os.Getenv("ABYSS_PORT") - - app.authText = os.Getenv("SHOULD_AUTH") - if app.auth.username == "" { log.Fatal("basic auth username must be provided") } @@ -97,7 +101,7 @@ func setupHandlers(mux *http.ServeMux, app *Application) { mux.HandleFunc("/files/", app.fileHandler) - if app.authText == "yes" { + if app.authUpload == "yes" { mux.HandleFunc("/upload", BasicAuth(app.uploadHandler, app)) slog.Warn("text uploading through the browser will be restricted") } else { diff --git a/handlers.go b/handlers.go index 6b0d105..ef6e53e 100644 --- a/handlers.go +++ b/handlers.go @@ -23,7 +23,7 @@ type Application struct { key string filesDir string port string - authText string + authUpload string lastUploadedFile string }