drop seeder and PSQL config

This commit is contained in:
Hayden 2022-09-03 19:14:19 -08:00
parent c2613a03ca
commit 194a90ccfb
7 changed files with 4 additions and 143 deletions

View file

@ -83,8 +83,6 @@ func run(cfg *config.Config) error {
routes := app.newRouter(app.repos)
app.LogRoutes(routes)
app.SeedDatabase(app.repos)
log.Info().Msgf("Starting HTTP Server on %s:%s", app.server.Host, app.server.Port)
// =========================================================================

View file

@ -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")
}
}

View file

@ -5,7 +5,7 @@ swagger:
scheme: http
web:
port: 7745
host:
host:
database:
driver: sqlite3
sqlite-url: ./ent.db?_fk=1
@ -18,15 +18,3 @@ mailer:
username:
password:
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

View file

@ -22,7 +22,6 @@ type Config struct {
Database Database `yaml:"database"`
Log LoggerConf `yaml:"logger"`
Mailer MailerConf `yaml:"mailer"`
Seed Seed `yaml:"seed"`
Swagger SwaggerConf `yaml:"swagger"`
}

View file

@ -1,14 +1,12 @@
package config
const (
DriverSqlite3 = "sqlite3"
DriverPostgres = "postgres"
DriverSqlite3 = "sqlite3"
)
type Database struct {
Driver string `yaml:"driver" conf:"default:sqlite3"`
SqliteUrl string `yaml:"sqlite-url" conf:"default:file:ent?mode=memory&cache=shared&_fk=1"`
PostgresUrl string `yaml:"postgres-url" conf:""`
Driver string `yaml:"driver" conf:"default:sqlite3"`
SqliteUrl string `yaml:"sqlite-url" conf:"default:file:ent?mode=memory&cache=shared&_fk=1"`
}
func (d *Database) GetDriver() string {
@ -19,8 +17,6 @@ func (d *Database) GetUrl() string {
switch d.Driver {
case DriverSqlite3:
return d.SqliteUrl
case DriverPostgres:
return d.PostgresUrl
default:
panic("unknown database driver")
}

View file

@ -16,16 +16,6 @@ func Test_DatabaseConfig_Sqlite(t *testing.T) {
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) {
dbConf := &Database{
Driver: "null",

View file

@ -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"`
}