Skip to content

Commit

Permalink
fix: server setup
Browse files Browse the repository at this point in the history
  • Loading branch information
cugu committed Nov 8, 2024
1 parent 9285aec commit d169cd0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 38 deletions.
38 changes: 2 additions & 36 deletions app/app.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package app

import (
"fmt"
"os"
"strings"

Expand Down Expand Up @@ -39,47 +38,14 @@ func App(dir string, test bool) (*pocketbase.PocketBase, error) {
reaction.BindHooks(app, test)

app.OnAfterBootstrap().Add(func(e *core.BootstrapEvent) error {
if err := MigrateDBs(e.App); err != nil {
return err
}

if err := SetFlags(e.App, flags); err != nil {
return err
}

if HasFlag(e.App, "demo") {
bindDemoHooks(e.App)
}

if appURL != "" {
s := e.App.Settings()
s.Meta.AppUrl = appURL

if err := e.App.Dao().SaveSettings(s); err != nil {
return err
}
}

return e.App.RefreshSettings()
return MigrateDBs(e.App)
})

app.OnBeforeServe().Add(addRoutes())
app.OnBeforeServe().Add(setupServer(appURL, flags))

return app, nil
}

func bindDemoHooks(app core.App) {
app.OnRecordBeforeCreateRequest("files", "reactions").Add(func(e *core.RecordCreateEvent) error {
return fmt.Errorf("cannot create %s in demo mode", e.Record.Collection().Name)
})
app.OnRecordBeforeUpdateRequest("files", "reactions").Add(func(e *core.RecordUpdateEvent) error {
return fmt.Errorf("cannot update %s in demo mode", e.Record.Collection().Name)
})
app.OnRecordBeforeDeleteRequest("files", "reactions").Add(func(e *core.RecordDeleteEvent) error {
return fmt.Errorf("cannot delete %s in demo mode", e.Record.Collection().Name)
})
}

func dev() bool {
return strings.HasPrefix(os.Args[0], os.TempDir())
}
34 changes: 32 additions & 2 deletions app/routes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"fmt"
"net/http"
"net/http/httputil"
"net/url"
Expand All @@ -12,8 +13,25 @@ import (
"github.com/SecurityBrewery/catalyst/ui"
)

func addRoutes() func(*core.ServeEvent) error {
func setupServer(appURL string, flags []string) func(e *core.ServeEvent) error {
return func(e *core.ServeEvent) error {
if err := SetFlags(e.App, flags); err != nil {
return err
}

if HasFlag(e.App, "demo") {
bindDemoHooks(e.App)
}

if appURL != "" {
s := e.App.Settings()
s.Meta.AppUrl = appURL

if err := e.App.Dao().SaveSettings(s); err != nil {
return err
}
}

e.Router.GET("/", func(c echo.Context) error {
return c.Redirect(http.StatusFound, "/ui/")
})
Expand All @@ -37,10 +55,22 @@ func addRoutes() func(*core.ServeEvent) error {
})
})

return nil
return e.App.RefreshSettings()
}
}

func bindDemoHooks(app core.App) {
app.OnRecordBeforeCreateRequest("files", "reactions").Add(func(e *core.RecordCreateEvent) error {
return fmt.Errorf("cannot create %s in demo mode", e.Record.Collection().Name)
})
app.OnRecordBeforeUpdateRequest("files", "reactions").Add(func(e *core.RecordUpdateEvent) error {
return fmt.Errorf("cannot update %s in demo mode", e.Record.Collection().Name)
})
app.OnRecordBeforeDeleteRequest("files", "reactions").Add(func(e *core.RecordDeleteEvent) error {
return fmt.Errorf("cannot delete %s in demo mode", e.Record.Collection().Name)
})
}

func staticFiles() func(echo.Context) error {
return func(c echo.Context) error {
if dev() {
Expand Down

0 comments on commit d169cd0

Please sign in to comment.