WIP: Add Two Factor Authentication #7
3 changed files with 28 additions and 30 deletions
|
@ -3,23 +3,19 @@ package app
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"log"
|
|
||||||
|
|
||||||
"code.lila.network/adoralaura/go-urlsh/internal/app"
|
"code.lila.network/adoralaura/go-urlsh/internal/app"
|
||||||
"code.lila.network/adoralaura/go-urlsh/internal/db"
|
"code.lila.network/adoralaura/go-urlsh/internal/db"
|
||||||
|
"code.lila.network/adoralaura/go-urlsh/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Run() error {
|
func Run() error {
|
||||||
|
|
||||||
err := db.InitializeDB()
|
models.DB = db.InitializeDB()
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
go app.CleanupLogins()
|
go app.CleanupLogins()
|
||||||
go app.CleanupLoginsCronJob()
|
go app.CleanupLoginsCronJob()
|
||||||
|
|
||||||
err = app.SetupFiber()
|
err := app.SetupFiber()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("couldn't start webserver: %v", err.Error())
|
return fmt.Errorf("couldn't start webserver: %v", err.Error())
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,57 +3,59 @@ package db
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"log"
|
||||||
|
|
||||||
"code.lila.network/adoralaura/go-urlsh/models"
|
"code.lila.network/adoralaura/go-urlsh/models"
|
||||||
"github.com/uptrace/bun"
|
"github.com/uptrace/bun"
|
||||||
"github.com/uptrace/bun/dialect/pgdialect"
|
"github.com/uptrace/bun/dialect/pgdialect"
|
||||||
"github.com/uptrace/bun/driver/pgdriver"
|
"github.com/uptrace/bun/driver/pgdriver"
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitializeDB() error {
|
func InitializeDB() *bun.DB {
|
||||||
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(os.Getenv("DATABASE_URL"))))
|
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 {
|
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 {
|
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 {
|
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 {
|
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 {
|
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 {
|
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 {
|
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 {
|
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"
|
"log"
|
||||||
|
|
||||||
"code.lila.network/adoralaura/go-urlsh/migrations"
|
"code.lila.network/adoralaura/go-urlsh/migrations"
|
||||||
"code.lila.network/adoralaura/go-urlsh/models"
|
"github.com/uptrace/bun"
|
||||||
"github.com/uptrace/bun/migrate"
|
"github.com/uptrace/bun/migrate"
|
||||||
)
|
)
|
||||||
|
|
||||||
func doMigrations() error {
|
func doMigrations(db *bun.DB) error {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
migrator := migrate.NewMigrator(models.DB, migrations.Migrations)
|
migrator := migrate.NewMigrator(db, migrations.Migrations)
|
||||||
|
|
||||||
migrator.Init(ctx)
|
migrator.Init(ctx)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue