# README
go-sql-crud docker-composed app
About
- a
docker-composed golang sql
app that performs CRUD operations. - areas focused are
maintainability
,clean code
,simplicity
,scalability
,testability
. - demonstrates system design principles, code organization, OOP principles, and best practices in golang.
Technical key points
Ports
8080
- web port3306
- mysql port
How to run app
https://github.com/harssRajput/go_crud_sql/assets/82873133/65c3fb63-e068-4ff9-a583-582952007c82
https://github.com/harssRajput/go_crud_sql/assets/82873133/4dbd6cbd-da2e-4b25-87c9-48ff7f369cd5
- clone the repo
git clone [email protected]:harssRajput/go_crud_sql.git
- install docker and docker-compose.
- open terminal and head over to the project directory
- Now, Two ways to run the app:
- Using docker-compose (recommended one)
docker-compose up
ii. manual deployment of mysql and golang app
docker run --name mysql -e MYSQL_ROOT_PASSWORD=root -v ./scripts/init.sql:/docker-entrypoint-initdb.d/1.sql -p 3306:3306 mysql
go run main.go
About Tests
Two type of test cases are written which are covering all the scenarios.
- Unit test cases - it covers all the scenarios of the functions.
- Integration test cases - it covers end to end functionality of the APIs.
How to run tests
- install docker and docker-compose.
- take a clone and head over to the project directory as mentioned above in the
How to run app
section. - run the below command. it executes all testcases in the docker container.
go test ./... -v
Directory structure
.
├── Dockerfile
├── README.md
├── docker-compose.yml
├── go.mod
├── go.sum
├── main.go
├── scripts/
├── tests/
└── internal
├── entity/
├── handler/
├── server/
├── service/
├── utilityStore/
main.go
- entry point of the application.
scripts/
- contains the sql script to create the database and tables schema.
tests/
- contains the integration test cases. (unit tests present alongside the functions)
internal/
- contains the main codebase. all business/domain logic resides here.
entity/
- contains the entity objects.
handler/
- contains the request and response objects.
server/
- contains the server configuration.
service/
- contains the business logic of the application.
utilityStore/
- provides everything that server needs during initialization. like db connection, logger, env vars, router, error handling, etc.
About APIs
APIs contract
API Name | Signature | method |
---|---|---|
Create Account | http://localhost:8080/accounts/ | POST |
Get Account | http://localhost:8080/accounts/2 | GET |
Create Transactions | http://localhost:8080/transactions/ | POST |
(NOTE: catch-all fallback route also added) |
Sample request/response
- Create Account
json request
{
"document_number": "12345678909"
}
json response
{
"account_id": 7,
"document_number": "12345678909"
}
- Get Account
json response
{
"account_id": 7,
"document_number": "12345678909"
}
- Create Transactions
json request
{
"account_id": 3,
"operation_type_id": 4,
"amount": 100,
"balance": 100
}
json response
{
"transaction_id": 15,
"account_id": 3,
"operation_type_id": 4,
"amount": 100,
"balance": 100,
"event_date": "2024-06-05T18:05:20.857Z"
}