WIP: Add Two Factor Authentication #7
3 changed files with 28 additions and 30 deletions
|
@ -3,23 +3,19 @@ package app
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
"log"
|
||||
|
||||
"code.lila.network/adoralaura/go-urlsh/internal/app"
|
||||
"code.lila.network/adoralaura/go-urlsh/internal/db"
|
||||
"code.lila.network/adoralaura/go-urlsh/models"
|
||||
)
|
||||
|
||||
func Run() error {
|
||||
|
||||
err := db.InitializeDB()
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
models.DB = db.InitializeDB()
|
||||
|
||||
go app.CleanupLogins()
|
||||
go app.CleanupLoginsCronJob()
|
||||
|
||||
err = app.SetupFiber()
|
||||
err := app.SetupFiber()
|
||||
if err != nil {
|
||||
return fmt.Errorf("couldn't start webserver: %v", err.Error())
|
||||
}
|
||||
|
|
|
@ -3,57 +3,59 @@ package db
|
|||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"log"
|
||||
|
||||
"code.lila.network/adoralaura/go-urlsh/models"
|
||||
"github.com/uptrace/bun"
|
||||
"github.com/uptrace/bun/dialect/pgdialect"
|
||||
"github.com/uptrace/bun/driver/pgdriver"
|
||||
)
|
||||
|
||||
func InitializeDB() error {
|
||||
func InitializeDB() *bun.DB {
|
||||
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(os.Getenv("DATABASE_URL"))))
|
||||
models.DB = bun.NewDB(sqldb, pgdialect.New())
|
||||
db := bun.NewDB(sqldb, pgdialect.New())
|
||||
|
||||
_, err := models.DB.NewCreateTable().IfNotExists().Model((*models.Link)(nil)).Exec(context.Background())
|
||||
_, err := db.NewCreateTable().IfNotExists().Model((*models.Link)(nil)).Exec(context.Background())
|
||||
if err != nil {
|
||||
return fmt.Errorf("[DB] couldn't create database: [%w]", err)
|
||||
log.Panicf("[DB] couldn't create database: [%w]", err)
|
||||
}
|
||||
|
||||
_, err = models.DB.NewCreateTable().IfNotExists().Model((*models.User)(nil)).Exec(context.Background())
|
||||
_, err = db.NewCreateTable().IfNotExists().Model((*models.User)(nil)).Exec(context.Background())
|
||||
if err != nil {
|
||||
return fmt.Errorf("[DB] couldn't create database: [%w]", err)
|
||||
log.Panicf("[DB] couldn't create database: [%w]", err)
|
||||
}
|
||||
|
||||
_, err = models.DB.NewCreateTable().IfNotExists().Model((*models.Session)(nil)).Exec(context.Background())
|
||||
_, err = db.NewCreateTable().IfNotExists().Model((*models.Session)(nil)).Exec(context.Background())
|
||||
if err != nil {
|
||||
return fmt.Errorf("[DB] couldn't create database: [%w]", err)
|
||||
log.Panicf("[DB] couldn't create database: [%w]", err)
|
||||
}
|
||||
|
||||
_, err = models.DB.NewCreateTable().IfNotExists().Model((*models.ApiKey)(nil)).Exec(context.Background())
|
||||
_, err = db.NewCreateTable().IfNotExists().Model((*models.ApiKey)(nil)).Exec(context.Background())
|
||||
if err != nil {
|
||||
return fmt.Errorf("[DB] couldn't create database: [%w]", err)
|
||||
log.Panicf("[DB] couldn't create database: [%w]", err)
|
||||
}
|
||||
|
||||
_, err = models.DB.NewCreateTable().IfNotExists().Model((*models.MFALoginTransaction)(nil)).Exec(context.Background())
|
||||
_, err = db.NewCreateTable().IfNotExists().Model((*models.MFALoginTransaction)(nil)).Exec(context.Background())
|
||||
if err != nil {
|
||||
return fmt.Errorf("[DB] couldn't create database: [%w]", err)
|
||||
log.Panicf("[DB] couldn't create database: [%w]", err)
|
||||
}
|
||||
|
||||
_, err = models.DB.NewCreateTable().IfNotExists().Model((*models.MFAConfig)(nil)).Exec(context.Background())
|
||||
_, err = db.NewCreateTable().IfNotExists().Model((*models.MFAConfig)(nil)).Exec(context.Background())
|
||||
if err != nil {
|
||||
return fmt.Errorf("[DB] couldn't create database: [%w]", err)
|
||||
log.Panicf("[DB] couldn't create database: [%w]", err)
|
||||
}
|
||||
|
||||
_, err = models.DB.NewCreateTable().IfNotExists().Model((*models.MFAScratchCode)(nil)).Exec(context.Background())
|
||||
_, err = db.NewCreateTable().IfNotExists().Model((*models.MFAScratchCode)(nil)).Exec(context.Background())
|
||||
if err != nil {
|
||||
return fmt.Errorf("[DB] couldn't create database: [%w]", err)
|
||||
log.Panicf("[DB] couldn't create database: [%w]", err)
|
||||
}
|
||||
|
||||
err = doMigrations()
|
||||
err = doMigrations(db)
|
||||
if err != nil {
|
||||
return fmt.Errorf("[DB] Error during Migrations: [%w]", err)
|
||||
log.Panicf("[DB] Error during Migrations: [%w]", err)
|
||||
}
|
||||
return nil
|
||||
|
||||
return db
|
||||
}
|
||||
|
|
|
@ -5,13 +5,13 @@ import (
|
|||
"log"
|
||||
|
||||
"code.lila.network/adoralaura/go-urlsh/migrations"
|
||||
"code.lila.network/adoralaura/go-urlsh/models"
|
||||
"github.com/uptrace/bun"
|
||||
"github.com/uptrace/bun/migrate"
|
||||
)
|
||||
|
||||
func doMigrations() error {
|
||||
func doMigrations(db *bun.DB) error {
|
||||
ctx := context.Background()
|
||||
migrator := migrate.NewMigrator(models.DB, migrations.Migrations)
|
||||
migrator := migrate.NewMigrator(db, migrations.Migrations)
|
||||
|
||||
migrator.Init(ctx)
|
||||
|
||||
|
|
Loading…
Reference in a new issue