Reorganize & Add DB Bootstrapping

This commit is contained in:
Evan Reichard 2021-01-12 17:06:27 -05:00
parent bc3b437ebc
commit fe932de37e
7 changed files with 49 additions and 40 deletions

View File

@ -1,11 +1,12 @@
package cmd
import (
"net/http"
"reichard.io/imagini/routes"
"reichard.io/imagini/internal/context"
"github.com/urfave/cli/v2"
"net/http"
log "github.com/sirupsen/logrus"
)
@ -16,19 +17,11 @@ var CmdServe = cli.Command{
Action: serveWeb,
}
// var CmdDBTest = cli.Command{
// Name: "test",
// Aliases: []string{"t"},
// Usage: "test db.",
// Action: testDatabase,
// }
func serveWeb(cliCtx *cli.Context) error {
log.Info("Serving Web")
ctx := context.NewImaginiContext()
routes.RegisterRoutes(ctx)
//listener, _ := net.Listen("tcp", ctx.Config.ListenPort)
if err := http.ListenAndServe(":" + ctx.Config.ListenPort, nil); err != nil {
log.Fatal(err)

Binary file not shown.

View File

@ -1,17 +1,14 @@
package auth
import (
"errors"
"gorm.io/gorm"
"golang.org/x/crypto/bcrypt"
log "github.com/sirupsen/logrus"
"reichard.io/imagini/internal/query"
"reichard.io/imagini/internal/models"
)
func authenticateLocalUser(user models.User, pw string) bool {
bPassword :=[]byte(pw)
err := bcrypt.CompareHashAndPassword([]byte(user.HashedPassword), bPassword)
err := bcrypt.CompareHashAndPassword([]byte(user.Password), bPassword)
if err == nil {
log.Info("[auth] Authentication successfull: ", user.Username)
return true
@ -19,20 +16,3 @@ func authenticateLocalUser(user models.User, pw string) bool {
log.Warn("[auth] Authentication failed: ", user.Username)
return false
}
func CreateUser(db *gorm.DB, user models.User, pw string) error {
log.Info("[auth] Creating user: ", user.Username)
_, err := query.User(db, user)
if !errors.Is(err, gorm.ErrRecordNotFound) {
log.Warn("[auth] User already exists: ", user.Username)
return errors.New("User already exists")
}
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(pw), bcrypt.DefaultCost)
if err != nil {
log.Error(err)
return err
}
user.HashedPassword = string(hashedPassword)
return query.CreateUser(db, user)
}

View File

@ -13,9 +13,9 @@ type ImaginiContext struct {
func NewImaginiContext() *ImaginiContext {
c := config.NewConfig()
gormDB := query.NewDB(c)
db := query.NewDB(c)
return &ImaginiContext{
DB: gormDB,
DB: db,
Config: c,
}
}

View File

@ -19,7 +19,7 @@ type User struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
AuthType string `json:"auth_type"`
HashedPassword string `json:"hashed_password"`
Password string `json:"password"`
}
type MediaItem struct {

View File

@ -1,13 +1,12 @@
package query
import (
"log"
"path"
// "time"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/driver/sqlite"
log "github.com/sirupsen/logrus"
"reichard.io/imagini/internal/config"
"reichard.io/imagini/internal/models"
@ -24,18 +23,39 @@ func NewDB(c *config.Config) *gorm.DB {
dbLocation := path.Join(c.ConfigPath, "imagini.db")
db, _ = gorm.Open(sqlite.Open(dbLocation), gormConfig)
} else {
log.Fatal("ERROR: Unsupported Database")
log.Fatal("Unsupported Database")
}
// Initialize Database
// Initialize database
db.AutoMigrate(&models.ServerSetting{})
db.AutoMigrate(&models.User{})
db.AutoMigrate(&models.MediaItem{})
db.AutoMigrate(&models.Tag{})
db.AutoMigrate(&models.Album{})
// Determine whether to bootstrap
var count int64
db.Model(&models.User{}).Count(&count)
if count == 0 {
bootstrapDatabase(db)
}
return db
}
func bootstrapDatabase(db *gorm.DB) {
log.Info("[query] Bootstrapping database.")
err := CreateUser(db, models.User{
Username: "admin",
Password: "admin",
AuthType: "Local",
})
if err != nil {
log.Fatal("[query] Unable to bootstrap database.")
}
}
func ItemsFromAlbum(db *gorm.DB, user models.User, album models.Album) []models.MediaItem {
var mediaItems []models.MediaItem
// db.Table("media_albums").

View File

@ -1,13 +1,29 @@
package query
import (
"errors"
"gorm.io/gorm"
"golang.org/x/crypto/bcrypt"
log "github.com/sirupsen/logrus"
"reichard.io/imagini/internal/models"
)
func CreateUser (db *gorm.DB, user models.User) error {
err := db.Create(&user).Error
func CreateUser(db *gorm.DB, user models.User) error {
log.Info("[query] Creating user: ", user.Username)
_, err := User(db, user)
if !errors.Is(err, gorm.ErrRecordNotFound) {
log.Warn("[query] User already exists: ", user.Username)
return errors.New("User already exists")
}
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost)
if err != nil {
log.Error(err)
return err
}
user.Password = string(hashedPassword)
return db.Create(&user).Error
}
func User (db *gorm.DB, user models.User) (models.User, error) {