Skip to content

chinhnguyen-95/book-management-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Book Management API

A simple microservice written in Golang to manage books, demonstrating backend skills, RESTful APIs, SQL/NoSQL integration, and containerization.


Features

  • 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.

Getting Started

Prerequisites

Make sure the following tools are installed on your system:


Running the Application

Follow these steps to build and run the application with its database services:

  1. Clone the Repository:

    git clone https://github.com/chinhnguyen-95/book-management-api.git
    cd book-management-api
  2. Build and Start Services:

    Use docker-compose to build and start the application and database services:

    docker-compose up --build
  3. Verify the Application:

    • The API will be available at: http://localhost:8080
    • PostgreSQL will run on port 5432.
    • MongoDB will run on port 27017.
  4. Test the API:

    Use tools like curl or Postman to interact with the API. Example:

    curl -X GET http://localhost:8080/books

API Endpoints

1. Retrieve All Books

  • GET /books
  • Response:
    [
      { "id": "1", "title": "1984", "author": "George Orwell" },
      { "id": "2", "title": "The Great Gatsby", "author": "F. Scott Fitzgerald" }
    ]

2. Retrieve a Book by ID

  • GET /books/{id}
  • Response:
    { "id": "1", "title": "1984", "author": "George Orwell" }

3. Add a New Book

  • 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" }

4. Update a Book

  • PUT /books/{id}
  • Request Body:
    { "title": "New Title", "author": "New Author" }
  • Response:
    { "id": "1", "title": "New Title", "author": "New Author" }

5. Delete a Book

  • DELETE /books/{id}
  • Response: HTTP status 204 No Content

Configuration

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"
  }
}

CI/CD Pipeline

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

Deploy to AWS

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.

Steps

  1. 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
  2. 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.
  3. 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

Next Steps

  • 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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published