Automatically generate the .env file if it is not there

This commit is contained in:
Achim Rohn
2025-09-17 09:59:02 +02:00
parent fae6653f94
commit 0283634b98
5 changed files with 63 additions and 25 deletions
+51
View File
@@ -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
+8 -13
View File
@@ -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",
+2 -9
View File
@@ -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))
}
+1 -2
View File
@@ -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
+1 -1
View File
@@ -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