SimpleBank

command module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 13, 2023 License: MIT Imports: 25 Imported by: 0

README

SimpleBank

Section Database


  1. DB Migration lib
$ # Go 1.16+
$ go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@$TAG
  1. migration in sqlc

    • sqlc does not perform database migrations for you. However, sqlc is able to differentiate between up and down migrations. sqlc ignores down migrations when parsing SQL files.

    • sqlc supports parsing migrations from the following tools:

      • dbmate
      • golang-migrate
      • goose
      • sql-migrate
      • tern
  2. comparing database/sql package, gorm, sqlx, and sqlc

  3. Transaction

    • A transaction is a single logical unit of work that accesses and possibly modifies the contents of a database. Transactions access data using read and write operations.
    • In order to maintain consistency in a database, before and after the transaction, certain properties are followed. These are called ACID properties.

https://www.geeksforgeeks.org/acid-properties-in-dbms/

  1. sql anomaly

  2. Deadlock in database

    In a database, a deadlock is a situation in which two or more transactions are waiting for one another to give up locks. For example, Transaction A might hold a lock on some rows in the Accounts table and needs to update some rows in the Orders table to finish.

  3. Github CI

Section RESTFful API


  1. Gin
  1. Viper

  2. Mock Testing

    • Mock testing are using "White Box" testing techniques, used to testing every functional func in code. (https://github.com/golang/mock)
    • Stub is replacement for some dependency in your code that will be used during test execution. It is typically built for one particular test and unlikely can be reused for another because it has hardcoded expectations and assumptions.
  3. Testing Method TDD / BDD / ATDD

  • TDD (test-driven development) approach with step:
    • First creating failing unit test.
    • Then start implementing unit test with MINIMAL code until all unit test condition are satisfied.
    • Last testing are passed, programmer refactor the design & making an improvement without changing the behavior from second step.
  • TDD in short explanation:
    • TDD steps :
      • Create unit test(Code) that including all process nedeed/writen.
      • Test unitTest (Failed result test).
      • Then repeating changes/implement code until all test are passed.
      • Done TDD cycle.
    • TDD Auidence are programmer-side since it's focuses more on code implementation of a feature.
    • TDD are more likely realted to "White Box" testing techniques.
  • BDD (Behavioral-Driven Development) are derived/similar from TDD but using the Given-When-Then approach is used for writing test cases. Some Given-When-Then example:

    • Given the user has entered valid login credentials
    • When a user clicks on the login button
    • Then display the successful validation message
  • BDD in short explanation:

    • BDD Steps are similar to TDD but "unit test" test are changed to Behavioral test that writen and describe in human language (ex:English).
    • BDD Auidence are more wide to other than programmer team, because main focus on Understanding Requirements.
    • BDD are more likely realted to "Black Box" testing techniques.
  • ATDD (Acceptance Test-Driven development) are similar with BDD focuses more on the behavior of the feature, whereas ATDD focuses on capturing the precise requirements. ATDD in short explanation:

    • BDD are still very similar to BDD testing techniques but focusing in More detailed an precise requirements at development feature.
  1. PASETO

Section Deploying aplication


  1. Docker images tag (alpine / bullseye / buster / windowsservercore / nanoserver):
  1. Docker build Documentation:
  1. Connecting docker container with docker network
  • create a new docker network and listed images to network
  1. Create docker serveral docker images with docker-compose.yaml
  • With docker-compose.yaml we can automated creating serveral docker images and listed it to same network automaticly

Section Session & GRPC

  1. Session token and Access Token

    • Refresh tokens provide a way to bypass the temporary nature of access tokens. Normally, a user with an access token can only access protected resources or perform specific actions for a set period of time, which reduces the risk of the token being compromised. A refresh token allows the user to get a new access token without needing to log in again.
    • Refresh Token best practice : https://stateful.com/blog/refresh-tokens-security
  2. DbDocs.io

    • Make sure NodeJS and NPM have been installed on your computer before the installation.
    • instal Dbdocs CLI
    $ npm install -g dbdocs
    $ dbdocs -login
    $ dbdocs build ~/path/to/database.dbml
    $ dbdocs password --set <password> --project <project name>
    
    $ npm install -g @dbml/cli
    $ dbml2sql schema.dbml --postgres -o schema.sql
    $ dbml2sql <path-to-dbml-file>
           [--mysql|--postgres|--mssql]
           [-o|--out-file <output-filepath>]
    
    • Convert a SQL file to DBML
    $ npm install -g @dbml/cli
    $ sql2dbml dump.sql --postgres
    $ sql2dbml --mysql dump.sql -o mydatabase.dbml
    $ sql2dbml <path-to-sql-file>
           [--mysql|--postgres|--mssql]
           [-o|--out-file <output-filepath>]
    
    • DBML CLI make dbml-error.log file as error output even it's nothings wrong.
  3. RPC Remote Procedure Call

  4. GRPC / Google RPC Framework

    • using GRPC ProtoBuff tolls can server Both GRPC and HTTP request
    • 4 types of gRPC
      1. Unary. (1 : 1)
      2. Server Streaming. (1 : many)
      3. Client Streaming. (many : 1)
      4. Bi-directional streaming. (many : many)
    • website https://grpc.io/
  5. Protocol Buffer (https://protobuf.dev/programming-guides/proto3/) V3

    $ go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28
    $ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2
    
    $ protoc --go_out=. --go_opt=paths=source_relative \
    --go-grpc_out=. --go-grpc_opt=paths=source_relative \
    helloworld/helloworld.proto
    
    --openapiv2_out=doc/swagger
    
  6. GRPC tools to testing

    evans --host localhost -p 9090 -r repl
    
    • Postman also support gRPC too.
    • To use Service definition using server reflection, gRPC server mus registeret to pakcage reflection
      • reflection.Register(grpcServer)
  7. gRPC gateway

    • gRPC-Gateway is a plugin of protoc. It reads a gRPC service definition and generates a reverse-proxy server which translates a RESTful JSON API into gRPC.
    • This project aims to provide that HTTP+JSON interface to your gRPC service because, you might still want to provide a traditional RESTful JSON API as well. Reasons can range from maintaining backward-compatibility, supporting languages or clients that are not well supported by gRPC
    • Source code : https://github.com/grpc-ecosystem/grpc-gateway
  8. gRPC request validator

    • in gin using binding/v10 in gRPC using "google.golang.org/genproto/googleapis/rpc/errdetails"
    • when making error validtaion best practice to write field same as request param name (ex : full_name) to create consistency.
  9. Authentication in GRPC

    • SSL/TLS: gRPC has SSL/TLS integration and promotes the use of SSL/TLS to authenticate the server.
    • ALTS: gRPC supports ALTS as a transport security mechanism.
    • Token-based authentication with Google : gRPC provides a generic mechanism to attach metadata based credentials to requests and responses.
      • Google credentials should only be used to connect to Google services. Sending a Google issued OAuth2 token to a non-Google service could result in this token being stolen and used to impersonate the client to Google services.
    • https://grpc.io/docs/guides/auth/
  10. Logger framework for go

  11. Swagger-ui

    • Swagger UI allows anyone — be it your development team or your end consumers — to visualize and interact with the API’s resources without having any of the implementation logic in place. It’s automatically generated from your OpenAPI (formerly known as Swagger) Specification, with the visual documentation making it easy for back end implementation and client side consumption.
    • using it in our local server with just adding swagger-ui/dist to our project and serve it as fileserver
    • https://github.com/swagger-api/swagger-ui
    • using rakyll/statik to allows you to embed a directory of static files into your Go binary (it' wil faster since it's no need to read file when called)
    • https://github.com/rakyll/statik

Section Asynchronous processing with background workers


  1. asynq lib
  2. Redis
    • Redis is an open source (BSD licensed), in-memory data structure store used as a database, cache, message broker, and streaming engine.
    • Redis Data type https://redis.io/docs/data-types/
    • Best practices https://climbtheladder.com/10-redis-key-best-practices/
    • Always seting task with some delay so database state will be ready and redis task will satified.
    • To start redis in windows bash we must bring .conf file while starting redis-server.exe
    $ redis-server.exe ~/path/to/redis/redis.windows.conf
    
  3. Simple Mail Transfer Protocol (SMTP)
    • standart liblary from go :https://pkg.go.dev/net/smtp (The smtp package is frozen and is not accepting new features. Some external packages provide more functionality.)
    • lib used :https://github.com/jordan-wright/email
    • testing.Short() used flag when testing is too long to execute and we wnat to skip it (code inside .short() t.Skip())
      • adding --short flag to set it true when calling "go test"
    • AWS SES (amazon Simple Email Services)
  4. Creating unit testing for gRPC services that included backgorund wokers (redis) using go-mock
    • Since there are two things need to be mocked (Database, Redis) in test code we must separate controler for each mock.
    • Code violations from database must be mapped separately to get expected error.
    • Token auth can embeded using metadata.NewIncomingContext (package google.golang.org/grpc/metadata).

ETC


  1. explanation of "var _ Interface = (*Type)(nil)"
  2. .yaml
  3. Bash
  4. Some API management tools that support gRPC testing include Postman, Insomnia, Kreya. app, and BloomRPC
  5. iana standart HTTP/2 response code
  6. GRPC Status code (1 - 16) and some case code will procude:
  7. GRPC vs WebSocket
  8. GRPC VS REST api
  9. MQTT
  10. Networking Technique Multiplexing (Multiplexers and de-Multiplexers)
  11. Serialization data
  12. Anatomy of API (Application Programming Interface)
  13. Phyton Framework Django vs Flask
  14. Swagger hub to populate api documentation (exmaple file in doc/swagger/*.json)
  15. 5 types of design patterns:
    • Creational patterns are used to deal with creating objects.
    • Structural patterns are used to build idiomatic structures.
    • Behavioural patterns are used to manage mostly with algorithms.
    • Concurrency patterns are used to manage the timing execution and order execution of applications that have more than one flow.
    • Microservice patterns are used to build microservice applications.
  16. REST API requests POST, GET, DELETE, PUT / PATCH ?
  17. Postman for testing Support http, gRPC, documenting, easy value set, etc
  18. synchronous and asynchronous
  • Default port
    • postgre : 5432
    • redis : 6379
    • webapps : 8080
  1. Docker windows/macos need to be curl in docker-machine -ip instead of localhost
  2. Redis docker Container connecting with redis-cli
  3. next GRPC
  4. kafka lib for golang

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
api
costumValidator
Costum validator for json request
Costum validator for json request
db
mock
Package mockdb is a generated GoMock package.
Package mockdb is a generated GoMock package.
doc
Package pb is a reverse proxy.
Package pb is a reverse proxy.
Enum of currency that support in apps
Enum of currency that support in apps
Package val populate func() to validate incoming request
Package val populate func() to validate incoming request
Costume logger for asyqn redis implementing: Debug(args ...interface{}) Info(args ...interface{}) Warn(args ...interface{}) Error(args ...interface{}) Fatal(args ...interface{})
Costume logger for asyqn redis implementing: Debug(args ...interface{}) Info(args ...interface{}) Warn(args ...interface{}) Error(args ...interface{}) Fatal(args ...interface{})
mock
Package mockworker is a generated GoMock package.
Package mockworker is a generated GoMock package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL