feature: add database management for later use, fix logging and add dependencies

This commit is contained in:
2026-04-01 23:05:23 +00:00
parent 584475b47d
commit 65c02c5af7
7 changed files with 100 additions and 5 deletions
+5
View File
@@ -43,6 +43,11 @@ ORCHESTRA_{API|WEB}_{ENV_VAR}
```env
ORCHESTRA_API_PORT=9810
ORCHESTRA_API_SQLITE_DATABASE_PATH=string // Not used when POSTGRES_ENABLED is set to true.
ORCHESTRA_API_POSTGRES_ENABLED=bool
ORCHESTRA_API_OPENTOFU_VERSION=string
ORCHESTRA_API_OAUTH_ENABLED=bool
ORCHESTRA_API_OAUTH_SECRET=string
ORCHESTRA_API_OAUTH_AUTH_URL=string
+2 -2
View File
@@ -18,7 +18,7 @@ import (
const idleTimeout time.Duration = 5 * time.Second
var apiAppPort string = config.Config("ORCHESTRA_API_PORT", ":9810")
var tofu, tofuDir, workPath = config.GetTofu(config.Config("ORCHESTRA_API_TOFU_VERSION", "1.11.5"))
var tofu, tofuDir, workPath = config.GetTofu(config.Config("ORCHESTRA_API_OPENTOFU_VERSION", "1.11.5"))
var app *fiber.App
func StartAPIServer(wg *sync.WaitGroup) {
@@ -46,7 +46,7 @@ func StartAPIServer(wg *sync.WaitGroup) {
routes.GlobalRouter(prefix)
golog.Fatal(app.Listen(apiAppPort))
app.Listen(apiAppPort)
}
func StopAPIService() {
+1 -2
View File
@@ -13,7 +13,6 @@ import (
"github.com/gofiber/fiber/v3/middleware/responsetime"
"github.com/gofiber/fiber/v3/middleware/session"
"github.com/gofiber/fiber/v3/middleware/static"
"github.com/kataras/golog"
)
const idleTimeout time.Duration = 5 * time.Second
@@ -60,7 +59,7 @@ func StartWebServer(wg *sync.WaitGroup) {
})
})
golog.Fatal(app.Listen(webAppPort))
app.Listen(webAppPort)
}
func StopWebService() {
+58
View File
@@ -0,0 +1,58 @@
package config
import (
"fmt"
"github.com/kataras/golog"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
var DB *gorm.DB
type Postgres struct {
host string
user string
password string
dbname string
port uint16
TimeZone string
}
type SQLite struct {
databasePath string
}
func SetupSQLite(s SQLite) *gorm.DB {
db, err := gorm.Open(sqlite.Open(s.databasePath), &gorm.Config{})
if err != nil {
golog.Fatalf("Database can not be created... %s", err)
}
return db
}
func SetupPostgres(p Postgres) *gorm.DB {
dbString := fmt.Sprintf("user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=%s", p.user, p.password, p.dbname, p.port, p.TimeZone)
db, err := gorm.Open(postgres.New(postgres.Config{
DSN: dbString,
PreferSimpleProtocol: true, // disables implicit prepared statement usage
}), &gorm.Config{})
if err != nil {
golog.Fatalf("Connection can not be created... %s", err)
}
return db
}
func SetupDB() {
db := SetupSQLite(SQLite{databasePath: "./db/sqlite.sqlite3"})
DB = db
}
func Migrate(tables ...interface{}) error {
return DB.AutoMigrate(tables...)
}
+1 -1
View File
@@ -22,7 +22,7 @@ func PrepareTemporaryDictionary(dir string) string {
func RemoveFolder(path string) {
err := os.RemoveAll(path)
if err == nil {
if err != nil {
golog.Fatalf("Can not remove the temporary folder... %s", err)
}
}
+13
View File
@@ -9,6 +9,9 @@ require (
github.com/opentofu/tofu-exec v0.0.2
github.com/opentofu/tofudl v0.0.1
github.com/tidwall/gjson v1.18.0
gorm.io/gorm v1.31.1
gorm.io/driver/sqlite v1.6.0
gorm.io/driver/postgres v1.6.0
)
require (
@@ -42,14 +45,21 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/terraform-json v0.22.1 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgx/v5 v5.9.1 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/jedib0t/go-pretty v4.3.0+incompatible // indirect
github.com/jedib0t/go-pretty/v6 v6.4.6 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/klauspost/compress v1.18.5 // indirect
github.com/knadh/koanf v1.5.0 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mattn/go-sqlite3 v1.14.38 // indirect
github.com/mergestat/timediff v0.0.3 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
@@ -84,4 +94,7 @@ require (
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/driver/postgres v1.6.0 // indirect
gorm.io/driver/sqlite v1.6.0 // indirect
gorm.io/gorm v1.31.1 // indirect
)
+20
View File
@@ -205,10 +205,22 @@ github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4Dvx
github.com/hjson/hjson-go/v4 v4.0.0/go.mod h1:KaYt3bTw3zhBjYqnXkYywcYctk0A2nxeEFTse3rH13E=
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.9.1 h1:uwrxJXBnx76nyISkhr33kQLlUqjv7et7b9FjCen/tdc=
github.com/jackc/pgx/v5 v5.9.1/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4=
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jedib0t/go-pretty v4.3.0+incompatible h1:CGs8AVhEKg/n9YbUenWmNStRW2PHJzaeDodcfvRAbIo=
github.com/jedib0t/go-pretty v4.3.0+incompatible/go.mod h1:XemHduiw8R651AF9Pt4FwCTKeG3oo7hrHJAoznj9nag=
github.com/jedib0t/go-pretty/v6 v6.4.6 h1:v6aG9h6Uby3IusSSEjHaZNXpHFhzqMmjXcPq1Rjl9Jw=
github.com/jedib0t/go-pretty/v6 v6.4.6/go.mod h1:Ndk3ase2CkQbXLLNf5QDHoYb6J9WtVfmHZu9n8rk2xs=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
@@ -258,6 +270,8 @@ github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-sqlite3 v1.14.38 h1:tDUzL85kMvOrvpCt8P64SbGgVFtJB11GPi2AdmITgb4=
github.com/mattn/go-sqlite3 v1.14.38/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mergestat/timediff v0.0.3 h1:ucCNh4/ZrTPjFZ081PccNbhx9spymCJkFxSzgVuPU+Y=
github.com/mergestat/timediff v0.0.3/go.mod h1:yvMUaRu2oetc+9IbPLYBJviz6sA7xz8OXMDfhBl7YSI=
@@ -616,6 +630,12 @@ gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/postgres v1.6.0 h1:2dxzU8xJ+ivvqTRph34QX+WrRaJlmfyPqXmoGVjMBa4=
gorm.io/driver/postgres v1.6.0/go.mod h1:vUw0mrGgrTK+uPHEhAdV4sfFELrByKVGnaVRkXDhtWo=
gorm.io/driver/sqlite v1.6.0 h1:WHRRrIiulaPiPFmDcod6prc4l2VGVWHz80KspNsxSfQ=
gorm.io/driver/sqlite v1.6.0/go.mod h1:AO9V1qIQddBESngQUKWL9yoH93HIeA1X6V633rBwyT8=
gorm.io/gorm v1.31.1 h1:7CA8FTFz/gRfgqgpeKIBcervUn3xSyPUmr6B2WXJ7kg=
gorm.io/gorm v1.31.1/go.mod h1:XyQVbO2k6YkOis7C2437jSit3SsDK72s7n7rsSHd+Gs=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=