Skip to content

Commit 10e3844

Browse files
committed
Create cloudevents
1 parent b5ec8d2 commit 10e3844

22 files changed

Lines changed: 396 additions & 5 deletions

api/go.mod

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@ go 1.18
44

55
require (
66
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
7+
github.com/cloudevents/sdk-go/v2 v2.10.0
78
github.com/davecgh/go-spew v1.1.1
89
github.com/go-redis/redis/v8 v8.11.5
910
github.com/gofiber/fiber/v2 v2.34.0
1011
github.com/gofiber/swagger v0.0.1
12+
github.com/google/uuid v1.3.0
1113
github.com/joho/godotenv v1.4.0
1214
github.com/palantir/stacktrace v0.0.0-20161112013806-78658fd2d177
1315
github.com/rs/zerolog v1.26.1
1416
github.com/swaggo/swag v1.8.2
1517
go.opentelemetry.io/otel v1.7.0
1618
go.opentelemetry.io/otel/trace v1.7.0
19+
gorm.io/driver/postgres v1.3.7
20+
gorm.io/gorm v1.23.5
1721
)
1822

1923
require (
@@ -25,15 +29,34 @@ require (
2529
github.com/go-openapi/jsonreference v0.20.0 // indirect
2630
github.com/go-openapi/spec v0.20.6 // indirect
2731
github.com/go-openapi/swag v0.21.1 // indirect
32+
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
33+
github.com/jackc/pgconn v1.12.1 // indirect
34+
github.com/jackc/pgio v1.0.0 // indirect
35+
github.com/jackc/pgpassfile v1.0.0 // indirect
36+
github.com/jackc/pgproto3/v2 v2.3.0 // indirect
37+
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
38+
github.com/jackc/pgtype v1.11.0 // indirect
39+
github.com/jackc/pgx/v4 v4.16.1 // indirect
40+
github.com/jinzhu/inflection v1.0.0 // indirect
41+
github.com/jinzhu/now v1.1.4 // indirect
2842
github.com/josharian/intern v1.0.0 // indirect
43+
github.com/json-iterator/go v1.1.10 // indirect
2944
github.com/klauspost/compress v1.15.5 // indirect
3045
github.com/mailru/easyjson v0.7.7 // indirect
46+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
47+
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
3148
github.com/swaggo/files v0.0.0-20210815190702-a29dd2bc99b2 // indirect
3249
github.com/valyala/bytebufferpool v1.0.0 // indirect
3350
github.com/valyala/fasthttp v1.37.0 // indirect
3451
github.com/valyala/tcplisten v1.0.0 // indirect
52+
go.uber.org/atomic v1.6.0 // indirect
53+
go.uber.org/multierr v1.5.0 // indirect
54+
go.uber.org/zap v1.13.0 // indirect
55+
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
56+
golang.org/x/lint v0.0.0-20190930215403-16217165b5de // indirect
3557
golang.org/x/net v0.0.0-20220526153639-5463443f8c37 // indirect
3658
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
59+
golang.org/x/text v0.3.7 // indirect
3760
golang.org/x/tools v0.1.10 // indirect
3861
gopkg.in/yaml.v2 v2.4.0 // indirect
3962
)

api/go.sum

Lines changed: 172 additions & 0 deletions
Large diffs are not rendered by default.

api/main.exe

5.64 MB
Binary file not shown.

api/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ package main
33
import (
44
"log"
55

6-
"github.com/NdoleStudio/http-sms-manager/pkg/di"
7-
86
_ "github.com/NdoleStudio/http-sms-manager/docs"
7+
"github.com/NdoleStudio/http-sms-manager/pkg/di"
98
"github.com/gofiber/fiber/v2"
109
"github.com/gofiber/fiber/v2/middleware/logger"
1110
"github.com/gofiber/swagger"
@@ -24,6 +23,8 @@ import (
2423
// @host api.httpsms.com
2524
// @BasePath /v1
2625
func main() {
26+
di.LoadEnv()
27+
2728
app := fiber.New()
2829

2930
app.Use(logger.New())

api/pkg/di/config.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package di
2+
3+
import (
4+
"log"
5+
6+
"github.com/joho/godotenv"
7+
)
8+
9+
// LoadEnv will read your .env file(s) and load them into ENV for this process.
10+
func LoadEnv(filenames ...string) {
11+
err := godotenv.Load(filenames...)
12+
if err != nil {
13+
log.Fatalf("Fatal: cannot load .env file: %v", err)
14+
}
15+
}

api/pkg/di/container.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import (
44
"fmt"
55
"os"
66

7+
"github.com/NdoleStudio/http-sms-manager/pkg/entities"
8+
"github.com/palantir/stacktrace"
9+
"gorm.io/driver/postgres"
10+
"gorm.io/gorm"
11+
712
"github.com/NdoleStudio/http-sms-manager/pkg/handlers"
813
"github.com/NdoleStudio/http-sms-manager/pkg/services"
914
"github.com/NdoleStudio/http-sms-manager/pkg/telemetry"
@@ -15,6 +20,7 @@ import (
1520
// Container is used to resolve services at runtime
1621
type Container struct {
1722
projectID string
23+
db *gorm.DB
1824
logger telemetry.Logger
1925
}
2026

@@ -32,6 +38,28 @@ func (container Container) Logger() telemetry.Logger {
3238
return logger()
3339
}
3440

41+
// DB creates an instance of gorm.DB if it has not been created already
42+
func (container Container) DB() (db *gorm.DB) {
43+
if container.db != nil {
44+
return container.db
45+
}
46+
47+
container.logger.Debug(fmt.Sprintf("creating %T", db))
48+
db, err := gorm.Open(postgres.Open(os.Getenv("DATABASE_URL")), &gorm.Config{})
49+
if err != nil {
50+
container.logger.Fatal(err)
51+
}
52+
container.db = db
53+
54+
container.logger.Debug(fmt.Sprintf("Running migrations for %T", db))
55+
56+
if err = db.AutoMigrate(&entities.Message{}); err != nil {
57+
container.logger.Fatal(stacktrace.Propagate(err, fmt.Sprintf("cannot migrate %T", &entities.Message{})))
58+
}
59+
60+
return container.db
61+
}
62+
3563
// Tracer creates a new instance of telemetry.Tracer
3664
func (container Container) Tracer() (t telemetry.Tracer) {
3765
container.logger.Debug("creating telemetry.Tracer")

api/pkg/entities/contact.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package entities
2+
3+
import (
4+
"time"
5+
6+
"github.com/google/uuid"
7+
)
8+
9+
// Contact stores a telephone contact
10+
type Contact struct {
11+
ID uuid.UUID `json:"id" gorm:"primaryKey;type:uuid;"`
12+
PhoneNumber string `json:"phone_number"`
13+
Name string `json:"name"`
14+
Color string `json:"color"`
15+
CreatedAt time.Time `json:"created_at"`
16+
UpdatedAt time.Time `json:"updated_at"`
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package entities
2+
3+
import (
4+
"time"
5+
6+
"github.com/google/uuid"
7+
)
8+
9+
// EventListenerLog stores the log of all the events handled
10+
type EventListenerLog struct {
11+
ID uuid.UUID `json:"id" gorm:"primaryKey;type:uuid;"`
12+
EventType string `json:"event_type"`
13+
Handler string `json:"handler"`
14+
HandledAt time.Time `json:"handled_at"`
15+
CreatedAt time.Time `json:"created_at"`
16+
}

api/pkg/entities/message.go

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,59 @@
11
package entities
22

3-
// Message represents a message sent between 2 users
4-
type Message struct{}
3+
import (
4+
"time"
5+
6+
"github.com/google/uuid"
7+
)
8+
9+
// MessageType is the type of message if it is incoming or outgoing
10+
type MessageType string
11+
12+
const (
13+
// MessageTypeMobileTerminated means the message it sent to a mobile phone
14+
MessageTypeMobileTerminated = "mobile-terminated"
15+
16+
// MessageTypeMobileOriginated means the message comes directly from a mobile phone
17+
MessageTypeMobileOriginated = "mobile-originated"
18+
)
19+
20+
// MessageStatus is the status of the message
21+
type MessageStatus string
22+
23+
const (
24+
// MessageStatusPending means the message has been queued to be sent
25+
MessageStatusPending = "pending"
26+
27+
// MessageStatusSending means a phone has picked up the message and is currently sending it
28+
MessageStatusSending = "sending"
29+
30+
// MessageStatusSent means the message has already sent by the mobile phone
31+
MessageStatusSent = "sent"
32+
33+
// MessageStatusReceived means the message was received by tne mobile phone (MO)
34+
MessageStatusReceived = "received"
35+
36+
// MessageStatusFailed means the mobile phone could not send the message
37+
MessageStatusFailed = "failed"
38+
)
39+
40+
// Message represents a message sent between 2 phone numbers
41+
type Message struct {
42+
ID uuid.UUID `json:"id" gorm:"primaryKey;type:uuid;"`
43+
From string `json:"from"`
44+
To string `json:"to"`
45+
Content string `json:"content"`
46+
Type MessageType `json:"type"`
47+
Status MessageStatus `json:"status"`
48+
49+
// SendDuration is the number of nanoseconds from when the request was received until when the mobile phone send the message
50+
SendDuration time.Duration `json:"send_time"`
51+
52+
RequestReceivedAt time.Time `json:"request_received_at"`
53+
CreatedAt time.Time `json:"created_at"`
54+
UpdatedAt time.Time `json:"updated_at"`
55+
OrderTimestamp time.Time `json:"order_timestamp"`
56+
LastAttemptedAt *time.Time `json:"last_attempted_at"`
57+
SentAt *time.Time `json:"sent_at"`
58+
ReceivedAt *time.Time `json:"received_at"`
59+
}

api/pkg/entities/message_thread.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package entities
2+
3+
import (
4+
"time"
5+
6+
"github.com/google/uuid"
7+
)
8+
9+
// MessageThread represents a message thread between 2 phone numbers
10+
type MessageThread struct {
11+
ID uuid.UUID `json:"id" gorm:"primaryKey;type:uuid;"`
12+
From string `json:"from"`
13+
To string `json:"to"`
14+
LastMessage string `json:"content"`
15+
CreatedAt time.Time `json:"created_at"`
16+
UpdatedAt time.Time `json:"updated_at"`
17+
OrderTimestamp time.Time `json:"order_timestamp"`
18+
}

0 commit comments

Comments
 (0)