refine install.go

This commit is contained in:
anthdm 2024-06-12 18:12:27 +02:00
parent 31728d53da
commit 77881a994c
4 changed files with 50 additions and 29 deletions

View file

@ -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 =

View file

@ -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 =
# 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}}

View file

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

View file

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