Some decisions regarding the lua env
it will be in the app environment because it serves the app, will think about it later
This commit is contained in:
+25
-20
@@ -3,29 +3,32 @@ package expenses
|
||||
import (
|
||||
"database/sql"
|
||||
"expenses/config"
|
||||
mod "expenses/expenses/models"
|
||||
"expenses/luadec"
|
||||
"log"
|
||||
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
var (
|
||||
Serv *Service
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
type ExpensesApp struct {
|
||||
DB *sql.DB
|
||||
Lua *lua.LState
|
||||
}
|
||||
|
||||
func NewExpenseService(db *sql.DB, luaVM *lua.LState) *Service {
|
||||
return &Service{
|
||||
var (
|
||||
App *ExpensesApp
|
||||
)
|
||||
|
||||
func NewExpenseApp(db *sql.DB, luaVM *lua.LState) *ExpensesApp {
|
||||
return &ExpensesApp{
|
||||
DB: db,
|
||||
Lua: luaVM,
|
||||
}
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) Close() {
|
||||
a.DB.Close()
|
||||
a.Lua.Close()
|
||||
}
|
||||
|
||||
func openDB(sys string, path string, extra string) (*sql.DB, error) {
|
||||
db, err := sql.Open(sys, "file:"+path+extra)
|
||||
if err != nil {
|
||||
@@ -37,37 +40,39 @@ func openDB(sys string, path string, extra string) (*sql.DB, error) {
|
||||
|
||||
func StartLuaVM() (*lua.LState, error) {
|
||||
L := lua.NewState()
|
||||
|
||||
L.SetGlobal("GetAllExpenses", L.NewFunction(Serv.LuaGetAllExpenses))
|
||||
L.SetGlobal("AddDecimal", L.NewFunction(luadec.AddDecimal))
|
||||
|
||||
App.registerLuaBinds(L)
|
||||
return L, nil
|
||||
}
|
||||
|
||||
func StartApp() error {
|
||||
config.SetConfig("./config.yaml")
|
||||
cfg := config.GetInstance()
|
||||
luaVM, err := StartLuaVM()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
migDB, err := openDB(cfg.DBSys, cfg.DBPath, "")
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open migration DB: %v", err)
|
||||
}
|
||||
|
||||
err = mod.RunMigrations(migDB, luaVM)
|
||||
App = NewExpenseApp(migDB, nil)
|
||||
|
||||
luaVM, err := StartLuaVM()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// api := luapi.New(App)
|
||||
App.Lua = luaVM
|
||||
|
||||
err = RunMigrations(migDB, luaVM)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to apply migrations: %v", err)
|
||||
}
|
||||
migDB.Close()
|
||||
App.Close()
|
||||
|
||||
db, err := openDB(cfg.DBSys, cfg.DBPath, "?_foreign_keys=on")
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open DB: %v", err)
|
||||
}
|
||||
Serv = NewExpenseService(db, luaVM)
|
||||
App = NewExpenseApp(db, luaVM)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user