diff --git a/bootstrap/.env b/bootstrap/.env deleted file mode 100644 index 80e3100..0000000 --- a/bootstrap/.env +++ /dev/null @@ -1,19 +0,0 @@ -# Application environment -# production -# development -APP_ENV = development - -# HTTP listen port of the application -HTTP_LISTEN_ADDR = :3000 - -# Database configuration -DB_DRIVER = sqlite3 -DB_USER = -DB_HOST = -DB_PASSWORD = -DB_NAME = app_db - -MIGRATION_DIR = app/db/migrations - -# Application secret for sessions (32 bytes long) -APP_SECRET = \ No newline at end of file diff --git a/bootstrap/.env.local b/bootstrap/.env.local index 51b4f7e..1701f9a 100644 --- a/bootstrap/.env.local +++ b/bootstrap/.env.local @@ -1,7 +1,6 @@ # Application environment -# production -# development -KIT_ENV = development +# production or development +APP_ENV = development # HTTP listen port of the application HTTP_LISTEN_ADDR = :3000 @@ -15,5 +14,10 @@ DB_NAME = app_db MIGRATION_DIR = app/db/migrations -# Application secret for sessions (32 bytes long) -APP_SECRET = \ No newline at end of file +# Application secret used to secure your sessions. +# The secret will be auto generated on install. +# If you still want to change it make sure its at +# least 32 bytes long. +# NOTE: You might want to change this secret when using +# your app in production. +APP_SECRET = {{app_secret}} \ No newline at end of file diff --git a/bootstrap/app/db/db.go b/bootstrap/app/db.go similarity index 79% rename from bootstrap/app/db/db.go rename to bootstrap/app/db.go index 0a93c1b..9b09070 100644 --- a/bootstrap/app/db/db.go +++ b/bootstrap/app/db.go @@ -1,4 +1,4 @@ -package db +package app import ( "log" @@ -13,7 +13,7 @@ import ( "github.com/uptrace/bun/extra/bundebug" ) -var Query *bun.DB +var DB *bun.DB func init() { config := db.Config{ @@ -27,8 +27,8 @@ func init() { if err != nil { log.Fatal(err) } - Query = bun.NewDB(db, sqlitedialect.New()) + DB = bun.NewDB(db, sqlitedialect.New()) if kit.IsDevelopment() { - Query.AddQueryHook(bundebug.NewQueryHook(bundebug.WithVerbose(true))) + DB.AddQueryHook(bundebug.NewQueryHook(bundebug.WithVerbose(true))) } } diff --git a/install.go b/install.go index 67b6bba..11b602d 100644 --- a/install.go +++ b/install.go @@ -1,6 +1,8 @@ package main import ( + "crypto/rand" + "encoding/hex" "fmt" "io/fs" "log" @@ -46,7 +48,7 @@ func main() { log.Fatal(err) } - fmt.Println("-- rename bootstrap to", projectName) + fmt.Println("-- renaming bootstrap ->", projectName) if err := os.Rename(path.Join("gothkit", bootstrapFolderName), projectName); err != nil { log.Fatal(err) } @@ -83,5 +85,39 @@ func main() { log.Fatal(err) } + fmt.Println("-- renaming .env.local -> .env") + if err := os.Rename( + path.Join(projectName, ".env.local"), + path.Join(projectName, ".env"), + ); err != nil { + log.Fatal(err) + } + + fmt.Println("-- generating secure secret") + pathToDotEnv := path.Join(projectName, ".env") + fmt.Println("rewriting ->", pathToDotEnv) + b, err := os.ReadFile(pathToDotEnv) + if err != nil { + log.Fatal(err) + } + replacedContent := strings.Replace(string(b), "{{app_secret}}", "foobar", -1) + fmt.Println(replacedContent) + file, err := os.OpenFile(pathToDotEnv, os.O_WRONLY|os.O_TRUNC, 0644) + if err != nil { + log.Fatal(err) + } + defer file.Close() + _, err = file.WriteString(replacedContent) + if err != nil { + log.Fatal(err) + } fmt.Printf("-- project (%s) successfully installed!\n", projectName) } + +func generateSecret() string { + bytes := make([]byte, 32) + if _, err := rand.Read(bytes); err != nil { + log.Fatal(err) + } + return hex.EncodeToString(bytes) +}