Skip to content

Commit

Permalink
fix blocking error during startup when in demo mode (#838)
Browse files Browse the repository at this point in the history
  • Loading branch information
hay-kot authored Mar 5, 2024
1 parent af9aa23 commit f621d3a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
1 change: 1 addition & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
version: "3"

env:
HBOX_LOG_LEVEL: debug
HBOX_STORAGE_SQLITE_URL: .data/homebox.db?_pragma=busy_timeout=1000&_pragma=journal_mode=WAL&_fk=1
HBOX_OPTIONS_ALLOW_REGISTRATION: true
UNSAFE_DISABLE_PASSWORD_PROJECTION: "yes_i_am_sure"
Expand Down
27 changes: 22 additions & 5 deletions backend/app/api/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"strings"
"time"

"github.com/hay-kot/homebox/backend/internal/core/services"
"github.com/rs/zerolog/log"
Expand All @@ -18,28 +19,44 @@ func (a *app) SetupDemo() {
,Kitchen,IOT;Home Assistant; Z-Wave,1,Smart Rocker Light Dimmer,"UltraPro Z-Wave Smart Rocker Light Dimmer with QuickFit and SimpleWire, 3-Way Ready, Compatible with Alexa, Google Assistant, ZWave Hub Required, Repeater/Range Extender, White Paddle Only, 39351",,,39351,Honeywell,,Amazon,65.98,09/30/0202,,,,,,,
`

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

registration := services.UserRegistration{
Email: "[email protected]",
Name: "Demo",
Password: "demo",
}

// First check if we've already setup a demo user and skip if so
_, err := a.services.User.Login(context.Background(), registration.Email, registration.Password, false)
log.Debug().Msg("Checking if demo user already exists")
_, err := a.services.User.Login(ctx, registration.Email, registration.Password, false)
if err == nil {
log.Info().Msg("Demo user already exists, skipping setup")
return
}

_, err = a.services.User.RegisterUser(context.Background(), registration)
log.Debug().Msg("Demo user does not exist, setting up demo")
_, err = a.services.User.RegisterUser(ctx, registration)
if err != nil {
log.Err(err).Msg("Failed to register demo user")
log.Fatal().Msg("Failed to setup demo")
}

token, _ := a.services.User.Login(context.Background(), registration.Email, registration.Password, false)
self, _ := a.services.User.GetSelf(context.Background(), token.Raw)
token, err := a.services.User.Login(ctx, registration.Email, registration.Password, false)
if err != nil {
log.Err(err).Msg("Failed to login demo user")
log.Fatal().Msg("Failed to setup demo")
return
}
self, err := a.services.User.GetSelf(ctx, token.Raw)
if err != nil {
log.Err(err).Msg("Failed to get self")
log.Fatal().Msg("Failed to setup demo")
return
}

_, err = a.services.Items.CsvImport(context.Background(), self.GroupID, strings.NewReader(csvText))
_, err = a.services.Items.CsvImport(ctx, self.GroupID, strings.NewReader(csvText))
if err != nil {
log.Err(err).Msg("Failed to import CSV")
log.Fatal().Msg("Failed to setup demo")
Expand Down
15 changes: 9 additions & 6 deletions backend/app/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,15 @@ func run(cfg *config.Config) error {

runner.AddFunc("eventbus", app.bus.Run)

runner.AddFunc("seed_database", func(ctx context.Context) error {
// TODO: Remove through external API that does setup
if cfg.Demo {
log.Info().Msg("Running in demo mode, creating demo data")
app.SetupDemo()
}
return nil
})

runner.AddPlugin(NewTask("purge-tokens", time.Duration(24)*time.Hour, func(ctx context.Context) {
_, err := app.repos.AuthTokens.PurgeExpiredTokens(ctx)
if err != nil {
Expand Down Expand Up @@ -239,12 +248,6 @@ func run(cfg *config.Config) error {
}
}))

// TODO: Remove through external API that does setup
if cfg.Demo {
log.Info().Msg("Running in demo mode, creating demo data")
app.SetupDemo()
}

if cfg.Debug.Enabled {
runner.AddFunc("debug", func(ctx context.Context) error {
debugserver := http.Server{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type EventBus struct {

func New() *EventBus {
return &EventBus{
ch: make(chan eventData, 10),
ch: make(chan eventData, 100),
subscribers: map[Event][]func(any){
EventLabelMutation: {},
EventLocationMutation: {},
Expand Down
4 changes: 4 additions & 0 deletions backend/internal/core/services/service_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,19 @@ func (svc *UserService) RegisterUser(ctx context.Context, data UserRegistration)
if err != nil {
return repo.UserOut{}, err
}
log.Debug().Msg("user created")

// Create the default labels and locations for the group.
if creatingGroup {
log.Debug().Msg("creating default labels")
for _, label := range defaultLabels() {
_, err := svc.repos.Labels.Create(ctx, usr.GroupID, label)
if err != nil {
return repo.UserOut{}, err
}
}

log.Debug().Msg("creating default locations")
for _, location := range defaultLocations() {
_, err := svc.repos.Locations.Create(ctx, usr.GroupID, location)
if err != nil {
Expand All @@ -112,6 +115,7 @@ func (svc *UserService) RegisterUser(ctx context.Context, data UserRegistration)

// Decrement the invitation token if it was used.
if token.ID != uuid.Nil {
log.Debug().Msg("decrementing invitation token")
err = svc.repos.Groups.InvitationUpdate(ctx, token.ID, token.Uses-1)
if err != nil {
log.Err(err).Msg("Failed to update invitation token")
Expand Down

0 comments on commit f621d3a

Please sign in to comment.