Skip to content

Commit 5f1cd97

Browse files
authored
Support MongoDB only (frain-dev#18)
* support MonGOdb ONLy * migrate org repo to use mongodb * migrate apps to mongo * implement create organisation command * create application * create endpoint * fix tests * update workflow * update workflow * update workflow * update workflow
1 parent b331e2a commit 5f1cd97

25 files changed

+692
-749
lines changed

.github/workflows/go.yml

+28-28
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,38 @@ jobs:
1010
strategy:
1111
matrix:
1212
go-version: [1.15.x, 1.16.x]
13-
os: [ubuntu-latest, macos-latest, windows-latest]
13+
os: [ubuntu-latest, macos-latest]
14+
mongodb-version: ['4.0', '4.2', '4.4']
15+
1416
runs-on: ubuntu-latest
1517

16-
services:
17-
postgres:
18-
image: postgres:13
19-
env:
20-
POSTGRES_PASSWORD: postgres
21-
POSTGRES_USER: hookcamptest
22-
ports: ["5432:5432"]
23-
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
18+
# services:
19+
# postgres:
20+
# image: postgres:13
21+
# env:
22+
# POSTGRES_PASSWORD: postgres
23+
# POSTGRES_USER: hookcamptest
24+
# ports: ["5432:5432"]
25+
# options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
2426

25-
mysql:
26-
image: mysql:5.7
27-
env:
28-
MYSQL_DATABASE: hookcamptest
29-
MYSQL_USER: hookcamptest
30-
MYSQL_PASSWORD: mysql
31-
MYSQL_ROOT_PASSWORD: mysql
32-
ports:
33-
- 3306:3306
34-
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
27+
# mysql:
28+
# image: mysql:5.7
29+
# env:
30+
# MYSQL_DATABASE: hookcamptest
31+
# MYSQL_USER: hookcamptest
32+
# MYSQL_PASSWORD: mysql
33+
# MYSQL_ROOT_PASSWORD: mysql
34+
# ports:
35+
# - 3306:3306
36+
# options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
3537

3638
steps:
3739

40+
- name: Start MongoDB
41+
uses: supercharge/mongodb-github-action@1.4.1
42+
with:
43+
mongodb-version: ${{ matrix.mongodb-version }}
44+
3845
- name: Get the version
3946
id: get_version
4047
run: echo ::set-output name=tag::$(echo ${GITHUB_SHA:8})
@@ -63,15 +70,8 @@ jobs:
6370
- name: Go vet
6471
run: go vet ./...
6572

66-
- name: Run postgres tests
67-
run: go test -tags integration -v ./...
68-
env:
69-
TEST_DATABASE_DSN: "postgresql://hookcamptest:postgres@localhost:5432/hookcamptest?sslmode=disable"
70-
TEST_DB_TYPE: postgres
71-
72-
- name: Run mysql tests
73+
- name: Run Mongo integration tests
7374
run: go test -tags integration -v ./...
7475
env:
75-
TEST_DATABASE_DSN: "hookcamptest:mysql@tcp(localhost:3306)/hookcamptest?parseTime=True"
76-
TEST_DB_TYPE: mysql
76+
TEST_DATABASE_DSN: "mongodb://localhost:27017"
7777

application.go

+20-39
Original file line numberDiff line numberDiff line change
@@ -4,60 +4,41 @@ import (
44
"context"
55
"errors"
66

7-
"github.com/google/uuid"
8-
"gorm.io/gorm"
7+
"go.mongodb.org/mongo-driver/bson/primitive"
98
)
109

1110
var (
12-
// ErrApplicationNotFound is returned when an application cannot be
13-
// found
1411
ErrApplicationNotFound = errors.New("application not found")
1512

16-
// ErrEndpointNotFound is returned when an endpoint cannot be found
1713
ErrEndpointNotFound = errors.New("endpoint not found")
1814
)
1915

20-
// Application defines an entity that can receive webhooks.
2116
type Application struct {
22-
ID uuid.UUID `json:"id" gorm:"type:varchar(220);uniqueIndex;not null"`
23-
OrgID uuid.UUID `json:"org_id" gorm:"not null"`
24-
Title string `json:"name" gorm:"not null;type:varchar(200)"`
25-
26-
gorm.Model
27-
Organisation Organisation `json:"organisation" gorm:"foreignKey:OrgID"`
17+
ID primitive.ObjectID `json:"-" bson:"_id"`
18+
UID string `json:"uid" bson:"uid"`
19+
OrgID string `json:"org_id" bson:"org_id"`
20+
Title string `json:"name" bson:"title"`
21+
22+
Endpoints []Endpoint `json:"endpoints" bson:"endpoints"`
23+
CreatedAt int64 `json:"created_at" bson:"created_at"`
24+
UpdatedAt int64 `json:"updated_at" bson:"updated_at"`
25+
DeletedAt int64 `json:"deleted_at" bson:"deleted_at"`
2826
}
2927

30-
// Endpoint defines a target service that can be reached in an application
3128
type Endpoint struct {
32-
ID uuid.UUID `json:"id" gorm:"type:varchar(220);uniqueIndex;not null"`
33-
AppID uuid.UUID `json:"app_id" gorm:"size:200;not null"`
34-
TargetURL string `json:"target_url" gorm:"not null"`
35-
Secret string `json:"secret" gorm:"type:varchar(200);uniqueIndex;not null"`
36-
Description string `json:"description" gorm:"size:220;default:''"`
37-
38-
Application Application `json:"-" gorm:"foreignKey:AppID"`
39-
gorm.Model
29+
UID string `json:"uid" bson:"uid"`
30+
TargetURL string `json:"target_url" bson:"target_url"`
31+
Secret string `json:"secret" bson:"secret"`
32+
Description string `json:"description" bson:"description"`
33+
34+
CreatedAt int64 `json:"created_at" bson:"created_at"`
35+
UpdatedAt int64 `json:"updated_at" bson:"updated_at"`
36+
DeletedAt int64 `json:"deleted_at" bson:"deleted_at"`
4037
}
4138

42-
// ApplicationRepository is an abstraction over all database operations of an
43-
// application
4439
type ApplicationRepository interface {
45-
// CreateApplication when called persists an application to the database
4640
CreateApplication(context.Context, *Application) error
47-
48-
// LoadApplications fetches a list of all apps from the database
4941
LoadApplications(context.Context) ([]Application, error)
50-
51-
// FindApplicationByID looks for an application by the provided ID.
52-
FindApplicationByID(context.Context, uuid.UUID) (*Application, error)
53-
}
54-
55-
// EndpointRepository is an abstraction over all endpoint operations with the
56-
// database
57-
type EndpointRepository interface {
58-
// CreateEndpoint adds a new endpoint to the database
59-
CreateEndpoint(context.Context, *Endpoint) error
60-
61-
// FindEndpointByID retrieves an endpoint by the proovided ID
62-
FindEndpointByID(context.Context, uuid.UUID) (*Endpoint, error)
42+
FindApplicationByID(context.Context, string) (*Application, error)
43+
UpdateApplication(context.Context, *Application) error
6344
}

cmd/application.go

+53-59
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@ package main
22

33
import (
44
"errors"
5-
"fmt"
6-
"os"
75

8-
"github.com/google/uuid"
9-
"github.com/hookcamp/hookcamp"
10-
"github.com/hookcamp/hookcamp/util"
11-
"github.com/olekukonko/tablewriter"
126
"github.com/spf13/cobra"
137
)
148

@@ -31,22 +25,22 @@ func listApplications(a *app) *cobra.Command {
3125
Use: "list",
3226
Short: "List all applications",
3327
RunE: func(cmd *cobra.Command, args []string) error {
34-
ctx, cancelFn := getCtx()
35-
defer cancelFn()
28+
// ctx, cancelFn := getCtx()
29+
// defer cancelFn()
3630

37-
apps, err := a.applicationRepo.LoadApplications(ctx)
38-
if err != nil {
39-
return err
40-
}
31+
// apps, err := a.applicationRepo.LoadApplications(ctx)
32+
// if err != nil {
33+
// return err
34+
// }
4135

42-
table := tablewriter.NewWriter(os.Stdout)
43-
table.SetHeader([]string{"ID", "Name", "Org name", "Created at"})
36+
// table := tablewriter.NewWriter(os.Stdout)
37+
// table.SetHeader([]string{"ID", "Name", "Org name", "Created at"})
4438

45-
for _, app := range apps {
46-
table.Append([]string{app.ID.String(), app.Title, app.Organisation.OrgName, app.CreatedAt.String()})
47-
}
39+
// for _, app := range apps {
40+
// table.Append([]string{app.UID.String(), app.Title, app.Organisation.OrgName, app.CreatedAt.String()})
41+
// }
4842

49-
table.Render()
43+
// table.Render()
5044
return nil
5145
},
5246
}
@@ -62,40 +56,40 @@ func createApplication(a *app) *cobra.Command {
6256
Use: "create",
6357
Short: "Create an application",
6458
RunE: func(cmd *cobra.Command, args []string) error {
65-
if util.IsStringEmpty(name) {
66-
return errors.New("please provide application name")
67-
}
59+
// if util.IsStringEmpty(name) {
60+
// return errors.New("please provide application name")
61+
// }
6862

69-
if util.IsStringEmpty(orgID) {
70-
return errors.New("please provide the org ID")
71-
}
63+
// if util.IsStringEmpty(orgID) {
64+
// return errors.New("please provide the org ID")
65+
// }
7266

73-
id, err := uuid.Parse(orgID)
74-
if err != nil {
75-
return fmt.Errorf("could not parse org ID..%w", err)
76-
}
67+
// id, err := uuid.Parse(orgID)
68+
// if err != nil {
69+
// return fmt.Errorf("could not parse org ID..%w", err)
70+
// }
7771

78-
ctx, cancelFn := getCtx()
79-
defer cancelFn()
72+
// ctx, cancelFn := getCtx()
73+
// defer cancelFn()
8074

81-
org, err := a.orgRepo.FetchOrganisationByID(ctx, id)
82-
if err != nil {
83-
return err
84-
}
75+
// org, err := a.orgRepo.FetchOrganisationByID(ctx, id)
76+
// if err != nil {
77+
// return err
78+
// }
8579

86-
app := &hookcamp.Application{
87-
Title: name,
88-
OrgID: org.ID,
89-
}
80+
// app := &hookcamp.Application{
81+
// Title: name,
82+
// OrgID: org.ID,
83+
// }
9084

91-
ctx, cancelFn = getCtx()
92-
defer cancelFn()
85+
// ctx, cancelFn = getCtx()
86+
// defer cancelFn()
9387

94-
if err := a.applicationRepo.CreateApplication(ctx, app); err != nil {
95-
return err
96-
}
88+
// if err := a.applicationRepo.CreateApplication(ctx, app); err != nil {
89+
// return err
90+
// }
9791

98-
fmt.Printf("Your application was successfully created. ID = %s \n", app.ID)
92+
// fmt.Printf("Your application was successfully created. ID = %s \n", app.ID)
9993
return nil
10094
},
10195
}
@@ -118,27 +112,27 @@ func getApplicationCommand(a *app) *cobra.Command {
118112
return nil
119113
},
120114
RunE: func(cmd *cobra.Command, args []string) error {
121-
ID := args[0]
115+
// ID := args[0]
122116

123-
appID, err := uuid.Parse(ID)
124-
if err != nil {
125-
return fmt.Errorf("Please provide a valid ID..%w", err)
126-
}
117+
// appID, err := uuid.Parse(ID)
118+
// if err != nil {
119+
// return fmt.Errorf("Please provide a valid ID..%w", err)
120+
// }
127121

128-
ctx, cancelFn := getCtx()
129-
defer cancelFn()
122+
// ctx, cancelFn := getCtx()
123+
// defer cancelFn()
130124

131-
app, err := a.applicationRepo.FindApplicationByID(ctx, appID)
132-
if err != nil {
133-
return fmt.Errorf("could not fetch app ID..%w", err)
134-
}
125+
// app, err := a.applicationRepo.FindApplicationByID(ctx, appID)
126+
// if err != nil {
127+
// return fmt.Errorf("could not fetch app ID..%w", err)
128+
// }
135129

136-
table := tablewriter.NewWriter(os.Stdout)
137-
table.SetHeader([]string{"ID", "Description"})
130+
// table := tablewriter.NewWriter(os.Stdout)
131+
// table.SetHeader([]string{"ID", "Description"})
138132

139-
table.Append([]string{app.ID.String(), app.Title})
133+
// table.Append([]string{app.ID.String(), app.Title})
140134

141-
table.Render()
135+
// table.Render()
142136
return nil
143137
},
144138
}

0 commit comments

Comments
 (0)