feature: Change state management from global variables to object passing
This restructures dbcore (now the db package) and jwtcore (now the jwt package) to use a single struct. There is now a state package, which contains a struct with the full application state. After this, instead of initializing the API routes directly in the main function, the state object gets passed, and the API routes get initialized with their accompanying code. One fix done to reduce memory usage and increase speed is that the validator object is now persistent across requests, instead of recreating it each time. This should speed things up slightly, and improve memory usage. One additional chore done is that the database models have been moved to be a seperate file from the DB initialization itself.
This commit is contained in:
parent
71d53990de
commit
d56a8eb7bf
23 changed files with 1901 additions and 2161 deletions
77
backend/api/db/db.go
Normal file
77
backend/api/db/db.go
Normal file
|
@ -0,0 +1,77 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type DB struct {
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
func New(backend, params string) (*DB, error) {
|
||||
var err error
|
||||
|
||||
dialector, err := initDialector(backend, params)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to initialize physical database: %s", err)
|
||||
}
|
||||
|
||||
database, err := gorm.Open(dialector)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open database: %s", err)
|
||||
}
|
||||
|
||||
return &DB{DB: database}, nil
|
||||
}
|
||||
|
||||
func (db *DB) DoMigrations() error {
|
||||
if err := db.DB.AutoMigrate(&Proxy{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := db.DB.AutoMigrate(&Backend{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := db.DB.AutoMigrate(&Permission{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := db.DB.AutoMigrate(&Token{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := db.DB.AutoMigrate(&User{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func initDialector(backend, params string) (gorm.Dialector, error) {
|
||||
switch backend {
|
||||
case "sqlite":
|
||||
if params == "" {
|
||||
return nil, fmt.Errorf("sqlite database file not specified")
|
||||
}
|
||||
|
||||
return sqlite.Open(params), nil
|
||||
case "postgresql":
|
||||
if params == "" {
|
||||
return nil, fmt.Errorf("postgres DSN not specified")
|
||||
}
|
||||
|
||||
return postgres.Open(params), nil
|
||||
case "":
|
||||
return nil, fmt.Errorf("no database backend specified in environment variables")
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown database backend specified: %s", os.Getenv(backend))
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue