Working on the Calculated thing
Did some work towards the lua interface, but its kinda shit tbh
This commit is contained in:
+23
-5
@@ -4,7 +4,10 @@ import (
|
||||
"database/sql"
|
||||
"expenses/config"
|
||||
mod "expenses/expenses/models"
|
||||
"expenses/luadec"
|
||||
"log"
|
||||
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -12,12 +15,14 @@ var (
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
DB *sql.DB
|
||||
DB *sql.DB
|
||||
Lua *lua.LState
|
||||
}
|
||||
|
||||
func NewExpenseService(db *sql.DB) *Service {
|
||||
func NewExpenseService(db *sql.DB, luaVM *lua.LState) *Service {
|
||||
return &Service{
|
||||
DB: db,
|
||||
DB: db,
|
||||
Lua: luaVM,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,16 +35,29 @@ func openDB(sys string, path string, extra string) (*sql.DB, error) {
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func StartLuaVM() (*lua.LState, error) {
|
||||
L := lua.NewState()
|
||||
|
||||
L.SetGlobal("GetAllExpenses", L.NewFunction(Serv.LuaGetAllExpenses))
|
||||
L.SetGlobal("AddDecimal", L.NewFunction(luadec.AddDecimal))
|
||||
|
||||
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)
|
||||
err = mod.RunMigrations(migDB, luaVM)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to apply migrations: %v", err)
|
||||
}
|
||||
@@ -49,7 +67,7 @@ func StartApp() error {
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open DB: %v", err)
|
||||
}
|
||||
Serv = NewExpenseService(db)
|
||||
Serv = NewExpenseService(db, luaVM)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user