A simple microservice written in Golang to manage books, demonstrating backend skills, RESTful APIs, SQL/NoSQL integration, and containerization.
- Golang Backend: Built with clean architecture principles.
- RESTful API: Supports full CRUD operations for book management.
- Database Integration:
- SQL: PostgreSQL for relational data.
- NoSQL: MongoDB for flexible schema-based data.
- Containerization: Dockerized application for seamless deployment.
- CI/CD Pipeline: GitHub Actions for automated builds and tests.
Make sure the following tools are installed on your system:
Follow these steps to build and run the application with its database services:
-
Clone the Repository:
git clone https://github.com/chinhnguyen-95/book-management-api.git cd book-management-api
-
Build and Start Services:
Use
docker-compose
to build and start the application and database services:docker-compose up --build
-
Verify the Application:
- The API will be available at:
http://localhost:8080
- PostgreSQL will run on port
5432
. - MongoDB will run on port
27017
.
- The API will be available at:
-
Test the API:
Use tools like
curl
or Postman to interact with the API. Example:curl -X GET http://localhost:8080/books
- GET
/books
- Response:
[ { "id": "1", "title": "1984", "author": "George Orwell" }, { "id": "2", "title": "The Great Gatsby", "author": "F. Scott Fitzgerald" } ]
- GET
/books/{id}
- Response:
{ "id": "1", "title": "1984", "author": "George Orwell" }
- POST
/books
- Request Body:
{ "id": "3", "title": "To Kill a Mockingbird", "author": "Harper Lee" }
- Response:
{ "id": "3", "title": "To Kill a Mockingbird", "author": "Harper Lee" }
- PUT
/books/{id}
- Request Body:
{ "title": "New Title", "author": "New Author" }
- Response:
{ "id": "1", "title": "New Title", "author": "New Author" }
- DELETE
/books/{id}
- Response:
HTTP status
204 No Content
The application reads configuration values from the config/config.json
file. Update the file to match your local setup if needed:
{
"server": {
"port": "8080"
},
"database": {
"dsn": "host=postgres user=youruser password=yourpassword dbname=bookdb port=5432 sslmode=disable"
},
"nosql": {
"uri": "mongodb://mongo:27017",
"database": "book_management",
"collection": "books"
}
}
The project includes a GitHub Actions workflow for automated testing and building. The pipeline is triggered on every push to the main
branch.
Workflow Includes:
- Dependency installation
- Linting and formatting checks
- Running unit tests
- Building the Docker image
This project can be deployed to AWS using Amazon ECS (Elastic Container Service). The deployment process involves building a Docker image, pushing it to Amazon ECR (Elastic Container Registry), and configuring ECS to run the service.
-
Build and Push the Docker Image:
aws ecr get-login-password --region <your-region> | docker login --username AWS --password-stdin <your-account-id>.dkr.ecr.<your-region>.amazonaws.com docker build -t book-management-api . docker tag book-management-api:latest <your-account-id>.dkr.ecr.<your-region>.amazonaws.com/book-management-api:latest docker push <your-account-id>.dkr.ecr.<your-region>.amazonaws.com/book-management-api:latest
-
Deploy to ECS:
- Create an ECS cluster.
- Create a Task Definition that uses the Docker image from Amazon ECR.
- Deploy the service to the cluster using Fargate.
-
GitHub Actions Workflow for AWS Deployment: Add the following GitHub Actions workflow file to automate deployment:
name: Deploy to AWS on: push: branches: - main jobs: deploy: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 - name: Log in to Amazon ECR uses: aws-actions/amazon-ecr-login@v1 - name: Build and push Docker image run: | docker build -t book-management-api . docker tag book-management-api:latest <your-account-id>.dkr.ecr.<your-region>.amazonaws.com/book-management-api:latest docker push <your-account-id>.dkr.ecr.<your-region>.amazonaws.com/book-management-api:latest - name: Deploy to ECS uses: aws-actions/amazon-ecs-deploy-task-definition@v1 with: task-definition: ecs-task-def.json service: book-management-service cluster: book-management-cluster
- Extend the API:
- Add advanced features like search, pagination, or user authentication.
- Deploy:
- Use a cloud platform like AWS, Google Cloud, or Azure for deployment.
- Monitor:
- Add logging and monitoring with tools like Prometheus or ELK Stack.