Initialize repository

This commit is contained in:
eikendev 2020-07-26 00:28:38 +02:00
commit 1d758fcfd0
No known key found for this signature in database
GPG key ID: A1BDB1B28C8EF694
28 changed files with 1107 additions and 0 deletions

31
database/application.go Normal file
View file

@ -0,0 +1,31 @@
package database
import (
"errors"
"github.com/eikendev/pushbits/model"
"gorm.io/gorm"
)
// CreateApplication creates an application.
func (d *Database) CreateApplication(application *model.Application) error {
return d.gormdb.Create(application).Error
}
// UpdateApplication updates an application.
func (d *Database) UpdateApplication(app *model.Application) error {
return d.gormdb.Save(app).Error
}
// GetApplicationByToken returns the application for the given token or nil.
func (d *Database) GetApplicationByToken(token string) (*model.Application, error) {
app := new(model.Application)
err := d.gormdb.Where("token = ?", token).First(app).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, err
}
return app, err
}

101
database/database.go Normal file
View file

@ -0,0 +1,101 @@
package database
import (
"database/sql"
"errors"
"log"
"os"
"path/filepath"
"time"
"github.com/eikendev/pushbits/authentication/credentials"
"github.com/eikendev/pushbits/model"
"gorm.io/driver/mysql"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
// Database holds information for the database connection.
type Database struct {
gormdb *gorm.DB
sqldb *sql.DB
}
func createFileDir(file string) {
if _, err := os.Stat(filepath.Dir(file)); os.IsNotExist(err) {
if err := os.MkdirAll(filepath.Dir(file), 0775); err != nil {
panic(err)
}
}
}
// Create instanciates a database connection.
func Create(dialect, connection string) (*Database, error) {
log.Println("Setting up database connection.")
maxOpenConns := 5
var db *gorm.DB
var err error
switch dialect {
case "sqlite3":
createFileDir(connection)
maxOpenConns = 1
db, err = gorm.Open(sqlite.Open(connection), &gorm.Config{})
case "mysql":
db, err = gorm.Open(mysql.Open(connection), &gorm.Config{})
default:
message := "Database dialect is not supported"
return nil, errors.New(message)
}
if err != nil {
return nil, err
}
sql, err := db.DB()
if err != nil {
return nil, err
}
sql.SetMaxOpenConns(maxOpenConns)
if dialect == "mysql" {
sql.SetConnMaxLifetime(9 * time.Minute)
}
db.AutoMigrate(&model.User{}, &model.Application{})
return &Database{gormdb: db, sqldb: sql}, nil
}
// Close closes the database connection.
func (d *Database) Close() {
d.sqldb.Close()
}
// Populate fills the database with initial information like the admin user.
func (d *Database) Populate(name, password, matrixID string) error {
user := new(model.User)
query := d.gormdb.Where("name = ?", name).First(user)
if errors.Is(query.Error, gorm.ErrRecordNotFound) {
user := model.NewUser(name, password, true, matrixID)
if err := d.gormdb.Create(&user).Error; err != nil {
return errors.New("user cannot be created")
}
} else {
log.Printf("Admin user %s already exists.\n", name)
user.PasswordHash = credentials.CreatePassword(password)
user.IsAdmin = true
user.MatrixID = matrixID
d.gormdb.Save(&user)
}
return nil
}

26
database/user.go Normal file
View file

@ -0,0 +1,26 @@
package database
import (
"errors"
"github.com/eikendev/pushbits/model"
"gorm.io/gorm"
)
// CreateUser creates a user.
func (d *Database) CreateUser(user *model.User) error {
return d.gormdb.Create(user).Error
}
// GetUserByName returns the user by the given name or nil.
func (d *Database) GetUserByName(name string) (*model.User, error) {
user := new(model.User)
err := d.gormdb.Where("name = ?", name).First(user).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, err
}
return user, err
}