Skip to content

Commit

Permalink
Merge pull request #36 from aryyawijaya/dev
Browse files Browse the repository at this point in the history
feat: gRPC
  • Loading branch information
aryyawijaya authored Feb 18, 2024
2 parents 65156fc + c7c746b commit c96c7b4
Show file tree
Hide file tree
Showing 79 changed files with 4,148 additions and 381 deletions.
19 changes: 18 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,22 @@ db-docs:
db-schema:
dbml2sql doc/db.dbml --postgres -o doc/schema.sql

.PHONY:
proto:
rm -f pb/*.go
rm -f doc/swagger/*.swagger.json
protoc \
--proto_path=proto \
--go_out=pb --go_opt=paths=source_relative \
--go-grpc_out=pb --go-grpc_opt=paths=source_relative \
--grpc-gateway_out=pb --grpc-gateway_opt=paths=source_relative \
--openapiv2_out=doc/swagger \
--openapiv2_opt=allow_merge=true,merge_file_name=simple_bank \
proto/*.proto

evans:
evans --host localhost --port 8081 -r repl

.PHONY: \
pull-postgres \
start-postgres \
logs-postgres \
Expand All @@ -148,3 +163,5 @@ db-schema:
query-update \
db-docs \
db-schema \
proto \
evans \
3 changes: 2 additions & 1 deletion app.env
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
DB_DRIVER=postgres
DB_SOURCE=postgresql://root:secretpassword@localhost:5433/simple_bank?sslmode=disable
SERVER_ADDRESS=0.0.0.0:8080
HTTP_SERVER_ADDRESS=0.0.0.0:8080
GRPC_SERVER_ADDRESS=0.0.0.0:8081
TOKEN_SYMETRIC_KEY=12345678901234567890123456789012
ACCESS_TOKEN_DURATION=15m
REFRESH_TOKEN_DURATION=24h
3 changes: 2 additions & 1 deletion compose.dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ services:
context: .
dockerfile: Dockerfile.dev
ports:
- "8080:8080"
- "8080:8080" # HTTP
- "8081:8081" # gRPC
environment:
- DB_SOURCE=postgresql://root:secretpassword@postgres:5432/simple_bank?sslmode=disable
depends_on:
Expand Down
3 changes: 2 additions & 1 deletion compose.prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ services:
context: .
dockerfile: Dockerfile.prod
ports:
- "8080:8080"
- "8080:8080" # HTTP
- "8081:8081" # gRPC
environment:
- DB_SOURCE=postgresql://root:secretpassword@postgres:5432/simple_bank?sslmode=disable
depends_on:
Expand Down
13 changes: 7 additions & 6 deletions db/sqlc/account_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package mydb
package mydb_test

import (
"context"
"database/sql"
"testing"
"time"

mydb "github.com/aryyawijaya/simple-bank/db/sqlc"
"github.com/aryyawijaya/simple-bank/util"
"github.com/stretchr/testify/require"
)

func createRandomAccount(t *testing.T) Account {
func createRandomAccount(t *testing.T) mydb.Account {
createdUser := createRandomUser(t)

arg := CreateAccountParams{
arg := mydb.CreateAccountParams{
Owner: createdUser.Username,
Balance: util.RandomBalance(),
Currency: util.RandomCurrency(),
Expand Down Expand Up @@ -54,7 +55,7 @@ func TestGetAccount(t *testing.T) {
func TestUpdateAccount(t *testing.T) {
createdAccount := createRandomAccount(t)

arg := UpdateAccountBalanceParams{
arg := mydb.UpdateAccountBalanceParams{
ID: createdAccount.ID,
Balance: util.RandomBalance(),
}
Expand Down Expand Up @@ -84,13 +85,13 @@ func TestDeleteAccount(t *testing.T) {
}

func TestListAccount(t *testing.T) {
var lastAccount Account
var lastAccount mydb.Account
for i := 0; i < 5; i++ {
lastAccount = createRandomAccount(t)
}

limit, offset := 3, 0
arg := ListAccountsParams{
arg := mydb.ListAccountsParams{
Owner: lastAccount.Owner,
Limit: int32(limit),
Offset: int32(offset),
Expand Down
7 changes: 4 additions & 3 deletions db/sqlc/main_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package mydb
package mydb_test

import (
"database/sql"
"log"
"os"
"testing"

mydb "github.com/aryyawijaya/simple-bank/db/sqlc"
"github.com/aryyawijaya/simple-bank/util"
_ "github.com/lib/pq"
)
Expand All @@ -15,7 +16,7 @@ import (
// dataSourceName = "postgresql://root:secretpassword@localhost:5433/simple_bank?sslmode=disable"
// )

var testQueries *Queries
var testQueries *mydb.Queries
var testDB *sql.DB

func TestMain(m *testing.M) {
Expand All @@ -31,7 +32,7 @@ func TestMain(m *testing.M) {
log.Fatalf("Cannot connect to database: %v", err)
}

testQueries = New(testDB)
testQueries = mydb.New(testDB)

os.Exit(m.Run())
}
13 changes: 7 additions & 6 deletions db/sqlc/store_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package mydb
package mydb_test

import (
"context"
"fmt"
"testing"

mydb "github.com/aryyawijaya/simple-bank/db/sqlc"
"github.com/stretchr/testify/require"
)

func TestTransferTx(t *testing.T) {
store := NewStore(testDB)
store := mydb.NewStore(testDB)

account1 := createRandomAccount(t)
account2 := createRandomAccount(t)
Expand All @@ -20,14 +21,14 @@ func TestTransferTx(t *testing.T) {
amount := int64(10)

errs := make(chan error)
results := make(chan TransferTxResult)
results := make(chan mydb.TransferTxResult)

for i := 0; i < n; i++ {
// txName := fmt.Sprintf("tx %d", i+1)
go func() {
// ctx := context.WithValue(context.Background(), txKey, txName)
ctx := context.Background()
result, err := store.TransferTx(ctx, TransferTxParams{
result, err := store.TransferTx(ctx, mydb.TransferTxParams{
FromAccountID: account1.ID,
ToAccountID: account2.ID,
Amount: amount,
Expand Down Expand Up @@ -129,7 +130,7 @@ func TestTransferTx(t *testing.T) {
}

func TestTransferTxDeadlockByOrderQuery(t *testing.T) {
store := NewStore(testDB)
store := mydb.NewStore(testDB)

account1 := createRandomAccount(t)
account2 := createRandomAccount(t)
Expand Down Expand Up @@ -158,7 +159,7 @@ func TestTransferTxDeadlockByOrderQuery(t *testing.T) {

go func() {
ctx := context.Background()
_, err := store.TransferTx(ctx, TransferTxParams{
_, err := store.TransferTx(ctx, mydb.TransferTxParams{
FromAccountID: fromAccountID,
ToAccountID: toAccountID,
Amount: amount,
Expand Down
7 changes: 4 additions & 3 deletions db/sqlc/user_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package mydb
package mydb_test

import (
"context"
"testing"

mydb "github.com/aryyawijaya/simple-bank/db/sqlc"
"github.com/aryyawijaya/simple-bank/modules/auth/password"
"github.com/aryyawijaya/simple-bank/util"
"github.com/stretchr/testify/require"
)

var ph = password.NewPassHelper()

func createRandomUser(t *testing.T) User {
func createRandomUser(t *testing.T) mydb.User {
hashedPass, err := ph.HashPassword(util.RandomString(8))
require.NoError(t, err)

arg := CreateUserParams{
arg := mydb.CreateUserParams{
Username: util.RandomOwner(),
HashedPassword: hashedPass,
FullName: util.RandomOwner(),
Expand Down
Binary file added doc/swagger/favicon-16x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/swagger/favicon-32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions doc/swagger/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
html {
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
}

*,
*:before,
*:after {
box-sizing: inherit;
}

body {
margin: 0;
background: #fafafa;
}
19 changes: 19 additions & 0 deletions doc/swagger/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!-- HTML for static distribution bundle build -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Swagger UI</title>
<link rel="stylesheet" type="text/css" href="./swagger-ui.css" />
<link rel="stylesheet" type="text/css" href="index.css" />
<link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" />
</head>

<body>
<div id="swagger-ui"></div>
<script src="./swagger-ui-bundle.js" charset="UTF-8"> </script>
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
<script src="./swagger-initializer.js" charset="UTF-8"> </script>
</body>
</html>
79 changes: 79 additions & 0 deletions doc/swagger/oauth2-redirect.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<!doctype html>
<html lang="en-US">
<head>
<title>Swagger UI: OAuth2 Redirect</title>
</head>
<body>
<script>
'use strict';
function run () {
var oauth2 = window.opener.swaggerUIRedirectOauth2;
var sentState = oauth2.state;
var redirectUrl = oauth2.redirectUrl;
var isValid, qp, arr;

if (/code|token|error/.test(window.location.hash)) {
qp = window.location.hash.substring(1).replace('?', '&');
} else {
qp = location.search.substring(1);
}

arr = qp.split("&");
arr.forEach(function (v,i,_arr) { _arr[i] = '"' + v.replace('=', '":"') + '"';});
qp = qp ? JSON.parse('{' + arr.join() + '}',
function (key, value) {
return key === "" ? value : decodeURIComponent(value);
}
) : {};

isValid = qp.state === sentState;

if ((
oauth2.auth.schema.get("flow") === "accessCode" ||
oauth2.auth.schema.get("flow") === "authorizationCode" ||
oauth2.auth.schema.get("flow") === "authorization_code"
) && !oauth2.auth.code) {
if (!isValid) {
oauth2.errCb({
authId: oauth2.auth.name,
source: "auth",
level: "warning",
message: "Authorization may be unsafe, passed state was changed in server. The passed state wasn't returned from auth server."
});
}

if (qp.code) {
delete oauth2.state;
oauth2.auth.code = qp.code;
oauth2.callback({auth: oauth2.auth, redirectUrl: redirectUrl});
} else {
let oauthErrorMsg;
if (qp.error) {
oauthErrorMsg = "["+qp.error+"]: " +
(qp.error_description ? qp.error_description+ ". " : "no accessCode received from the server. ") +
(qp.error_uri ? "More info: "+qp.error_uri : "");
}

oauth2.errCb({
authId: oauth2.auth.name,
source: "auth",
level: "error",
message: oauthErrorMsg || "[Authorization failed]: no accessCode received from the server."
});
}
} else {
oauth2.callback({auth: oauth2.auth, token: qp, isValid: isValid, redirectUrl: redirectUrl});
}
window.close();
}

if (document.readyState !== 'loading') {
run();
} else {
document.addEventListener('DOMContentLoaded', function () {
run();
});
}
</script>
</body>
</html>
Loading

0 comments on commit c96c7b4

Please sign in to comment.