Skip to content

Commit

Permalink
feat: use sortable uid to replace uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
webhookx-x committed Aug 24, 2024
1 parent 2be9f67 commit 45c6e72
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 85 deletions.
88 changes: 44 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,35 +51,35 @@ $ curl http://localhost:8080
##### 1. Create an endpoint

```
curl -X POST 'http://localhost:8080/workspaces/default/endpoints' \
--header 'Content-Type: application/json' \
--data '{
"request": {
"url": "https://httpbin.org/anything",
"method": "POST"
},
"events": [
"charge.succeeded"
]
}'
$ curl -X POST http://localhost:8080/workspaces/default/endpoints \
--header 'Content-Type: application/json' \
--data '{
"request": {
"url": "https://httpbin.org/anything",
"method": "POST"
},
"events": [
"charge.succeeded"
]
}'
```

##### 2. Create a source

```
curl -X POST 'http://localhost:8080/workspaces/default/sources' \
--header 'accept: application/json' \
--header 'Content-Type: application/json' \
--data '{
"path": "/",
"methods": ["POST"]
}'
$ curl -X POST http://localhost:8080/workspaces/default/sources \
--header 'accept: application/json' \
--header 'Content-Type: application/json' \
--data '{
"path": "/",
"methods": ["POST"]
}'
```

#### 3. Send an event to proxy

```
curl -X POST 'http://localhost:8081/' \
$ curl -X POST http://localhost:8081 \
--header 'Content-Type: application/json' \
--data '{
"event_type": "charge.succeeded",
Expand All @@ -92,33 +92,33 @@ curl -X POST 'http://localhost:8081/' \
#### 4. Retrieve delivery attemp

```
curl 'http://localhost:8080/workspaces/default/attempts'
$ curl http://localhost:8080/workspaces/default/attempts
{
"total": 1,
"data": [
{
"id": "c11b4011-623c-4aa0-8c54-4e4672799e15",
"event_id": "dfc3fa33-4e0e-4b43-96ab-c1ad92de67f4",
"endpoint_id": "97d5fecd-f912-43fb-9a22-2073931acbeb",
"status": "SUCCESSFUL",
"attempt_number": 1,
"attempt_at": 1724336398,
"request": {
"method": "POST",
"url": "https://httpbin.org/anything",
"header": {},
"body": "{\"key\": \"value\"}"
},
"response": {
"status": 200,
"header": {},
"body": "{\n \"args\": {}, \n \"data\": \"{\\\"key\\\": \\\"value\\\"}\", \n \"files\": {}, \n \"form\": {}, \n \"headers\": {\n \"Accept-Encoding\": \"gzip\", \n \"Content-Length\": \"16\", \n \"Content-Type\": \"application/json; charset=utf-8\", \n \"Host\": \"httpbin.org\", \n \"User-Agent\": \"WebhookX/dev\", \n \"X-Amzn-Trace-Id\": \"Root=1-66c7490f-044864fb41bb67160fe2a4e3\"\n }, \n \"json\": {\n \"key\": \"value\"\n }, \n \"method\": \"POST\", \n \"origin\": \"117.186.1.161\", \n \"url\": \"https://httpbin.org/anything\"\n}\n"
},
"created_at": 1724307597,
"updated_at": 1724307597
}
]
"total": 1,
"data": [
{
"id": "2l6HMc9FSJHsGqf8ouLdqTGx1GB",
"event_id": "2l6HMYvsWlK35Kz5RzIi1KV1jvl",
"endpoint_id": "2l6HLC2usWDFOj7H4e8dIgEaJO5",
"status": "SUCCESSFUL",
"attempt_number": 1,
"attempt_at": 1724493558,
"request": {
"method": "POST",
"url": "https://httpbin.org/anything",
"header": {},
"body": "{\"key\": \"value\"}"
},
"response": {
"status": 200,
"header": {},
"body": "{\n \"args\": {}, \n \"data\": \"{\\\"key\\\": \\\"value\\\"}\", \n \"files\": {}, \n \"form\": {}, \n \"headers\": {\n \"Accept-Encoding\": \"gzip\", \n \"Content-Length\": \"16\", \n \"Content-Type\": \"application/json; charset=utf-8\", \n \"Host\": \"httpbin.org\", \n \"User-Agent\": \"WebhookX/dev\", \n \"X-Amzn-Trace-Id\": \"Root=1-66c9aef9-214447eb1bcaad151f29744e\"\n }, \n \"json\": {\n \"key\": \"value\"\n }, \n \"method\": \"POST\", \n \"origin\": \"13.114.230.241\", \n \"url\": \"https://httpbin.org/anything\"\n}\n"
},
"created_at": 1724493559,
"updated_at": 1724493559
}
]
}
```

Expand Down
4 changes: 2 additions & 2 deletions admin/api/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,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
event.ID = utils.UUID()
event.ID = utils.KSUID()

if err := json.NewDecoder(r.Body).Decode(&event); err != nil {
api.error(400, w, err)
Expand Down Expand Up @@ -75,7 +75,7 @@ func DispatchEvent(api *API, ctx context.Context, event *entities.Event) error {

for _, endpoint := range endpoints {
attempt := &entities.Attempt{
ID: utils.UUID(),
ID: utils.KSUID(),
EventId: event.ID,
EndpointId: endpoint.ID,
Status: entities.AttemptStatusInit,
Expand Down
2 changes: 1 addition & 1 deletion admin/api/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (api *API) CreateWorkspace(w http.ResponseWriter, r *http.Request) {
return
}

workspace.ID = utils.UUID()
workspace.ID = utils.KSUID()
err := api.DB.Workspaces.Insert(r.Context(), &workspace)
api.assert(err)

Expand Down
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (app *Application) initialize() error {
app.log = zap.S()

// db
db, err := db.NewDB(cfg)
db, err := db.NewDB(&cfg.DatabaseConfig)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func newMigrationsCmd() *cobra.Command {
Short: "print the migration status",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
m := migrator.New(cfg)
m := migrator.New(&cfg.DatabaseConfig)
version, dirty, err := m.Status()
if err != nil {
return err
Expand All @@ -47,7 +47,7 @@ func newMigrationsCmd() *cobra.Command {
Short: "run any new migrations",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
m := migrator.New(cfg)
m := migrator.New(&cfg.DatabaseConfig)
if err := m.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
return err
}
Expand All @@ -62,7 +62,7 @@ func newMigrationsCmd() *cobra.Command {
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
// fixme: add promp
m := migrator.New(cfg)
m := migrator.New(&cfg.DatabaseConfig)
fmt.Println("resetting database...")
if err := m.Reset(); err != nil {
return err
Expand Down
6 changes: 0 additions & 6 deletions db/dao/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ func (dao *DAO[T]) UnsafeDB(ctx context.Context) Queryable {
}

func (dao *DAO[T]) Get(ctx context.Context, id string) (entity *T, err error) {
if ok := utils.IsValidUUID(id); !ok {
return nil, nil
}
builder := psql.Select("*").From(dao.table).Where(sq.Eq{"id": id})
if dao.workspace {
wid := ucontext.GetWorkspaceID(ctx)
Expand Down Expand Up @@ -110,9 +107,6 @@ func (dao *DAO[T]) selectByField(ctx context.Context, field string, value string
}

func (dao *DAO[T]) Delete(ctx context.Context, id string) (bool, error) {
if ok := utils.IsValidUUID(id); !ok {
return false, nil
}
builder := psql.Delete(dao.table).Where(sq.Eq{"id": id})
if dao.workspace {
wid := ucontext.GetWorkspaceID(ctx)
Expand Down
6 changes: 3 additions & 3 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type DB struct {
SourcesWS dao.SourceDAO
}

func initSqlxDB(cfg config.DatabaseConfig) (*sqlx.DB, error) {
func initSqlxDB(cfg *config.DatabaseConfig) (*sqlx.DB, error) {
db, err := cfg.GetDB()
//db.SetMaxOpenConns(100)
//db.SetMaxIdleConns(100)
Expand All @@ -37,8 +37,8 @@ func initSqlxDB(cfg config.DatabaseConfig) (*sqlx.DB, error) {
return sqlx.NewDb(db, "postgres"), nil
}

func NewDB(cfg *config.Config) (*DB, error) {
sqlxDB, err := initSqlxDB(cfg.DatabaseConfig)
func NewDB(cfg *config.DatabaseConfig) (*DB, error) {
sqlxDB, err := initSqlxDB(cfg)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion db/entities/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Endpoint struct {
}

func (m *Endpoint) Init() {
m.ID = utils.UUID()
m.ID = utils.KSUID()
m.Enabled = true
}

Expand Down
2 changes: 1 addition & 1 deletion db/entities/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ func (m *Source) Validate() error {
}

func (m *Source) Init() {
m.ID = utils.UUID()
m.ID = utils.KSUID()
m.Enabled = true
}
24 changes: 11 additions & 13 deletions db/migrations/1_init.up.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE TABLE IF NOT EXISTS "workspaces" (
"id" UUID PRIMARY KEY,
"id" char(27) PRIMARY KEY,
"name" TEXT UNIQUE,
"description" TEXT,
"metadata" JSONB NOT NULL DEFAULT '{}'::jsonb,
Expand All @@ -8,10 +8,8 @@ CREATE TABLE IF NOT EXISTS "workspaces" (
"updated_at" TIMESTAMPTZ DEFAULT (CURRENT_TIMESTAMP(0) AT TIME ZONE 'UTC')
);

INSERT INTO workspaces(id, name) VALUES (gen_random_uuid(), 'default');

CREATE TABLE IF NOT EXISTS "endpoints" (
"id" UUID PRIMARY KEY,
"id" char(27) PRIMARY KEY,
"name" TEXT,
"description" TEXT,
"request" JSONB NOT NULL DEFAULT '{}'::jsonb,
Expand All @@ -20,7 +18,7 @@ CREATE TABLE IF NOT EXISTS "endpoints" (
"events" TEXT[],
"retry" JSONB NOT NULL DEFAULT '{}'::jsonb,

"ws_id" UUID,
"ws_id" char(27),
"created_at" TIMESTAMPTZ DEFAULT (CURRENT_TIMESTAMP(0) AT TIME ZONE 'UTC'),
"updated_at" TIMESTAMPTZ DEFAULT (CURRENT_TIMESTAMP(0) AT TIME ZONE 'UTC')
);
Expand All @@ -29,21 +27,21 @@ CREATE INDEX idx_endpoints_ws_id ON endpoints (ws_id);
CREATE UNIQUE INDEX uk_endpoints_ws_name ON endpoints (ws_id, name);

CREATE TABLE IF NOT EXISTS "events" (
"id" UUID PRIMARY KEY,
"id" char(27) PRIMARY KEY,
"data" JSONB NOT NULL,
"event_type" TEXT NOT NULL,

"ws_id" UUID,
"ws_id" char(27),
"created_at" TIMESTAMPTZ DEFAULT (CURRENT_TIMESTAMP(0) AT TIME ZONE 'UTC'),
"updated_at" TIMESTAMPTZ DEFAULT (CURRENT_TIMESTAMP(0) AT TIME ZONE 'UTC')
);

CREATE INDEX idx_events_ws_id ON events (ws_id);

CREATE TABLE IF NOT EXISTS "attempts" (
"id" UUID PRIMARY KEY,
"event_id" UUID REFERENCES "events" ("id") ON DELETE CASCADE,
"endpoint_id" UUID REFERENCES "endpoints" ("id") ON DELETE CASCADE,
"id" char(27) PRIMARY KEY,
"event_id" char(27) REFERENCES "events" ("id") ON DELETE CASCADE,
"endpoint_id" char(27) REFERENCES "endpoints" ("id") ON DELETE CASCADE,
"status" varchar(20) not null,

"attempt_number" SMALLINT NOT NULL DEFAULT 1,
Expand All @@ -52,7 +50,7 @@ CREATE TABLE IF NOT EXISTS "attempts" (
"request" JSONB,
"response" JSONB,

"ws_id" UUID,
"ws_id" char(27),
"created_at" TIMESTAMPTZ DEFAULT (CURRENT_TIMESTAMP(0) AT TIME ZONE 'UTC'),
"updated_at" TIMESTAMPTZ DEFAULT (CURRENT_TIMESTAMP(0) AT TIME ZONE 'UTC')
);
Expand All @@ -63,15 +61,15 @@ CREATE INDEX idx_attempts_ws_id ON attempts (ws_id);
CREATE INDEX idx_attempts_status ON attempts (status);

CREATE TABLE IF NOT EXISTS "sources" (
"id" UUID PRIMARY KEY,
"id" char(27) PRIMARY KEY,
"name" TEXT UNIQUE,
"enabled" BOOLEAN NOT NULL DEFAULT true,

"path" TEXT,
"methods" TEXT[],
"response" JSONB,

"ws_id" UUID,
"ws_id" char(27),
"created_at" TIMESTAMPTZ DEFAULT (CURRENT_TIMESTAMP(0) AT TIME ZONE 'UTC'),
"updated_at" TIMESTAMPTZ DEFAULT (CURRENT_TIMESTAMP(0) AT TIME ZONE 'UTC')
);
Expand Down
31 changes: 24 additions & 7 deletions db/migrator/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,30 @@ import (
"github.com/golang-migrate/migrate/v4/database/postgres"
"github.com/golang-migrate/migrate/v4/source/iofs"
"github.com/webhookx-io/webhookx/config"
"github.com/webhookx-io/webhookx/db"
"github.com/webhookx-io/webhookx/db/migrations"
"github.com/webhookx-io/webhookx/utils"
)

// Migrator is a database migrator
type Migrator struct {
cfg *config.Config
cfg *config.DatabaseConfig
}

func New(cfg *config.Config) *Migrator {
return &Migrator{
func New(cfg *config.DatabaseConfig) *Migrator {
migrator := &Migrator{
cfg: cfg,
}
return migrator
}

func (m *Migrator) init() (*migrate.Migrate, error) {
db, err := m.cfg.DatabaseConfig.GetDB()
db, err := m.cfg.GetDB()
if err != nil {
return nil, err
}

driver, err := postgres.WithInstance(db, &postgres.Config{
DatabaseName: m.cfg.DatabaseConfig.Database,
DatabaseName: m.cfg.Database,
})
if err != nil {
return nil, err
Expand Down Expand Up @@ -58,7 +60,12 @@ func (m *Migrator) Up() error {
if err != nil {
return err
}
return migrate.Up()
err = migrate.Up()
if err != nil {
return err
}

return m.initDefaultWorkspace()
}

func (m *Migrator) Down() error {
Expand All @@ -77,3 +84,13 @@ func (m *Migrator) Status() (version uint, dirty bool, err error) {
}
return migrate.Version()
}

func (m *Migrator) initDefaultWorkspace() error {
db, err := db.NewDB(m.cfg)
if err != nil {
return err
}
sql := `INSERT INTO workspaces(id, name) VALUES($1, 'default') ON CONFLICT(name) DO NOTHING;`
_, err = db.DB.Exec(sql, utils.KSUID())
return err
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ require (
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/segmentio/ksuid v1.0.4 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTE
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
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.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand Down
Loading

0 comments on commit 45c6e72

Please sign in to comment.