todoapp-backend
Rest API for todoapp written in Golang, using Clean Architecture and CI/CD (includes unit tests and integration tests).
Using:
- Web framework: Gin
- Authentication: JWT
- Database (choose one out of three options):
- In Memory DB
- Microsoft SQL Server (using Gorm)
- MongoDB
- CI/CD:
- GitHub Actions (Docker Image for testing)
- Microsoft Azure Web App (Docker Image for deployment)
Note: Config to change database (default to In Memory DB)
Getting Started
Development environment needed:
- Golang (>v1.13)
Clone the project to your working directory
git clone https://github.com/daopmdean/todoapp-backend.git
This project support to run on
- Memory Database
- Microsoft SQL Server Database
- MongoDB
Setup env on zsh
export <ENV_NAME>='
'
check to see if your environment declared
echo $<ENV_NAME>
Example
export NAME='Dean'
echo $NAME
-> Dean
PORT: environment variable to setup the port your program running on.
If you don't set PORT env variable, Program will run on default port: http://localhost:5000
Example:
export PORT=3456
Your program will run on -> http://localhost:3456
DB Mode
You can choose DB Mode to run with DB_MODE environment variable. If you do not provide any, default the program will run with memory db.
Memory DB
Run the program
make run
MS SQL DB
export DB_MODE=mssql
Using ORM Library: Gorm
Setup env variables
- DB_USERNAME: Username to connect to sql server
- DB_PASSWORD: Password to connect to sql server
- DB_HOST: Host to connect to sql server
- DB_PORT: Port to connect to sql server
- DB_NAME: Database name to connect to sql server
Example
export DB_USERNAME=sa
export [email protected]
export DB_HOST=localhost
export DB_PORT=1433
export DB_NAME=todoapp
Run the program
make run
MongoDB
export DB_MODE=mongodb
Setup env variables
- MONGO_URI: Connection string to MongoDB
- MONGO_DB_NAME: Database name
Run init mongodb to create indexes
Note: this command should run only once when initialize MongoDB
make init_mongodb
Run the program
make run
Test
To run all tests
make test_all
To run api tests
make test_api
To run usecase tests
make test_usecase
To run entity tests
make test_entity
System Architect
The system designed following The Clean Architecture guideline.
Project structure
todoapp
├── cmd/server
│ └── main.go
├── internal
│ └── adapter
│ └── http/rest
│ └── memdb
│ └── mssqldb
│ └── mongodb
│ └── common/config
│ └── entity
│ └── usecase
│ └── repo
└── test
└── api_rest_test
└── repomock
Annotate
- cmd/server/main.go: run the program
- adapter: store plugins that you can easily replace with another implementations
- http/rest: store api handle http request with gin-gonic framework
- memdb: implementations for memory db
- mssqldb: implementations for microsoft sql server db
- mongodb: implementations for mongodb
- test: store test cases