forked from mirrors/homebox
drop seeder and PSQL config
This commit is contained in:
parent
c2613a03ca
commit
194a90ccfb
7 changed files with 4 additions and 143 deletions
|
@ -83,8 +83,6 @@ func run(cfg *config.Config) error {
|
||||||
routes := app.newRouter(app.repos)
|
routes := app.newRouter(app.repos)
|
||||||
app.LogRoutes(routes)
|
app.LogRoutes(routes)
|
||||||
|
|
||||||
app.SeedDatabase(app.repos)
|
|
||||||
|
|
||||||
log.Info().Msgf("Starting HTTP Server on %s:%s", app.server.Host, app.server.Port)
|
log.Info().Msgf("Starting HTTP Server on %s:%s", app.server.Host, app.server.Port)
|
||||||
|
|
||||||
// =========================================================================
|
// =========================================================================
|
||||||
|
|
|
@ -1,96 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/hay-kot/content/backend/ent"
|
|
||||||
"github.com/hay-kot/content/backend/internal/repo"
|
|
||||||
"github.com/hay-kot/content/backend/internal/types"
|
|
||||||
"github.com/hay-kot/content/backend/pkgs/hasher"
|
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
DefaultGroup = "Default"
|
|
||||||
DefaultName = "Admin"
|
|
||||||
DefaultEmail = "admin@admin.com"
|
|
||||||
DefaultPassword = "admin"
|
|
||||||
)
|
|
||||||
|
|
||||||
// EnsureAdministrator ensures that there is at least one superuser in the database
|
|
||||||
// if one isn't found a default is generate using the default credentials
|
|
||||||
func (a *app) EnsureAdministrator() {
|
|
||||||
superusers, err := a.repos.Users.GetSuperusers(context.Background())
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("failed to get superusers")
|
|
||||||
}
|
|
||||||
if len(superusers) > 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
pw, _ := hasher.HashPassword(DefaultPassword)
|
|
||||||
newSuperUser := types.UserCreate{
|
|
||||||
Name: DefaultName,
|
|
||||||
Email: DefaultEmail,
|
|
||||||
IsSuperuser: true,
|
|
||||||
Password: pw,
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Info().
|
|
||||||
Str("name", newSuperUser.Name).
|
|
||||||
Str("email", newSuperUser.Email).
|
|
||||||
Msg("no superusers found, creating default superuser")
|
|
||||||
|
|
||||||
_, err = a.repos.Users.Create(context.Background(), newSuperUser)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("failed to create default superuser")
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *app) SeedDatabase(repos *repo.AllRepos) {
|
|
||||||
if !a.conf.Seed.Enabled {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
group, err := repos.Groups.Create(context.Background(), DefaultGroup)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("failed to create default group")
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, seedUser := range a.conf.Seed.Users {
|
|
||||||
|
|
||||||
// Check if User Exists
|
|
||||||
usr, err := repos.Users.GetOneEmail(context.Background(), seedUser.Email)
|
|
||||||
if err != nil && !ent.IsNotFound(err) {
|
|
||||||
log.Fatal().Err(err).Msg("failed to get user")
|
|
||||||
}
|
|
||||||
|
|
||||||
if usr != nil && usr.ID != uuid.Nil {
|
|
||||||
log.Info().Str("email", seedUser.Email).Msg("user already exists, skipping")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
hashedPw, err := hasher.HashPassword(seedUser.Password)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("failed to hash password")
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = repos.Users.Create(context.Background(), types.UserCreate{
|
|
||||||
Name: seedUser.Name,
|
|
||||||
Email: seedUser.Email,
|
|
||||||
IsSuperuser: seedUser.IsSuperuser,
|
|
||||||
Password: hashedPw,
|
|
||||||
GroupID: group.ID,
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("failed to create user")
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Info().Str("email", seedUser.Email).Msg("created user")
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -5,7 +5,7 @@ swagger:
|
||||||
scheme: http
|
scheme: http
|
||||||
web:
|
web:
|
||||||
port: 7745
|
port: 7745
|
||||||
host:
|
host:
|
||||||
database:
|
database:
|
||||||
driver: sqlite3
|
driver: sqlite3
|
||||||
sqlite-url: ./ent.db?_fk=1
|
sqlite-url: ./ent.db?_fk=1
|
||||||
|
@ -18,15 +18,3 @@ mailer:
|
||||||
username:
|
username:
|
||||||
password:
|
password:
|
||||||
from: example@email.com
|
from: example@email.com
|
||||||
seed:
|
|
||||||
enabled: true
|
|
||||||
group: Default
|
|
||||||
users:
|
|
||||||
- name: Admin
|
|
||||||
email: admin@admin.com
|
|
||||||
password: admin
|
|
||||||
isSuperuser: true
|
|
||||||
- name: User
|
|
||||||
email: user@user.com
|
|
||||||
password: user
|
|
||||||
isSuperuser: false
|
|
||||||
|
|
|
@ -22,7 +22,6 @@ type Config struct {
|
||||||
Database Database `yaml:"database"`
|
Database Database `yaml:"database"`
|
||||||
Log LoggerConf `yaml:"logger"`
|
Log LoggerConf `yaml:"logger"`
|
||||||
Mailer MailerConf `yaml:"mailer"`
|
Mailer MailerConf `yaml:"mailer"`
|
||||||
Seed Seed `yaml:"seed"`
|
|
||||||
Swagger SwaggerConf `yaml:"swagger"`
|
Swagger SwaggerConf `yaml:"swagger"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,12 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
const (
|
const (
|
||||||
DriverSqlite3 = "sqlite3"
|
DriverSqlite3 = "sqlite3"
|
||||||
DriverPostgres = "postgres"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Database struct {
|
type Database struct {
|
||||||
Driver string `yaml:"driver" conf:"default:sqlite3"`
|
Driver string `yaml:"driver" conf:"default:sqlite3"`
|
||||||
SqliteUrl string `yaml:"sqlite-url" conf:"default:file:ent?mode=memory&cache=shared&_fk=1"`
|
SqliteUrl string `yaml:"sqlite-url" conf:"default:file:ent?mode=memory&cache=shared&_fk=1"`
|
||||||
PostgresUrl string `yaml:"postgres-url" conf:""`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Database) GetDriver() string {
|
func (d *Database) GetDriver() string {
|
||||||
|
@ -19,8 +17,6 @@ func (d *Database) GetUrl() string {
|
||||||
switch d.Driver {
|
switch d.Driver {
|
||||||
case DriverSqlite3:
|
case DriverSqlite3:
|
||||||
return d.SqliteUrl
|
return d.SqliteUrl
|
||||||
case DriverPostgres:
|
|
||||||
return d.PostgresUrl
|
|
||||||
default:
|
default:
|
||||||
panic("unknown database driver")
|
panic("unknown database driver")
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,16 +16,6 @@ func Test_DatabaseConfig_Sqlite(t *testing.T) {
|
||||||
assert.Equal(t, "file:ent?mode=memory&cache=shared&_fk=1", dbConf.GetUrl())
|
assert.Equal(t, "file:ent?mode=memory&cache=shared&_fk=1", dbConf.GetUrl())
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_DatabaseConfig_Postgres(t *testing.T) {
|
|
||||||
dbConf := &Database{
|
|
||||||
Driver: DriverPostgres,
|
|
||||||
PostgresUrl: "postgres://user:pass@host:port/dbname?sslmode=disable",
|
|
||||||
}
|
|
||||||
|
|
||||||
assert.Equal(t, "postgres", dbConf.GetDriver())
|
|
||||||
assert.Equal(t, "postgres://user:pass@host:port/dbname?sslmode=disable", dbConf.GetUrl())
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_DatabaseConfig_Unknown(t *testing.T) {
|
func Test_DatabaseConfig_Unknown(t *testing.T) {
|
||||||
dbConf := &Database{
|
dbConf := &Database{
|
||||||
Driver: "null",
|
Driver: "null",
|
||||||
|
|
|
@ -1,14 +0,0 @@
|
||||||
package config
|
|
||||||
|
|
||||||
type SeedUser struct {
|
|
||||||
Name string `yaml:"name"`
|
|
||||||
Email string `yaml:"email"`
|
|
||||||
Password string `yaml:"password"`
|
|
||||||
IsSuperuser bool `yaml:"isSuperuser"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Seed struct {
|
|
||||||
Enabled bool `yaml:"enabled" conf:"default:false"`
|
|
||||||
Users []SeedUser `yaml:"users"`
|
|
||||||
Group string `yaml:"group"`
|
|
||||||
}
|
|
Loading…
Reference in a new issue