Skip to content

Commit

Permalink
Merge pull request #133 from ownmfa/bconway_update_app_event_identity…
Browse files Browse the repository at this point in the history
…_dao_rw_ro

Update app, event, and identity DAO for RW and RO database connections
  • Loading branch information
bconway authored Dec 4, 2023
2 parents 9eaa9ee + 98bc6bc commit 4fca722
Show file tree
Hide file tree
Showing 47 changed files with 442 additions and 126 deletions.
10 changes: 6 additions & 4 deletions build/deploy/hermes/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
version: "3"
services:
hermes-api:
image: ghcr.io/ownmfa/hermes:4d717212
image: ghcr.io/ownmfa/hermes:9eaa9ee8
command: hermes-api
restart: on-failure
depends_on:
- hermes-notifier
environment:
- API_STATSD_ADDR=dogstatsd:8125
- API_PG_URI=postgres://postgres:notasecurepassword@postgres/hermes
- API_PG_RW_URI=postgres://postgres:notasecurepassword@postgres/hermes
- API_PG_RO_URI=postgres://postgres:notasecurepassword@postgres/hermes
- API_REDIS_HOST=redis
- API_PWT_KEY=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
- API_IDENTITY_KEY=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
Expand All @@ -35,12 +36,13 @@ services:
- "traefik.http.services.hermes-grpc.loadbalancer.server.scheme=h2c"

hermes-notifier:
image: ghcr.io/ownmfa/hermes:4d717212
image: ghcr.io/ownmfa/hermes:9eaa9ee8
command: hermes-notifier
restart: on-failure
environment:
- NOTIFIER_STATSD_ADDR=dogstatsd:8125
- NOTIFIER_PG_URI=postgres://postgres:notasecurepassword@postgres/hermes
- NOTIFIER_PG_RW_URI=postgres://postgres:notasecurepassword@postgres/hermes
- NOTIFIER_PG_RO_URI=postgres://postgres:notasecurepassword@postgres/hermes
- NOTIFIER_REDIS_HOST=redis
- NOTIFIER_IDENTITY_KEY=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
- NOTIFIER_NSQ_PUB_ADDR=nsqd:4150
Expand Down
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/smira/go-statsd v1.3.3
github.com/stretchr/testify v1.8.4
go.uber.org/mock v0.3.0
golang.org/x/crypto v0.15.0
golang.org/x/crypto v0.16.0
google.golang.org/grpc v1.59.0
google.golang.org/protobuf v1.31.0
)
Expand All @@ -30,16 +30,16 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/genproto v0.0.0-20231127180814-3a041ad873d4 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231127180814-3a041ad873d4 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231127180814-3a041ad873d4 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
28 changes: 14 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -530,8 +530,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 h1:L0QtFUgDarD7Fpv9jeVMgy/+Ec0mtnmYuImjTz6dtDA=
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.5.0 h1:NxstgwndsTRy7eq9/kqYc/BZh5w2hHJV86wjvO+1xPw=
github.com/jackc/pgx/v5 v5.5.0/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
Expand Down Expand Up @@ -606,8 +606,8 @@ golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA=
golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g=
golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY=
golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down Expand Up @@ -691,8 +691,8 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug
golang.org/x/net v0.0.0-20220909164309-bea034e7d591/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.0.0-20221012135044-0b7e1fb9d458/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg=
golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -797,8 +797,8 @@ golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220624220833-87e55d714810/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down Expand Up @@ -1045,12 +1045,12 @@ google.golang.org/genproto v0.0.0-20221117204609-8f9c96812029/go.mod h1:rZS5c/ZV
google.golang.org/genproto v0.0.0-20221118155620-16455021b5e6/go.mod h1:rZS5c/ZVYMaOGBfO68GWtjOw/eLaZM1X6iVtgjZ+EWg=
google.golang.org/genproto v0.0.0-20221201164419-0e50fba7f41c/go.mod h1:rZS5c/ZVYMaOGBfO68GWtjOw/eLaZM1X6iVtgjZ+EWg=
google.golang.org/genproto v0.0.0-20230202175211-008b39050e57/go.mod h1:RGgjbofJ8xD9Sq1VVhDM1Vok1vRONV+rg+CjzG4SZKM=
google.golang.org/genproto v0.0.0-20231106174013-bbf56f31fb17 h1:wpZ8pe2x1Q3f2KyT5f8oP/fa9rHAKgFPr/HZdNuS+PQ=
google.golang.org/genproto v0.0.0-20231106174013-bbf56f31fb17/go.mod h1:J7XzRzVy1+IPwWHZUzoD0IccYZIrXILAQpc+Qy9CMhY=
google.golang.org/genproto/googleapis/api v0.0.0-20231106174013-bbf56f31fb17 h1:JpwMPBpFN3uKhdaekDpiNlImDdkUAyiJ6ez/uxGaUSo=
google.golang.org/genproto/googleapis/api v0.0.0-20231106174013-bbf56f31fb17/go.mod h1:0xJLfVdJqpAPl8tDg1ujOCGzx6LFLttXT5NhllGOXY4=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 h1:Jyp0Hsi0bmHXG6k9eATXoYtjd6e2UzZ1SCn/wIupY14=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17/go.mod h1:oQ5rr10WTTMvP4A36n8JpR1OrO1BEiV4f78CneXZxkA=
google.golang.org/genproto v0.0.0-20231127180814-3a041ad873d4 h1:W12Pwm4urIbRdGhMEg2NM9O3TWKjNcxQhs46V0ypf/k=
google.golang.org/genproto v0.0.0-20231127180814-3a041ad873d4/go.mod h1:5RBcpGRxr25RbDzY5w+dmaqpSEvl8Gwl1x2CICf60ic=
google.golang.org/genproto/googleapis/api v0.0.0-20231127180814-3a041ad873d4 h1:ZcOkrmX74HbKFYnpPY8Qsw93fC29TbJXspYKaBkSXDQ=
google.golang.org/genproto/googleapis/api v0.0.0-20231127180814-3a041ad873d4/go.mod h1:k2dtGpRrbsSyKcNPKKI5sstZkrNCZwpU/ns96JoHbGg=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231127180814-3a041ad873d4 h1:DC7wcm+i+P1rN3Ff07vL+OndGg5OhNddHyTA+ocPqYE=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231127180814-3a041ad873d4/go.mod h1:eJVxU6o+4G1PSczBr85xmyvSNYAKvAYgkub40YGomFM=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=
google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
Expand Down
23 changes: 15 additions & 8 deletions internal/hermes-api/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ func New(cfg *config.Config) (*API, error) {
}

// Set up database connection.
pg, err := dao.NewPgDB(cfg.PgURI)
pgRW, err := dao.NewPgDB(cfg.PgRwURI)
if err != nil {
return nil, err
}

pgRO, err := dao.NewPgDB(cfg.PgRoURI)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -109,21 +114,23 @@ func New(cfg *config.Config) (*API, error) {
"/ownmfa.api.UserService/UpdateUser": {},
}

orgDAO := org.NewDAO(pg, redis, orgExp)
orgDAO := org.NewDAO(pgRW, redis, orgExp)
srv := grpc.NewServer(grpc.ChainUnaryInterceptor(
interceptor.Log(nil),
interceptor.Auth(skipAuth, cfg.PWTKey, redis, orgDAO),
interceptor.Validate(skipValidate),
))

api.RegisterAppIdentityServiceServer(srv,
service.NewAppIdentity(app.NewDAO(pg), identity.NewDAO(pg,
cfg.IdentityKey), event.NewDAO(pg), redis, n, nsq, cfg.NSQPubTopic))
api.RegisterEventServiceServer(srv, service.NewEvent(event.NewDAO(pg)))
service.NewAppIdentity(app.NewDAO(pgRW, pgRO), identity.NewDAO(pgRW,
pgRO, cfg.IdentityKey), event.NewDAO(pgRW, pgRO), redis, n, nsq,
cfg.NSQPubTopic))
api.RegisterEventServiceServer(srv, service.NewEvent(event.NewDAO(pgRW,
pgRO)))
api.RegisterOrgServiceServer(srv, service.NewOrg(orgDAO))
api.RegisterSessionServiceServer(srv, service.NewSession(user.NewDAO(pg),
key.NewDAO(pg), redis, cfg.PWTKey))
api.RegisterUserServiceServer(srv, service.NewUser(user.NewDAO(pg)))
api.RegisterSessionServiceServer(srv, service.NewSession(user.NewDAO(pgRW),
key.NewDAO(pgRW), redis, cfg.PWTKey))
api.RegisterUserServiceServer(srv, service.NewUser(user.NewDAO(pgRW)))

// Register gRPC-Gateway handlers.
ctx, cancel := context.WithCancel(context.Background())
Expand Down
7 changes: 5 additions & 2 deletions internal/hermes-api/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ type Config struct {
LogLevel string
StatsDAddr string

PgURI string
PgRwURI string
PgRoURI string
RedisHost string

NSQPubAddr string
Expand All @@ -32,7 +33,9 @@ func New() *Config {
LogLevel: config.String(pref+"LOG_LEVEL", "DEBUG"),
StatsDAddr: config.String(pref+"STATSD_ADDR", ""),

PgURI: config.String(pref+"PG_URI",
PgRwURI: config.String(pref+"PG_RW_URI",
"postgres://postgres:[email protected]/hermes_test"),
PgRoURI: config.String(pref+"PG_RO_URI",
"postgres://postgres:[email protected]/hermes_test"),
RedisHost: config.String(pref+"REDIS_HOST", "127.0.0.1"),

Expand Down
9 changes: 5 additions & 4 deletions internal/hermes-api/test/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ func TestMain(m *testing.M) {
// Set up Config.
testConfig := testconfig.New()
cfg := config.New()
cfg.PgURI = testConfig.PgURI
cfg.PgRwURI = testConfig.PgURI
cfg.PgRoURI = testConfig.PgURI
cfg.RedisHost = testConfig.RedisHost

cfg.NSQPubAddr = testConfig.NSQPubAddr
Expand All @@ -89,14 +90,14 @@ func TestMain(m *testing.M) {
}()

// Set up database connection.
pg, err := dao.NewPgDB(cfg.PgURI)
pg, err := dao.NewPgDB(cfg.PgRwURI)
if err != nil {
log.Fatalf("TestMain dao.NewPgDB: %v", err)
}
globalOrgDAO = org.NewDAO(pg, nil, 0)
globalUserDAO = user.NewDAO(pg)
globalIdentDAO = identity.NewDAO(pg, key)
globalEvDAO = event.NewDAO(pg)
globalIdentDAO = identity.NewDAO(pg, pg, key)
globalEvDAO = event.NewDAO(pg, pg)

// Set up cache connection.
globalCache, err = cache.NewRedis(cfg.RedisHost + ":6379")
Expand Down
7 changes: 5 additions & 2 deletions internal/hermes-notifier/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ type Config struct {
StatsDAddr string
Concurrency int

PgURI string
PgRwURI string
PgRoURI string
RedisHost string

IdentityKey []byte
Expand All @@ -38,7 +39,9 @@ func New() *Config {
StatsDAddr: config.String(pref+"STATSD_ADDR", ""),
Concurrency: config.Int(pref+"CONCURRENCY", 5),

PgURI: config.String(pref+"PG_URI",
PgRwURI: config.String(pref+"PG_RW_URI",
"postgres://postgres:[email protected]/hermes_test"),
PgRoURI: config.String(pref+"PG_RO_URI",
"postgres://postgres:[email protected]/hermes_test"),
RedisHost: config.String(pref+"REDIS_HOST", "127.0.0.1"),

Expand Down
13 changes: 9 additions & 4 deletions internal/hermes-notifier/notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ func New(cfg *config.Config) (*Notifier, error) {
}

// Set up database connection.
pg, err := dao.NewPgDB(cfg.PgURI)
pgRW, err := dao.NewPgDB(cfg.PgRwURI)
if err != nil {
return nil, err
}

pgRO, err := dao.NewPgDB(cfg.PgRoURI)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -107,9 +112,9 @@ func New(cfg *config.Config) (*Notifier, error) {
}

return &Notifier{
appDAO: app.NewDAO(pg),
identDAO: identity.NewDAO(pg, cfg.IdentityKey),
evDAO: event.NewDAO(pg),
appDAO: app.NewDAO(pgRW, pgRO),
identDAO: identity.NewDAO(pgRW, pgRO, cfg.IdentityKey),
evDAO: event.NewDAO(pgRW, pgRO),
cache: redis,

notQueue: nsq,
Expand Down
11 changes: 6 additions & 5 deletions internal/hermes-notifier/test/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ func TestMain(m *testing.M) {
// Set up Config.
testConfig := testconfig.New()
cfg := config.New()
cfg.PgURI = testConfig.PgURI
cfg.PgRwURI = testConfig.PgURI
cfg.PgRoURI = testConfig.PgURI
cfg.RedisHost = testConfig.RedisHost

cfg.IdentityKey = key
Expand Down Expand Up @@ -74,14 +75,14 @@ func TestMain(m *testing.M) {
}()

// Set up database connection.
pg, err := dao.NewPgDB(cfg.PgURI)
pg, err := dao.NewPgDB(cfg.PgRwURI)
if err != nil {
log.Fatalf("TestMain dao.NewPgDB: %v", err)
}
globalOrgDAO = org.NewDAO(pg, nil, 0)
globalAppDAO = app.NewDAO(pg)
globalIdentDAO = identity.NewDAO(pg, key)
globalEvDAO = event.NewDAO(pg)
globalAppDAO = app.NewDAO(pg, pg)
globalIdentDAO = identity.NewDAO(pg, pg, key)
globalEvDAO = event.NewDAO(pg, pg)

// Set up cache connection.
globalCache, err = cache.NewRedis(cfg.RedisHost + ":6379")
Expand Down
26 changes: 14 additions & 12 deletions pkg/dao/app/crudl.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ func (d *DAO) Create(ctx context.Context, app *api.App) (*api.App, error) {
app.CreatedAt = timestamppb.New(now)
app.UpdatedAt = timestamppb.New(now)

if err := d.pg.QueryRowContext(ctx, createApp, app.GetOrgId(), app.GetName(),
app.GetDisplayName(), app.GetEmail(), app.GetPushoverKey(), app.GetSubjectTemplate(),
app.GetTextBodyTemplate(), app.GetHtmlBodyTemplate(), now).Scan(
&app.Id); err != nil {
if err := d.rw.QueryRowContext(ctx, createApp, app.GetOrgId(),
app.GetName(), app.GetDisplayName(), app.GetEmail(),
app.GetPushoverKey(), app.GetSubjectTemplate(),
app.GetTextBodyTemplate(), app.GetHtmlBodyTemplate(),
now).Scan(&app.Id); err != nil {
return nil, dao.DBToSentinel(err)
}

Expand All @@ -47,7 +48,7 @@ func (d *DAO) Read(ctx context.Context, appID, orgID string) (*api.App, error) {
app := &api.App{}
var createdAt, updatedAt time.Time

if err := d.pg.QueryRowContext(ctx, readApp, appID, orgID).Scan(&app.Id,
if err := d.ro.QueryRowContext(ctx, readApp, appID, orgID).Scan(&app.Id,
&app.OrgId, &app.Name, &app.DisplayName, &app.Email, &app.PushoverKey,
&app.SubjectTemplate, &app.TextBodyTemplate, &app.HtmlBodyTemplate,
&createdAt, &updatedAt); err != nil {
Expand Down Expand Up @@ -76,10 +77,11 @@ func (d *DAO) Update(ctx context.Context, app *api.App) (*api.App, error) {
updatedAt := time.Now().UTC().Truncate(time.Microsecond)
app.UpdatedAt = timestamppb.New(updatedAt)

if err := d.pg.QueryRowContext(ctx, updateApp, app.GetName(), app.GetDisplayName(),
app.GetEmail(), app.GetPushoverKey(), app.GetSubjectTemplate(), app.GetTextBodyTemplate(),
app.GetHtmlBodyTemplate(), updatedAt, app.GetId(), app.GetOrgId()).Scan(
&createdAt); err != nil {
if err := d.rw.QueryRowContext(ctx, updateApp, app.GetName(),
app.GetDisplayName(), app.GetEmail(), app.GetPushoverKey(),
app.GetSubjectTemplate(), app.GetTextBodyTemplate(),
app.GetHtmlBodyTemplate(), updatedAt, app.GetId(),
app.GetOrgId()).Scan(&createdAt); err != nil {
return nil, dao.DBToSentinel(err)
}

Expand All @@ -101,7 +103,7 @@ func (d *DAO) Delete(ctx context.Context, appID, orgID string) error {
return err
}

_, err := d.pg.ExecContext(ctx, deleteApp, appID, orgID)
_, err := d.rw.ExecContext(ctx, deleteApp, appID, orgID)

return dao.DBToSentinel(err)
}
Expand Down Expand Up @@ -141,7 +143,7 @@ func (d *DAO) List(
) ([]*api.App, int32, error) {
// Run count query.
var count int32
if err := d.pg.QueryRowContext(ctx, countApps, orgID).Scan(
if err := d.ro.QueryRowContext(ctx, countApps, orgID).Scan(
&count); err != nil {
return nil, 0, dao.DBToSentinel(err)
}
Expand All @@ -162,7 +164,7 @@ func (d *DAO) List(
}

// Run list query.
rows, err := d.pg.QueryContext(ctx, query, args...)
rows, err := d.ro.QueryContext(ctx, query, args...)
if err != nil {
return nil, 0, dao.DBToSentinel(err)
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/dao/app/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (

// DAO contains functions to query and modify applications in the database.
type DAO struct {
pg *sql.DB
rw *sql.DB
ro *sql.DB
}

// NewDAO instantiates and returns a new DAO.
func NewDAO(pg *sql.DB) *DAO {
func NewDAO(rw *sql.DB, ro *sql.DB) *DAO {
return &DAO{
pg: pg,
rw: rw,
ro: ro,
}
}
2 changes: 1 addition & 1 deletion pkg/dao/app/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestMain(m *testing.M) {
log.Fatalf("TestMain dao.NewPgDB: %v", err)
}
globalOrgDAO = org.NewDAO(pg, nil, 0)
globalAppDAO = NewDAO(pg)
globalAppDAO = NewDAO(pg, pg)

os.Exit(m.Run())
}
Loading

0 comments on commit 4fca722

Please sign in to comment.