diff --git a/backend/app/api/main.go b/backend/app/api/main.go index 99a668a..7519ee7 100644 --- a/backend/app/api/main.go +++ b/backend/app/api/main.go @@ -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) // ========================================================================= diff --git a/backend/app/api/seed.go b/backend/app/api/seed.go deleted file mode 100644 index 76d9daa..0000000 --- a/backend/app/api/seed.go +++ /dev/null @@ -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") - } -} diff --git a/backend/config.template.yml b/backend/config.template.yml index 366e2a3..cb699c9 100644 --- a/backend/config.template.yml +++ b/backend/config.template.yml @@ -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 diff --git a/backend/internal/config/conf.go b/backend/internal/config/conf.go index 5f6c7e1..79edadd 100644 --- a/backend/internal/config/conf.go +++ b/backend/internal/config/conf.go @@ -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"` } diff --git a/backend/internal/config/conf_database.go b/backend/internal/config/conf_database.go index d8a6c7b..7b396bd 100644 --- a/backend/internal/config/conf_database.go +++ b/backend/internal/config/conf_database.go @@ -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") } diff --git a/backend/internal/config/conf_database_test.go b/backend/internal/config/conf_database_test.go index 4720a15..f11c21c 100644 --- a/backend/internal/config/conf_database_test.go +++ b/backend/internal/config/conf_database_test.go @@ -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", diff --git a/backend/internal/config/conf_seed.go b/backend/internal/config/conf_seed.go deleted file mode 100644 index 67a409a..0000000 --- a/backend/internal/config/conf_seed.go +++ /dev/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"` -}