Compare commits

...

6 Commits

Author SHA1 Message Date
Jonas Plum
7b92d59dff fix: relative day display (#1116) 2024-11-29 23:11:11 +01:00
Jonas Plum
6a8c92f1f6 fix: server setup (#1115) 2024-11-08 21:56:32 +01:00
Jonas Plum
9285aec468 fix: docker entrypoint permissions (#1114) 2024-11-06 02:12:09 +01:00
Jonas Plum
97d0cd3428 fix: goreleaser docker (#1113) 2024-11-06 02:02:47 +01:00
Jonas Plum
baba5b7a45 feat: docker entrypoint with environment variables (#1112) 2024-11-06 01:52:48 +01:00
Jonas Plum
d1cf75ab79 refactor: subcommands (#1111) 2024-11-06 01:21:31 +01:00
10 changed files with 85 additions and 79 deletions

View File

@@ -18,6 +18,8 @@ dockers:
- "ghcr.io/securitybrewery/catalyst:main"
- "{{if not .Prerelease}}ghcr.io/securitybrewery/catalyst:latest{{end}}"
- "ghcr.io/securitybrewery/catalyst:{{.Tag}}"
extra_files:
- docker/entrypoint.sh
archives:
- format: tar.gz

View File

@@ -60,18 +60,16 @@ dev:
@echo "Running..."
rm -rf catalyst_data
go run . admin create admin@catalyst-soar.com 1234567890
go run . set-feature-flags dev
go run . fake-data
go run . serve
go run . serve --app-url http://localhost:8090 --flags dev
.PHONY: dev-10000
dev-10000:
@echo "Running..."
rm -rf catalyst_data
go run . admin create admin@catalyst-soar.com 1234567890
go run . set-feature-flags dev
go run . fake-data --users 100 --tickets 10000
go run . serve
go run . serve --app-url http://localhost:8090 --flags dev
.PHONY: serve-ui
serve-ui:

View File

@@ -1,7 +1,6 @@
package app
import (
"fmt"
"os"
"strings"
@@ -23,47 +22,30 @@ func App(dir string, test bool) (*pocketbase.PocketBase, error) {
DefaultDataDir: dir,
})
var appURL string
app.RootCmd.PersistentFlags().StringVar(&appURL, "app-url", "", "the app's URL")
var flags []string
app.RootCmd.PersistentFlags().StringSliceVar(&flags, "flags", nil, "feature flags")
_ = app.RootCmd.ParseFlags(os.Args[1:])
app.RootCmd.AddCommand(fakeDataCmd(app))
webhook.BindHooks(app)
reaction.BindHooks(app, test)
app.OnBeforeServe().Add(addRoutes())
app.OnAfterBootstrap().Add(func(e *core.BootstrapEvent) error {
if HasFlag(e.App, "demo") {
bindDemoHooks(e.App)
}
return nil
return MigrateDBs(e.App)
})
// Register additional commands
app.RootCmd.AddCommand(fakeDataCmd(app))
app.RootCmd.AddCommand(setFeatureFlagsCmd(app))
app.RootCmd.AddCommand(setAppURL(app))
if err := app.Bootstrap(); err != nil {
return nil, err
}
if err := MigrateDBs(app); err != nil {
return nil, err
}
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())
}

View File

@@ -12,10 +12,8 @@ func fakeDataCmd(app core.App) *cobra.Command {
cmd := &cobra.Command{
Use: "fake-data",
Run: func(_ *cobra.Command, _ []string) {
if err := fakedata.Generate(app, userCount, ticketCount); err != nil {
app.Logger().Error(err.Error())
}
RunE: func(_ *cobra.Command, _ []string) error {
return fakedata.Generate(app, userCount, ticketCount)
},
}

View File

@@ -6,7 +6,6 @@ import (
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/models"
"github.com/spf13/cobra"
"github.com/SecurityBrewery/catalyst/migrations"
)
@@ -85,35 +84,3 @@ func SetFlags(app core.App, args []string) error {
return nil
}
func setFeatureFlagsCmd(app core.App) *cobra.Command {
return &cobra.Command{
Use: "set-feature-flags",
Run: func(_ *cobra.Command, args []string) {
if err := SetFlags(app, args); err != nil {
app.Logger().Error(err.Error())
}
},
}
}
func setAppURL(app core.App) *cobra.Command {
return &cobra.Command{
Use: "set-app-url",
Run: func(_ *cobra.Command, args []string) {
if len(args) != 1 {
app.Logger().Error("missing app url")
return
}
settings := app.Settings()
settings.Meta.AppUrl = args[0]
if err := app.Dao().SaveSettings(settings); err != nil {
app.Logger().Error(err.Error())
}
},
}
}

View File

@@ -1,6 +1,7 @@
package app
import (
"fmt"
"net/http"
"net/http/httputil"
"net/url"
@@ -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/")
})
@@ -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() {

View File

@@ -11,4 +11,8 @@ VOLUME /usr/local/bin/catalyst_data
HEALTHCHECK --interval=5s --timeout=3s --retries=3 \
CMD curl -f http://localhost:8080/health || exit 1
CMD ["/usr/local/bin/catalyst", "serve", "--http", "0.0.0.0:8080"]
COPY docker/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
CMD ["/entrypoint.sh"]

15
docker/entrypoint.sh Normal file
View File

@@ -0,0 +1,15 @@
#!/bin/bash
# Set the flags
FLAGS=""
if [ -n "$CATALYST_FLAGS" ]; then
FLAGS="$CATALYST_FLAGS"
fi
# Set the app url
APP_URL=""
if [ -n "$CATALYST_APP_URL" ]; then
APP_URL="$CATALYST_APP_URL"
fi
/usr/local/bin/catalyst serve --http 0.0.0.0:8080 --flags "$FLAGS" --app-url "$APP_URL"

View File

@@ -26,6 +26,10 @@ func App(t *testing.T) (*pocketbase.PocketBase, *Counter, func()) {
t.Fatal(err)
}
if err := baseApp.Bootstrap(); err != nil {
t.Fatal(fmt.Errorf("failed to bootstrap: %w", err))
}
baseApp.Settings().Logs.MaxDays = 0
defaultTestData(t, baseApp)

View File

@@ -30,8 +30,14 @@ const {
}
})
const age = (ticket: Ticket) =>
intervalToDuration({ start: new Date(ticket.created), end: new Date() }).days
const age = (ticket: Ticket) => {
const days = intervalToDuration({ start: new Date(ticket.created), end: new Date() }).days
if (days === 0) return 'today'
if (days === 1) return 'yesterday'
return `${days} days`
}
</script>
<template>
@@ -45,7 +51,7 @@ const age = (ticket: Ticket) =>
<Separator orientation="vertical" class="hidden h-4 sm:block" />
<span class="text-sm text-muted-foreground">{{ ticket.expand.type.singular }}</span>
<Separator orientation="vertical" class="hidden h-4 sm:block" />
<span class="text-sm text-muted-foreground">Open since {{ age(ticket) }} days</span>
<span class="text-sm text-muted-foreground">Open since {{ age(ticket) }}</span>
<RouterLink
:to="{
name: 'tickets',