Skip to content

Commit

Permalink
Merge pull request #6 from morka17/ft/mailservices
Browse files Browse the repository at this point in the history
Ft/mailservices
  • Loading branch information
morka17 authored Jul 10, 2023
2 parents 6785fc7 + 140d793 commit feaf889
Show file tree
Hide file tree
Showing 37 changed files with 1,240 additions and 75 deletions.
5 changes: 5 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Set the default behavior,
# in case people don't have core.autocrlf set.
* text=auto
# Declares that files will always have CRLF line ends
*.sh text eol=lf
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ FROM golang:1.16-alpine AS builder
WORKDIR /app

COPY main .
COPY . .
# COPY migrate.linux-amd64 .

# FROM golang:1.16-alpine AS builder
Expand All @@ -14,13 +13,14 @@ COPY . .
# COPY go.mod .
# RUN go mod download
# # Copy the source code.
# COPY . .g
# COPY . .
# # Build
# # RUN apk add curl
# # RUN curl -L https://github.com/golang-migrate/migrate/releases/download/v4.12.2/migrate.linux-amd64.tar.gz | tar xvz
# COPY migrate.linux-amd64 .
# RUN CGO_ENABLED=0 GOOS=linux go build -o main main.go


# RUN STAGE
FROM alpine:latest
WORKDIR /app
Expand All @@ -33,5 +33,7 @@ COPY start.sh .


EXPOSE 8080
EXPOSE 9090

CMD ["/app/main"]
ENTRYPOINT [ "/app/start.sh"]
# ENTRYPOINT [ "/app/start.sh"]
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ dropdb:
docker exec -it postgres12 dropdb shiny_bank

createmigrate:
migrate create -ext sql -dir db/migration -seq init_schema
migrate create -ext sql -dir ./src/db/migration -seq $(name)

migrateup:
migrate -path ./src/db/migration -database "postgresql://root:secret@$(dburl)/$(dbname)?sslmode=disable" -verbose up
Expand All @@ -43,6 +43,7 @@ sqlc:

mockgenstore:
mockgen -package mockdb -destination src/db/mock/store.go github.com/morka17/shiny_bank/v1/src/db/sqlc Store
mockgen -package mockwk -destination src/worker/mock/distributor.go github.com/morka17/shiny_bank/v1/src/worker TaskDistributor

db_docs:
dbdocs build doc/db.dbml
Expand Down Expand Up @@ -88,6 +89,6 @@ redis:


test:
go test -v -cover ./...
go test -v -cover -short ./...

.PHONY: postgres mockgenstore createdb db_docs db_schema dropdb migrateup migrateup1 migratedown1 migratedown sqlc test server proto
5 changes: 4 additions & 1 deletion app.env
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ GRPC_Server_Address = :9090
TOKEN_SYMMETRIC_KEY = 12344563454624235362365474356564
ACCESS_TOKEN_DURATION=15m
REFRESH_TOKEN_DURATION=24h
REDIS_ADDRESS=0.0.0.0:6379
REDIS_ADDRESS=0.0.0.0:6379
EMAIL_SENDER_NAME=Shiny bank
EMAIL_SENDER_ADDRESS=morkajoshua50@gmail.com
EMAIL_SENDER_PASSWORD = zndwhrxsufitpbay
15 changes: 14 additions & 1 deletion doc/schema.sql
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
-- SQL dump generated using DBML (dbml-lang.org)
-- Database: PostgreSQL
-- Generated at: 2023-07-01T00:13:29.628Z
-- Generated at: 2023-07-07T20:24:55.868Z

CREATE TABLE "users" (
"username" varchar PRIMARY KEY,
"hashed_password" varchar NOT NULL,
"full_name" varchar NOT NULL,
"email" varchar UNIQUE NOT NULL,
"is_email_verified" boolean NOT NULL DEFAULT false,
"password_change_at" timestamptz NOT NULL DEFAULT (0001-01-01 00:00:00+00Z),
"created" timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE "verify_emails" (
"id" bigserial PRIMARY KEY,
"username" varchar NOT NULL,
"email" varchar NOT NULL,
"secret_code" varchar NOT NULL,
"is_used" bool NOT NULL DEFAULT false,
"created_at" timestamptz NOT NULL DEFAULT (now()),
"expired_at" timestamptz NOT NULL DEFAULT (now() + interval '15 minutes')
);

CREATE TABLE "sessions" (
"id" uuid PRIMARY KEY,
"username" varchar NOT NULL,
Expand Down Expand Up @@ -61,6 +72,8 @@ COMMENT ON COLUMN "entries"."amount" IS 'can"t be empty';

COMMENT ON COLUMN "transfers"."amount" IS 'can"t be empty';

ALTER TABLE "verify_emails" ADD FOREIGN KEY ("username") REFERENCES "users" ("username");

ALTER TABLE "sessions" ADD FOREIGN KEY ("username") REFERENCES "users" ("username");

ALTER TABLE "accounts" ADD FOREIGN KEY ("owner") REFERENCES "users" ("username");
Expand Down
2 changes: 1 addition & 1 deletion doc/statik/statik.go

Large diffs are not rendered by default.

47 changes: 47 additions & 0 deletions doc/swagger/shiny_bank.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,56 @@
"SimpleBank"
]
}
},
"/v1/verify_email": {
"get": {
"summary": "Verify email",
"description": "Use this API to verify user's email address",
"operationId": "SimpleBank_VerifyEmail",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/VerifyEmailResponse"
}
},
"default": {
"description": "An unexpected error response.",
"schema": {
"$ref": "#/definitions/rpcStatus"
}
}
},
"parameters": [
{
"name": "emaiilId",
"in": "query",
"required": false,
"type": "string",
"format": "int64"
},
{
"name": "secretCode",
"in": "query",
"required": false,
"type": "string"
}
],
"tags": [
"SimpleBank"
]
}
}
},
"definitions": {
"VerifyEmailResponse": {
"type": "object",
"properties": {
"isVerified": {
"type": "boolean"
}
}
},
"pbCreateUserRequest": {
"type": "object",
"properties": {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/google/uuid v1.3.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0
github.com/hibiken/asynq v0.24.1
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible
github.com/o1egl/paseto v1.0.0
github.com/rakyll/statik v0.1.7
github.com/rs/zerolog v1.29.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ github.com/hibiken/asynq v0.24.1 h1:+5iIEAyA9K/lcSPvx3qoPtsKJeKI5u9aOIvUmSsazEw=
github.com/hibiken/asynq v0.24.1/go.mod h1:u5qVeSbrnfT+vtG5Mq8ZPzQu/BmCKMHvTGb91uy9Tts=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible h1:jdpOPRN1zP63Td1hDQbZW73xKmzDvZHzVdNYxhnTMDA=
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible/go.mod h1:1c7szIrayyPPB/987hsnvNzLushdWf4o/79s3P08L8A=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
Expand Down
15 changes: 8 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/morka17/shiny_bank/v1/src/api"
db "github.com/morka17/shiny_bank/v1/src/db/sqlc"
"github.com/morka17/shiny_bank/v1/src/gapi"
"github.com/morka17/shiny_bank/v1/src/mail"
"github.com/morka17/shiny_bank/v1/src/utils"
"github.com/morka17/shiny_bank/v1/src/worker"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -53,18 +54,16 @@ func main() {
redisOpt := asynq.RedisClientOpt{
Addr: config.RedisAddress,
}

taskDistributor := worker.NewRedisTaskDistributor(redisOpt)

go runTaskProcessor(redisOpt, store)
go runTaskProcessor(redisOpt, store, config)

go runGatewayServer(config, store, taskDistributor)
runGrpcServer(config, store, taskDistributor)

}



// RUN database migration
func runDBMigration(migrationURL string, dbSource string) {
migration, err := migrate.New(migrationURL, dbSource)
Expand Down Expand Up @@ -158,12 +157,14 @@ func runGatewayServer(config utils.Config, store db.Store, taskDistributor worke

}

func runTaskProcessor(redisOpt asynq.RedisClientOpt, store db.Store, config utils.Config) {

func runTaskProcessor(redisOpt asynq.RedisClientOpt, store db.Store) {
taskProcessor := worker.NewRedisTaskProcessor(redisOpt, store)
mailer := mail.NewGmailSender(config.EmailSenderName, config.EmailSenderAddress, config.EmailSenderPassword)

taskProcessor := worker.NewRedisTaskProcessor(redisOpt, store, mailer)
log.Info().Msg("start task processor")
err := taskProcessor.Start()
if err != nil {
log.Fatal().Err(err).Msg("Failed to start task processor")
}
}
}
Loading

0 comments on commit feaf889

Please sign in to comment.