This project is a simple bank application developed using Go and several libraries and tools to manage database migrations, configurations, testing, and more.
The following libraries and tools are used in this project:
- Migrate: A tool for managing database migrations.
- SQLC: Generates Go code from SQL queries.
- Gin: A web framework for building APIs.
- Viper: A configuration solution for Go applications.
- Gomock: A mocking framework for Go.
- golang-jwt: A library for creating and verifying JSON Web Tokens.
- PASETO: A library for creating and verifying Platform-Agnostic SEcure TOkens.
Migrate is used to manage database migrations.
-
Set the version variable:
version=v4.15.0
-
Download Migrate:
curl -L https://github.com/golang-migrate/migrate/releases/download/$version/migrate.linux-amd64.tar.gz -o migrate.tar.gz -
Extract the tar file:
tar xvzf migrate.tar.gz
-
Move Migrate to
/usr/local/bin:sudo mv migrate /usr/local/bin/migrate
-
Verify the installation:
migrate -version
- Download the Migrate binary from here.
- Extract the
migrate.exefile to a directory of your choice. - Add the directory to your system's PATH variable.
- Verify the installation by opening a command prompt and running:
migrate -version
- Create a migration:
migrate create -ext sql -dir db/migration -seq init_schema
-
Enter the PostgreSQL container:
docker exec -it postgres12 /bin/sh -
Access PostgreSQL:
psql simple_bank
-
Drop the
simple_bankdatabase:dropdb simple_bank
-
Create the
simple_bankdatabase:docker exec -it postgres12 createdb --username=wginroot --owner=wginroot simple_bank -
Access the
simple_bankdatabase:docker exec -it postgres12 psql -U wginroot simple_bank
-
Enter the PostgreSQL container:
docker exec -it postgres12 cmd -
Access PostgreSQL:
psql simple_bank
-
Drop the
simple_bankdatabase:dropdb simple_bank
-
Create the
simple_bankdatabase:docker exec -it postgres12 createdb --username=wginroot --owner=wginroot simple_bank
-
Access the
simple_bankdatabase:docker exec -it postgres12 psql -U wginroot simple_bank
SQLC is used to generate Go code from SQL queries.
-
Install SQLC:
go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
-
Initialize SQLC:
sqlc init
go.mod is essential for managing dependencies in a Go project.
- Create the
go.modfile:go mod init github.com/techschool/simple_bank
- Add missing dependencies and remove unused ones, then update
go.sum:go mod tidy
-
Gin Web Framework:
- Install Gin:
go get -u github.com/gin-gonic/gin
- Install Gin CLI Tool:
go install github.com/codegangsta/gin@latest
- Start the project:
Or:
gin run main.go
gin --port 8080 run main.go
- Install Gin:
-
Viper:
- Install Viper:
go get github.com/spf13/viper
- Install Viper:
-
Gomock:
mockgen เป็นเครื่องมือในภาษา Go ที่ใช้ในการสร้าง mock objects สำหรับการทดสอบโค้ด โดยเฉพาะในการทดสอบ unit test-
Install Gomock:
go install github.com/golang/mock/mockgen@latest or go get github.com/golang/mock/mockgen@latest
-
set up for ubuntu
export PATH=$PATH:$(go env GOPATH)/bin -
-
golang-jwt:
- Install golang-jwt:
go get github.com/golang-jwt/jwt
- Install golang-jwt:
-
PASETO:
- Install PASETO:
go get github.com/o1egl/paseto
- Install PASETO:
Migrate is a tool used for managing database migrations. It helps in applying, reverting, and tracking changes to the database schema.
SQLC
SQLC generates Go code from SQL queries. This tool ensures that your database queries are type-safe and reduces boilerplate code.
Gin
Gin is a web framework written in Go. It provides a robust set of features for building RESTful APIs and web services.
Viper
Viper is a comprehensive configuration solution for Go applications. It supports reading from various configuration file formats and environment variables.
Gomock
Gomock is a mocking framework for Go. It helps in creating mocks for interfaces, which is useful for unit testing.
golang-jwt
The golang-jwt library is used for creating and verifying JSON Web Tokens (JWT). It simplifies authentication and authorization processes in your application.
PASETO
PASETO (Platform-Agnostic SEcure TOkens) is a token format designed to be more secure and easier to use than JWT. It addresses common security pitfalls associated with JWT.
Conclusion
This README provides a comprehensive guide to setting up and using various tools and libraries for the Simple Bank project. Follow the steps carefully to ensure a smooth setup process.
Docker
Docker create network เป็นการสร้างเครื่อข่ายเชื่อมต่อคอนเทนเนอร์หลายตัวกับเครือข่าย
-
Create network with docker:
docker network create bank-network
-
network connect multi docker:
docker network connect bank-network postgres12
-
ตรวจสอบรายละเอียดของเครือข่าย Docker:
docker network inspect bank-network
Use Docker for deploy project
-
Create Docker image with Dockerfile:
docker build -t simplebank:latest . -
Check Docker image:
docker images
-
Start Docker image with Dockerfile:
docker run --name simplebank -p 8080:8080 -e GIN_MODE=release simplebank:latest Or use Network docker run --name simplebank --network bank-network -p 8080:8080 -e GIN_MODE=release -e "DB_SOURCE=postgresql://wginroot:secret@postgres12:5432/simple_bank?sslmode=disable" simplebank:latesคำอธิบาย:
--name simplebank: กำหนดชื่อให้กับคอนเทนเนอร์-p 8080:8080: แมพพอร์ต 8080 บนเครื่องของคุณกับพอร์ต 8080 ในคอนเทนเนอร์-e GIN_MODE=release: ตั้งค่าตัวแปรสภาพแวดล้อมGIN_MODEให้เป็นreleasepostgres12: Database container name--network bank-network: Use Network is bank-network
- docker container inspect container_name => ตรวจสอบรายละเอียดของ container
- docker network inspect container_name => ตรวจสอบรายละเอียดเครือข่ายของ container
wait-for.sh เป็นสคริปต์ Bash ที่ช่วยในการรอให้บริการหรือทรัพยากรภายนอก (เช่น ฐานข้อมูล) พร้อมใช้งานก่อนที่จะเริ่มทำงานของแอปพลิเคชัน โดยเฉพาะในบริบทของการพัฒนาหรือการใช้งาน Docker คอนเทนเนอร์
คุณสามารถใช้ wait-for.sh เพื่อรอให้บริการที่ระบุพร้อมใช้งาน จากนั้นจึงดำเนินการคำสั่งที่ต้องการ
สมมติว่าคุณต้องการรอให้บริการ PostgreSQL บนโฮสต์ database พอร์ต 5432 พร้อมใช้งานก่อนที่จึงจะดำเนินการคำสั่งต่อไป คุณสามารถใช้สคริปต์ดังนี้:
./wait-for.sh database:5432 -- echo "Database is up"คือบริการจาก AWS ที่ใช้สำหรับเก็บและจัดการ container images สำหรับ Docker โดยมีคุณสมบัติดังนี้
`คุณสมบัติหลัก`
1. การเก็บรักษา: ECR ช่วยให้คุณสามารถเก็บ container images ได้อย่างปลอดภัย พร้อมการเข้ารหัสข้อมูลทั้งที่เก็บและส่งข้อมูล
2. การจัดการ: ECR มีเครื่องมือในการจัดการและเวอร์ชันของ images รวมถึงการจัดการการเข้าถึง (IAM) เพื่อควบคุมว่าใครสามารถเข้าถึงและจัดการ images ได้
3. การบูรณาการ: ECR ทำงานร่วมกับบริการอื่น ๆ ของ AWS ได้ดี เช่น Amazon ECS (Elastic Container Service) และ EKS (Elastic Kubernetes Service) เพื่อให้คุณสามารถใช้ container images ได้ง่ายขึ้นในแอปพลิเคชันของคุณ
4. ประสิทธิภาพ: ECR มีความสามารถในการทำ cache และดึง images อย่างรวดเร็ว ช่วยลดเวลาในการ deploymentการสร้างและ push images: คุณสามารถสร้าง Docker images และ push ขึ้นไปยัง ECR เพื่อเก็บไว้ การดึง images: เมื่อคุณต้องการใช้งาน container images ในการ deploy สามารถดึง images จาก ECR ได้โดยตรง
เป็นบริการของ AWS ที่ช่วยจัดการการเข้าถึงทรัพยากร AWS อย่างปลอดภัย โดยผู้ใช้สามารถควบคุมว่าใครสามารถทำอะไรกับทรัพยากรใน AWS ได้บ้าง IAM ช่วยให้คุณสามารถ
เพิ่มเติมที่ ที่นี่
`login aws`
- get password: aws ecr get-login-password
or
- aws ecr get-login-password | docker login --username AWS --password-stdin 325274678571.dkr.ecr.ap-southeast-2.amazonaws.com
Amazon Elastic Kubernetes Service (EKS) คือบริการที่มีการจัดการจาก Amazon Web Services (AWS) สำหรับการใช้งาน `Kubernetes` ซึ่งเป็นแพลตฟอร์มโอเพ่นซอร์สสำหรับการจัดการ containerized applications. EKS ช่วยให้ผู้ใช้สามารถเริ่มต้นและบริหารจัดการ `Kubernetes` บน AWS ได้ง่ายขึ้นโดยที่ไม่ต้องตั้งค่าและบริหารจัดการ control plane ของ `Kubernetes` เอง
- Install kubernetes to local form `https://kubernetes.io/docs/tasks/tools/install-kubectl-linux/`
- Connect kube and aws with `aws eks update-kubeconfig --name simple-bank --region ap-southeast-2`
- Check config detail with `cat ~/.kube/config`
คำสั่ง aws sts get-caller-identity ใช้เพื่อตรวจสอบและแสดงข้อมูลเกี่ยวกับ IAM identity (ผู้ใช้หรือ role) ที่กำลังใช้งาน AWS CLI หรือ SDK ในขณะนั้น
- aws sts get-caller-identity
คำสั่งนี้ใช้เพื่อแสดงข้อมูลเกี่ยวกับ Kubernetes cluster ที่คุณเชื่อมต่ออยู่
- kubectl cluster-info
คำสั่งนี้ใช้เพื่อแสดงรายการของ pods ที่กำลังรันอยู่ใน Kubernetes cluster
- kubectl get pods
K9s เป็นเครื่องมือที่ใช้สำหรับการจัดการและมอนิเตอร์ Kubernetes clusters ผ่านทาง command-line interface (CLI) ที่มีลักษณะของ UI แบบ text-based ซึ่งช่วยให้การจัดการและตรวจสอบ Kubernetes cluster ง่ายและรวดเร็วขึ้น
- curl -sS https://webinstall.dev/k9s | bash
- k9s version
- k9s
โดยใช้เครื่องมือหลายตัวที่เกี่ยวข้องกับการสร้างเอกสารฐานข้อมูล (DB Docs) จากโค้ด DBML (Database Markup Language) ซึ่งในภาพประกอบด้วย
- DBML
เป็นภาษาที่ใช้ในการนิยามโครงสร้างฐานข้อมูลเชิงสัมพันธ์ (Relational Database) ทำให้การออกแบบและจัดการฐานข้อมูลง่ายขึ้นผ่านการเขียนโค้ด
- Dbdocs
เครื่องมือที่ช่วยสร้างเอกสารจากโค้ด DBML โดยแปลงข้อมูลโครงสร้างฐานข้อมูลให้เป็นเอกสารที่เข้าใจง่ายและสามารถแชร์ได้
- Dbdiagram
ใช้สำหรับการสร้างและแสดงภาพ (diagram) ของโครงสร้างฐานข้อมูลที่ช่วยให้การสื่อสารระหว่างทีมพัฒนาเป็นไปอย่างมีประสิทธิภาพ
-> https://grpc.io/docs/languages/go/quickstart/
-> https://grpc.io/docs/protoc-installation/
gRPC และ Protocol Buffers (protobuf) เป็นเทคโนโลยีที่ช่วยในการสื่อสารระหว่างบริการ (services) ในระบบ distributed systems หรือไมโครเซอร์วิส
`วิธีการทำงานร่วมกันระหว่าง gRPC & protobuf`
1. เขียนไฟล์ .proto: คุณจะสร้างไฟล์ที่บอกว่าโปรแกรมควรจะส่งข้อมูลแบบไหนและสื่อสารกันอย่างไร
2. คอมไพล์ไฟล์ .proto: ใช้เครื่องมือเพื่อแปลงไฟล์นี้เป็นโค้ดที่โปรแกรมของคุณสามารถใช้ได้
3. พัฒนาโปรแกรม: เขียนโปรแกรมที่ใช้โค้ดจากการคอมไพล์เพื่อเรียกใช้ฟังก์ชันที่อยู่บนเซิร์ฟเวอร์หรือรับข้อมูลจากเซิร์ฟเวอร์
- gRPC: เหมือนการโทรศัพท์ไปถามคำตอบจากเพื่อน
- protobuf: เหมือนการเขียนจดหมายที่สั้นและชัดเจน เพื่อให้เพื่อนอ่านเข้าใจและตอบกลับได้เร็ว
เพิ่มเติมที่ ที่นี่
ป็นโครงการโอเพนซอร์สที่พัฒนาโดยผู้ใช้ GitHub ชื่อ ktr0731 ซึ่ง Evans เป็นเครื่องมือที่ใช้สำหรับการโต้ตอบกับ gRPC APIs ผ่าน CLI (Command Line Interface) หรือที่เรียกได้ว่าเป็น gRPC client ในรูปแบบ command-line
Install ktr0731/evans
1. ติดตั้ง Evans ผ่าน Go:
go install github.com/ktr0731/evans@latest
2. เพิ่ม Go binary path ไปยัง $PATH:
- ls $(go env GOPATH)/bin/evans
- nano ~/.bashrc
- export PATH=$PATH:$(go env GOPATH)/bin
- source ~/.bashrc
3. ตรวจสอบการติดตั้ง:
evans --version
grpcurl เป็นเครื่องมือ command-line ที่ช่วยทดสอบ gRPC services โดยไม่ต้องเขียน client code ช่วยให้การเรียกใช้ services และการ debug ทำได้สะดวกผ่านการส่งและรับข้อมูลในรูปแบบ JSON
- ดาวน์โหลด
grpcurlจาก ที่นี่ - ตรวจสอบการติดตั้งด้วยคำสั่ง:
grpcurl --version
- List services บน gRPC Server ใช้คำสั่งนี้เพื่อแสดงรายชื่อ services ที่ gRPC server ให้บริการ:
grpcurl -plaintext localhost:50051 list
- List methods ของ service:
grpcurl -plaintext localhost:50051 list pb.SimpleBank
- เรียกใช้งาน method บน gRPC Server
grpcurl -plaintext -d '{"username": "test_user", "password": "test_pass"}' localhost:50051 pb.SimpleBank.LoginUser
- เรียกใช้ method ที่ไม่ต้องการข้อมูล
grpcurl -plaintext localhost:50051 pb.SimpleBank.GetUser
6.5. ใช้งานกับ TLS (Secure Connection)
grpcurl -insecure -d '{"username": "test_user", "password": "test_pass"}' localhost:50051 pb.SimpleBank.LoginUser
เพิ่มเติมที่ ที่นี่
เป็นเครื่องมือในระบบ gRPC ที่ช่วยให้สามารถสร้าง RESTful HTTP API โดยทำการแปลงการเรียก gRPC ไปเป็นการเรียกผ่าน HTTP/1.1 และใช้ JSON ในการสื่อสารได้โดยอัตโนมัติ ซึ่งช่วยให้ผู้พัฒนาสามารถรองรับการสื่อสารทั้งแบบ gRPC และ RESTful API ได้ในเวลาเดียวกัน โดยไม่ต้องเขียนโค้ดสองชุดแยกกัน
1. `API การแปลง:` grpc-gateway ทำหน้าที่เป็น proxy ระหว่าง HTTP และ gRPC โดยมันจะรับคำร้องจาก HTTP (REST API) ที่ส่งมาเป็น JSON และแปลงคำร้องเหล่านั้นให้เป็นการเรียก gRPC ภายใน
2. `การเชื่อมโยงระหว่าง gRPC กับ HTTP:` เราจะใช้ไฟล์ .proto (ไฟล์ที่กำหนดโครงสร้างของ gRPC service) เพิ่มคำสั่งที่บอกว่า RPC method แต่ละตัวจะถูกแมปกับ HTTP method (GET, POST, PUT, DELETE) และ endpoint ใด
3. `การตอบกลับ:` เมื่อ gRPC ส่งผลลัพธ์กลับมา มันจะถูกแปลงเป็น JSON และส่งกลับไปยัง client ผ่าน HTTP
clone project ที่นี่
1. move file to proto/google/api
`
google/api/annotations.proto
google/api/field_behavior.proto
google/api/http.proto
google/api/httpbody.proto
`
2. add option in your service `SimpleBank`
`
option (google.api.http) = {
post: "/v1/create_user"
body: "*"
};
`
3. add `--grpc-gateway_out=pb --grpc-gateway_opt=paths=source_relative` in proto script
4. run make proto
clone project ที่นี่
1. move file *.proto in Folder protoc-gen-openapiv2/options/ to your project proto/protoc-gen-openapiv2/options
2. create option in service_simple_bank.proto
3. add `--openapiv2_out=doc/swagger --openapiv2_opt=allow_merge=true,merge_file_name=simple_bank` in proto script
4. run make proto
clone project ที่นี่
1. move all file in folder dist to your folder `doc/swagger`
2. chang url in file swagger-initializer.js to your swagger file json `(doc/swagger/simple_bank.swagger.json)`
3. add config runGatewayServer at mark TODO: Setup Swagger in main.go file
4. go to swagger with `http://localhost:8080/swagger`
เพิ่มเติมที่ ที่นี่
สร้าง log ในระบบของ golang ด้วย zerolog