gitea-github-proxy/db/db.go

48 lines
792 B
Go
Raw Permalink Normal View History

package db
import (
"context"
"git.lumeweb.com/LumeWeb/gitea-github-proxy/config"
"git.lumeweb.com/LumeWeb/gitea-github-proxy/db/model"
"go.uber.org/fx"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type DatabaseParams struct {
fx.In
Config *config.Config
}
var Module = fx.Module("db",
fx.Options(
fx.Provide(NewDatabase),
),
)
func NewDatabase(lc fx.Lifecycle, params DatabaseParams) *gorm.DB {
db, err := gorm.Open(sqlite.Open(params.Config.DbPath), &gorm.Config{})
if err != nil {
panic(err)
}
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
return db.AutoMigrate(
&model.Apps{},
)
},
OnStop: func(ctx context.Context) error {
sqlDb, err := db.DB()
if err != nil {
return err
}
return sqlDb.Close()
},
})
return db
}