Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion admin/api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/webhookx-io/webhookx/pkg/contextx"
"github.com/webhookx-io/webhookx/pkg/openapi"
"github.com/webhookx-io/webhookx/pkg/types"
"github.com/webhookx-io/webhookx/pkg/uid"
"github.com/webhookx-io/webhookx/utils"
)

Expand Down Expand Up @@ -45,7 +46,7 @@ func (api *API) GetEndpoint(w http.ResponseWriter, r *http.Request) {

func (api *API) CreateEndpoint(w http.ResponseWriter, r *http.Request) {
var endpoint entities.Endpoint
defaults := map[string]interface{}{"id": utils.KSUID()}
defaults := map[string]interface{}{"id": uid.Generate(uid.EndpointPrefix)}
if err := ValidateRequest(r, defaults, &endpoint); err != nil {
api.error(400, w, err)
return
Expand Down
4 changes: 2 additions & 2 deletions admin/api/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/webhookx-io/webhookx/pkg/contextx"
"github.com/webhookx-io/webhookx/pkg/openapi"
"github.com/webhookx-io/webhookx/pkg/types"
"github.com/webhookx-io/webhookx/pkg/uid"
"github.com/webhookx-io/webhookx/services/eventbus"
"github.com/webhookx-io/webhookx/utils"
)

func (api *API) PageEvent(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -48,7 +48,7 @@ func (api *API) GetEvent(w http.ResponseWriter, r *http.Request) {

func (api *API) CreateEvent(w http.ResponseWriter, r *http.Request) {
var event entities.Event
defaults := map[string]interface{}{"id": utils.KSUID()}
defaults := map[string]interface{}{"id": uid.Generate(uid.EventPrefix)}
if err := ValidateRequest(r, defaults, &event); err != nil {
api.error(400, w, err)
return
Expand Down
3 changes: 2 additions & 1 deletion admin/api/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/webhookx-io/webhookx/pkg/errs"
"github.com/webhookx-io/webhookx/pkg/openapi"
"github.com/webhookx-io/webhookx/pkg/types"
"github.com/webhookx-io/webhookx/pkg/uid"
"github.com/webhookx-io/webhookx/utils"
)

Expand Down Expand Up @@ -46,7 +47,7 @@ func (api *API) GetPlugin(w http.ResponseWriter, r *http.Request) {

func (api *API) CreatePlugin(w http.ResponseWriter, r *http.Request) {
var model entities.Plugin
defaults := map[string]interface{}{"id": utils.KSUID()}
defaults := map[string]interface{}{"id": uid.Generate(uid.PluginPrefix)}
if err := ValidateRequest(r, defaults, &model); err != nil {
api.error(400, w, err)
return
Expand Down
3 changes: 2 additions & 1 deletion admin/api/sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/webhookx-io/webhookx/pkg/contextx"
"github.com/webhookx-io/webhookx/pkg/openapi"
"github.com/webhookx-io/webhookx/pkg/types"
"github.com/webhookx-io/webhookx/pkg/uid"
"github.com/webhookx-io/webhookx/utils"
)

Expand Down Expand Up @@ -45,7 +46,7 @@ func (api *API) GetSource(w http.ResponseWriter, r *http.Request) {

func (api *API) CreateSource(w http.ResponseWriter, r *http.Request) {
var source entities.Source
defaults := map[string]interface{}{"id": utils.KSUID()}
defaults := map[string]interface{}{"id": uid.Generate(uid.SourcePrefix)}
if err := ValidateRequest(r, defaults, &source); err != nil {
api.error(400, w, err)
return
Expand Down
5 changes: 3 additions & 2 deletions admin/api/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/webhookx-io/webhookx/db/entities"
"github.com/webhookx-io/webhookx/pkg/openapi"
"github.com/webhookx-io/webhookx/pkg/types"
"github.com/webhookx-io/webhookx/pkg/uid"
"github.com/webhookx-io/webhookx/utils"
)

Expand Down Expand Up @@ -45,13 +46,13 @@ func (api *API) GetWorkspace(w http.ResponseWriter, r *http.Request) {

func (api *API) CreateWorkspace(w http.ResponseWriter, r *http.Request) {
var workspace entities.Workspace
defaults := map[string]interface{}{"id": utils.KSUID()}
defaults := map[string]interface{}{"id": uid.Generate(uid.WorkspacePrefix)}
if err := ValidateRequest(r, defaults, &workspace); err != nil {
api.error(400, w, err)
return
}

workspace.ID = utils.KSUID()
workspace.ID = uid.Generate(uid.WorkspacePrefix)
err := api.db.Workspaces.Insert(r.Context(), &workspace)
api.assert(err)

Expand Down
29 changes: 29 additions & 0 deletions db/migrations/1778517090_typeid.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
ALTER TABLE "attempt_details"
ALTER COLUMN "id" TYPE CHAR(27),
ALTER COLUMN "ws_id" TYPE CHAR(27);

ALTER TABLE "attempts"
ALTER COLUMN "id" TYPE CHAR(27),
ALTER COLUMN "event_id" TYPE CHAR(27),
ALTER COLUMN "endpoint_id" TYPE CHAR(27),
ALTER COLUMN "ws_id" TYPE CHAR(27);

ALTER TABLE "endpoints"
ALTER COLUMN "id" TYPE CHAR(27),
ALTER COLUMN "ws_id" TYPE CHAR(27);

ALTER TABLE "events"
ALTER COLUMN "id" TYPE CHAR(27),
ALTER COLUMN "ws_id" TYPE CHAR(27);

ALTER TABLE "plugins"
ALTER COLUMN "id" TYPE CHAR(27),
ALTER COLUMN "source_id" TYPE CHAR(27),
ALTER COLUMN "endpoint_id" TYPE CHAR(27),
ALTER COLUMN "ws_id" TYPE CHAR(27);

ALTER TABLE "sources"
ALTER COLUMN "id" TYPE CHAR(27),
ALTER COLUMN "ws_id" TYPE CHAR(27);

ALTER TABLE "workspaces" ALTER COLUMN "id" TYPE CHAR(27);
29 changes: 29 additions & 0 deletions db/migrations/1778517090_typeid.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
ALTER TABLE "attempt_details"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

COLLATE C ?

ALTER COLUMN "id" TYPE TEXT,
ALTER COLUMN "ws_id" TYPE TEXT;

ALTER TABLE "attempts"
ALTER COLUMN "id" TYPE TEXT,
ALTER COLUMN "event_id" TYPE TEXT,
ALTER COLUMN "endpoint_id" TYPE TEXT,
ALTER COLUMN "ws_id" TYPE TEXT;

ALTER TABLE "endpoints"
ALTER COLUMN "id" TYPE TEXT,
ALTER COLUMN "ws_id" TYPE TEXT;

ALTER TABLE "events"
ALTER COLUMN "id" TYPE TEXT,
ALTER COLUMN "ws_id" TYPE TEXT;

ALTER TABLE "plugins"
ALTER COLUMN "id" TYPE TEXT,
ALTER COLUMN "source_id" TYPE TEXT,
ALTER COLUMN "endpoint_id" TYPE TEXT,
ALTER COLUMN "ws_id" TYPE TEXT;

ALTER TABLE "sources"
ALTER COLUMN "id" TYPE TEXT,
ALTER COLUMN "ws_id" TYPE TEXT;

ALTER TABLE "workspaces" ALTER COLUMN "id" TYPE TEXT;
4 changes: 2 additions & 2 deletions db/migrator/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/golang-migrate/migrate/v4/database/postgres"
"github.com/golang-migrate/migrate/v4/source/iofs"
"github.com/webhookx-io/webhookx/db/migrations"
"github.com/webhookx-io/webhookx/utils"
"github.com/webhookx-io/webhookx/pkg/uid"
)

type Log struct{}
Expand Down Expand Up @@ -185,6 +185,6 @@ func (m *Migrator) Status() (status Status, err error) {

func (m *Migrator) initDefaultWorkspace() error {
sql := `INSERT INTO workspaces(id, name) VALUES($1, 'default') ON CONFLICT(name) DO NOTHING;`
_, err := m.db.Exec(sql, utils.KSUID())
_, err := m.db.Exec(sql, uid.Generate(uid.WorkspacePrefix))
return err
}
4 changes: 2 additions & 2 deletions dispatcher/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/webhookx-io/webhookx/pkg/metrics"
"github.com/webhookx-io/webhookx/pkg/tracing"
"github.com/webhookx-io/webhookx/pkg/types"
"github.com/webhookx-io/webhookx/pkg/uid"
"github.com/webhookx-io/webhookx/services/eventbus"
"github.com/webhookx-io/webhookx/utils"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -122,7 +122,7 @@ func fanout(event *entities.Event, endpoints []*entities.Endpoint, mode entities
for _, endpoint := range endpoints {
delay := endpoint.Retry.Config.Attempts[0]
attempt := &entities.Attempt{
ID: utils.KSUID(),
ID: uid.Generate(uid.AttemptPrefix),
EventId: event.ID,
EndpointId: endpoint.ID,
Status: entities.AttemptStatusInit,
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ require (
github.com/rs/zerolog v1.35.1
github.com/santhosh-tekuri/jsonschema/v6 v6.0.2
github.com/satori/go.uuid v1.2.0
github.com/segmentio/ksuid v1.0.4
github.com/spf13/cobra v1.10.2
github.com/stretchr/testify v1.11.1
github.com/stripe/stripe-go/v84 v84.4.1
github.com/tetratelabs/wazero v1.11.0
github.com/tidwall/gjson v1.18.0
github.com/vmihailenco/msgpack/v5 v5.4.1
github.com/webhookx-io/webhookx/api/license v0.1.0
go.jetify.com/typeid/v2 v2.0.0-alpha.3
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.68.0
go.opentelemetry.io/contrib/propagators/autoprop v0.68.0
go.opentelemetry.io/otel v1.43.0
Expand Down Expand Up @@ -133,6 +133,7 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/gofrs/uuid/v5 v5.3.2 // indirect
github.com/google/go-cmp v0.7.0 // indirect
github.com/google/pprof v0.0.0-20260402051712-545e8a4df936 // indirect
github.com/google/uuid v1.6.0 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ github.com/go-test/deep v1.1.1 h1:0r/53hagsehfO4bzD2Pgr/+RgHqhmf+k1Bpse2cTu1U=
github.com/go-test/deep v1.1.1/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
github.com/gofrs/uuid/v5 v5.3.2 h1:2jfO8j3XgSwlz/wHqemAEugfnTlikAYHhnqQ8Xh4fE0=
github.com/gofrs/uuid/v5 v5.3.2/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang-migrate/migrate/v4 v4.19.1 h1:OCyb44lFuQfYXYLx1SCxPZQGU7mcaZ7gH9yH4jSFbBA=
Expand Down Expand Up @@ -285,8 +287,6 @@ github.com/santhosh-tekuri/jsonschema/v6 v6.0.2 h1:KRzFb2m7YtdldCEkzs6KqmJw4nqEV
github.com/santhosh-tekuri/jsonschema/v6 v6.0.2/go.mod h1:JXeL+ps8p7/KNMjDQk3TCwPpBy0wYklyWTfbkIzdIFU=
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/segmentio/ksuid v1.0.4 h1:sBo2BdShXjmcugAMwjugoGUdUV0pcxY5mW4xKRn3v4c=
github.com/segmentio/ksuid v1.0.4/go.mod h1:/XUiZBD3kVx5SmUOl55voK5yeAbBNNIed+2O73XgrPE=
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
Expand Down Expand Up @@ -324,6 +324,8 @@ github.com/woodsbury/decimal128 v1.3.0 h1:8pffMNWIlC0O5vbyHWFZAt5yWvWcrHA+3ovIIj
github.com/woodsbury/decimal128 v1.3.0/go.mod h1:C5UTmyTjW3JftjUFzOVhC20BEQa2a4ZKOB5I6Zjb+ds=
github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs=
github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s=
go.jetify.com/typeid/v2 v2.0.0-alpha.3 h1:T6RPx6bNl10lp0JN2Xz/XcgLZWSlVmL58Xqy9cgTCcc=
go.jetify.com/typeid/v2 v2.0.0-alpha.3/go.mod h1:zfD1ZDHDJNgXZANsO9jDOD81XRRQ0zAOnDBEHmIV/Gw=
go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.68.0 h1:CqXxU8VOmDefoh0+ztfGaymYbhdB/tT3zs79QaZTNGY=
Expand Down
9 changes: 5 additions & 4 deletions pkg/declarative/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package declarative

import (
"github.com/webhookx-io/webhookx/db/entities"
"github.com/webhookx-io/webhookx/pkg/uid"
"github.com/webhookx-io/webhookx/utils"
)

Expand All @@ -19,22 +20,22 @@ func (cfg *Configuration) SchemaName() string {
func (cfg *Configuration) Init() {
for _, m := range cfg.Sources {
if m.ID == "" {
m.ID = utils.KSUID()
m.ID = uid.Generate(uid.SourcePrefix)
}
for _, p := range m.Plugins {
if p.ID == "" {
p.ID = utils.KSUID()
p.ID = uid.Generate(uid.PluginPrefix)
}
p.SourceId = new(m.ID)
}
}
for _, m := range cfg.Endpoints {
if m.ID == "" {
m.ID = utils.KSUID()
m.ID = uid.Generate(uid.EndpointPrefix)
}
for _, p := range m.Plugins {
if p.ID == "" {
p.ID = utils.KSUID()
p.ID = uid.Generate(uid.PluginPrefix)
}
p.EndpointId = new(m.ID)
}
Expand Down
29 changes: 29 additions & 0 deletions pkg/uid/uid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package uid

import (
"go.jetify.com/typeid/v2"
)

type Prefix string

func (p Prefix) String() string {
return string(p)
}

const (
AttemptPrefix Prefix = "at"
EndpointPrefix Prefix = "end"
EventPrefix Prefix = "evt"
PluginPrefix Prefix = "plg"
SourcePrefix Prefix = "src"
WorkspacePrefix Prefix = "ws"
)

func Generate(prefix Prefix) string {
return genTypeID(prefix.String())
}


func genTypeID(prefix string) string {
return typeid.MustGenerate(prefix).String()
}
35 changes: 35 additions & 0 deletions pkg/uid/uid_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package uid

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func Test(t *testing.T) {
t.Run("attempt prefix", func(t *testing.T) {
id := Generate(AttemptPrefix)
assert.True(t, strings.HasPrefix(id, "at_"))
})
t.Run("endpoint prefix", func(t *testing.T) {
id := Generate(EndpointPrefix)
assert.True(t, strings.HasPrefix(id, "end_"))
})
t.Run("event prefix", func(t *testing.T) {
id := Generate(EventPrefix)
assert.True(t, strings.HasPrefix(id, "evt_"))
})
t.Run("plugin prefix", func(t *testing.T) {
id := Generate(PluginPrefix)
assert.True(t, strings.HasPrefix(id, "plg_"))
})
t.Run("source prefix", func(t *testing.T) {
id := Generate(SourcePrefix)
assert.True(t, strings.HasPrefix(id, "src_"))
})
t.Run("workspace prefix", func(t *testing.T) {
id := Generate(WorkspacePrefix)
assert.True(t, strings.HasPrefix(id, "ws_"))
})
}
3 changes: 2 additions & 1 deletion proxy/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/webhookx-io/webhookx/pkg/store"
"github.com/webhookx-io/webhookx/pkg/tracing"
"github.com/webhookx-io/webhookx/pkg/types"
"github.com/webhookx-io/webhookx/pkg/uid"
"github.com/webhookx-io/webhookx/plugins"
"github.com/webhookx-io/webhookx/proxy/router"
"github.com/webhookx-io/webhookx/services"
Expand Down Expand Up @@ -279,7 +280,7 @@ func (g *Gateway) handleRequest(w http.ResponseWriter, r *http.Request) (*Respon
}
}

event.ID = utils.KSUID()
event.ID = uid.Generate(uid.EventPrefix)
event.IngestedAt = types.Time{Time: time.Now()}
event.WorkspaceId = source.WorkspaceId
if err := event.Validate(); err != nil {
Expand Down
Loading
Loading