From 0283634b9854b293379c7bba889aec1b256a7694 Mon Sep 17 00:00:00 2001 From: Achim Rohn Date: Wed, 17 Sep 2025 09:59:02 +0200 Subject: [PATCH] Automatically generate the .env file if it is not there --- starter/.air.toml | 51 ++++++++++++++++++++++++++++++++++++++ starter/env/environment.go | 21 ++++++---------- starter/main.go | 11 ++------ starter/routes/routing.go | 3 +-- template.air.toml | 2 +- 5 files changed, 63 insertions(+), 25 deletions(-) create mode 100644 starter/.air.toml diff --git a/starter/.air.toml b/starter/.air.toml new file mode 100644 index 0000000..6c7b8c1 --- /dev/null +++ b/starter/.air.toml @@ -0,0 +1,51 @@ +root = "." +testdata_dir = "testdata" +tmp_dir = "tmp" + +[build] + args_bin = [] + bin = "./tmp/main" + cmd = "time go build -gcflags=\"all=-l\" -ldflags=\"-w -s\" -o ./tmp/main main.go" + delay = 1000 + exclude_dir = ["assets", "tmp", "vendor", "testdata", "cdk", "dist", "db", "static"] + exclude_file = [] + exclude_regex = ["_test.go"] + exclude_unchanged = false + follow_symlink = false + full_bin = "" + include_dir = [] + include_ext = ["go", "tpl", "tmpl", "html", "gohtml", "css", "js"] + include_file = [] + kill_delay = "0s" + log = "build-errors.log" + poll = false + poll_interval = 0 + post_cmd = [] + pre_cmd = [] + rerun = false + rerun_delay = 500 + send_interrupt = false + stop_on_error = false + +[color] + app = "" + build = "yellow" + main = "magenta" + runner = "green" + watcher = "cyan" + +[log] + main_only = false + time = false + +[misc] + clean_on_exit = false + +[proxy] + app_port = 8090 + enabled = true + proxy_port = 8091 + +[screen] + clear_on_rebuild = false + keep_scroll = true diff --git a/starter/env/environment.go b/starter/env/environment.go index aa76782..e5170a3 100644 --- a/starter/env/environment.go +++ b/starter/env/environment.go @@ -10,7 +10,6 @@ import ( ) const ( - EnvKeyRepositoryType = "REPOSITORY_TYPE" EnvKeyDatabaseURL = "DATABASE_URL" EnvKeySessionSecret = "SESSION_SECRET" EnvKeyBaseURL = "BASE_URL" @@ -33,7 +32,6 @@ type Environment struct { DatabaseUrl string MistralApiKey string IsDev bool - RepositoryType string GoogleSheetsCredPath string GoogleSheetsSpreadId string GoogleSheetsSheetName string @@ -54,21 +52,21 @@ type KeycloakConfig struct { } func LoadEnvironment() Environment { + if _, err := os.Stat(".env"); err != nil { + err = GenerateEnvFile("", false) + if err != nil { + Error("error generating the environment file", err) + } + } err := godotenv.Load() if err != nil { LogError("Error loading .env file: %v", err) panic(err) } - // Default repository type is postgres if not specified - repoType := os.Getenv(EnvKeyRepositoryType) - if repoType == "" { - repoType = "postgres" - } return Environment{ DatabaseUrl: os.Getenv(EnvKeyDatabaseURL), IsDev: os.Getenv(EnvKeyIsDev) == "true" || os.Getenv(EnvKeyIsLocal) == "true", - RepositoryType: repoType, GoogleClientId: os.Getenv(EnvKeyGoogleClientID), GoogleClientSecret: os.Getenv(EnvKeyGoogleClientSecret), GoogleRedirectUrl: os.Getenv(EnvKeyGoogleRedirectURL), @@ -88,7 +86,6 @@ func LoadEnvironment() Environment { // without duplicating or hard-coding the list elsewhere. func EnvKeys() []string { return []string{ - EnvKeyRepositoryType, EnvKeyDatabaseURL, EnvKeySessionSecret, EnvKeyBaseURL, @@ -136,13 +133,11 @@ func GenerateEnvFile(rootPath string, overwrite bool) error { // Define default values and comments for specific keys defaults := map[string]string{ - EnvKeyRepositoryType: "postgres", - EnvKeyIsLocal: "false", - EnvKeyIsDev: "false", + EnvKeyIsLocal: "true", + EnvKeyIsDev: "true", } comments := map[string]string{ - EnvKeyRepositoryType: "# Repository type (postgres, sqlite, etc.)", EnvKeyDatabaseURL: "# Database connection URL", EnvKeySessionSecret: "# Secret key for session management", EnvKeyBaseURL: "# Base URL of the application", diff --git a/starter/main.go b/starter/main.go index b24fecc..12e9166 100644 --- a/starter/main.go +++ b/starter/main.go @@ -12,15 +12,8 @@ func main() { GlobalI18n = GlobalI18nImplementation{} environment := env.LoadEnvironment() - - db, err := CreatePostgresConnpool(environment.DatabaseUrl) - if err != nil { - Error("Failed to create database connection:", err) - panic(err) - } - defer db.Close() - + Debug(environment) Debug("starting white label app on port 8090") - handler := routes.CreateApi(db) + handler := routes.CreateApi() log.Fatal(http.ListenAndServe(":8090", handler)) } diff --git a/starter/routes/routing.go b/starter/routes/routing.go index 6866fb5..3945095 100644 --- a/starter/routes/routing.go +++ b/starter/routes/routing.go @@ -6,12 +6,11 @@ import ( "ersteller-lib/starter/page" "net/http" - "github.com/jackc/pgx/v5/pgxpool" . "maragu.dev/gomponents" . "maragu.dev/gomponents/html" ) -func CreateApi(db *pgxpool.Pool) http.Handler { +func CreateApi() http.Handler { server := NewHtmxServer() HtmxRouteDebugTrace = true diff --git a/template.air.toml b/template.air.toml index 930cd0a..6c7b8c1 100644 --- a/template.air.toml +++ b/template.air.toml @@ -7,7 +7,7 @@ tmp_dir = "tmp" bin = "./tmp/main" cmd = "time go build -gcflags=\"all=-l\" -ldflags=\"-w -s\" -o ./tmp/main main.go" delay = 1000 - exclude_dir = ["assets", "tmp", "vendor", "testdata", "cdk", "dist", "db"] + exclude_dir = ["assets", "tmp", "vendor", "testdata", "cdk", "dist", "db", "static"] exclude_file = [] exclude_regex = ["_test.go"] exclude_unchanged = false