Skip to content

FIAP-SOAT-G20/tc4-kitchen-service

Repository files navigation

Fast Food FIAP Tech Challenge 4 - Kitchen Service

GitHub CI - Tests

Fast Food FIAP Tech Challenge 4 - G19

Go Gin PostgreSQL Docker Make GitHub Actions Swagger Postman

Fast Food

πŸ’¬ About

Repository for the FIAP Tech Challenge 4, focused on developing a microservice backend system for managing orders after the payment of a user is approved in a fast-food restaurant.

Tech Challenge 4 specifications can be found here. Youtube video explaining this project can be found here

Note

Tech Challenge 1, 2 and 3 repositories can be found in main page of the FIAP-SOAT-G20 organization here

πŸ”— Related Projects

This project is part of a larger system that includes:

  • Customer Service - Microservice responsible to authentication and management of customers
  • Order Service - Microservice responsible to manage new orders, products, categories
  • Payment Service - Microservice responsible to manage order checkout and comunicating with external payment service
  • Infrastructure - Terraform - Project responsible of create infrastructure to support all TC4 microservices.
  • Infrastructure - Deploy - Project responsible to publish the microservices into Kubernetes

πŸ“š Dictionary - Ubiquitous Language

  • Customer (actor): Actor responsible for initiating the purchasing process
  • Cook (actor): Actor responsible for preparing the customer's order
  • Attendant (actor): Actor responsible for interacting with the customer, providing support for the order
  • Identification method: Format in which the customer is identified on the platform: via CPF or anonymous.
  • Identification: Customer identification on the platform
  • Authorization: Grants permission to the customer to perform operations on the platform, such as placing an order, changing registration information
  • Order: Represents all items selected by the customer in the store
  • Order Status: Represents the stage of order preparation after payment is confirmed.

πŸ—οΈ Architecture

Flow Diagram

Flow Diagram

πŸ“‚ Project Structure

.
β”œβ”€β”€ bin
β”œβ”€β”€ cmd
β”‚   β”œβ”€β”€ server
β”‚   └── worker
β”‚       └── consumer
β”œβ”€β”€ docs
β”œβ”€β”€ internal
β”‚   β”œβ”€β”€ adapter
β”‚   β”‚   β”œβ”€β”€ controller
β”‚   β”‚   β”œβ”€β”€ gateway
β”‚   β”‚   └── presenter
β”‚   β”œβ”€β”€ core
β”‚   β”‚   β”œβ”€β”€ domain
β”‚   β”‚   β”‚   β”œβ”€β”€ entity
β”‚   β”‚   β”‚   └── value_object
β”‚   β”‚   β”œβ”€β”€ dto
β”‚   β”‚   β”œβ”€β”€ port
β”‚   β”‚   β”‚   └── mocks
β”‚   β”‚   └── usecase
β”‚   β”œβ”€β”€ infrastructure
β”‚   β”‚   β”œβ”€β”€ api
β”‚   β”‚   β”‚   └── response
β”‚   β”‚   β”œβ”€β”€ aws
β”‚   β”‚   β”‚   └── sqs
β”‚   β”‚   β”œβ”€β”€ config
β”‚   β”‚   β”œβ”€β”€ database
β”‚   β”‚   β”‚   └── migrations
β”‚   β”‚   β”œβ”€β”€ datasource
β”‚   β”‚   β”‚   └── model
β”‚   β”‚   β”œβ”€β”€ handler
β”‚   β”‚   β”‚   β”œβ”€β”€ request
β”‚   β”‚   β”‚   └── response
β”‚   β”‚   β”œβ”€β”€ httpclient
β”‚   β”‚   β”œβ”€β”€ logger
β”‚   β”‚   β”œβ”€β”€ middleware
β”‚   β”‚   β”œβ”€β”€ route
β”‚   β”‚   β”œβ”€β”€ server
β”‚   β”‚   └── setup
β”‚   └── util
β”‚       └── testdata
β”‚           β”œβ”€β”€ common
β”‚           β”œβ”€β”€ customer
β”‚           β”œβ”€β”€ order
β”‚           β”œβ”€β”€ order_product
β”‚           β”œβ”€β”€ product
β”‚           └── staff
β”œβ”€β”€ mockserver
β”œβ”€β”€ scripts
└── tests
    └── features
Project Structure Explanation

1️⃣ Core (Innermost layer)

  • domain/: Central business entities and rules.
  • dto/: Data transfer objects.
  • port/: Interfaces that define contracts between layers, ensuring independence.
  • usecase/: Application use cases.

2️⃣ Adapter (Middle layer)

  • controller/: Coordinates the flow of data between use cases and infrastructure.
  • presenter/: Formats data for presentation.
  • gateway/: Implements access to data from external sources (databases, APIs, etc.).

3️⃣ Infrastructure (External layer)

  • api/: Comunication with external API clients
  • aws/: Integration with AWS Services, like Simple Queue Service
  • config/: Application configuration management.
  • database/: Configuration and connection to the database.
  • datasource/: Concrete implementations of data sources.
  • handler/: Handling of HTTP requests.
  • httpclient/: HTTP client for external requests.
  • logger/: Structured logger for detailed logs.
  • middleware/: HTTP middlewares for handling requests.
  • route/: Definition of API routes.
  • server/: Initialization of the HTTP server.
  • setup/: Single point of entrance to initiate all core classes to be used on HTTP Server or by the Worker SQS Consumer.

πŸ’‘ Decisions

  • Clean Architecture structure: The project was structured using the Clean Architecture pattern, which aims to separate the application into layers, making it easier to maintain and test. The project is divided into three layers: Core, Adapter, and Infrastructure.
  • Presenter: The presenter (from Adapter layer) was created to format the data to be returned to the client. This layer is responsible for transforming the data into the desired format, such as JSON, XML, etc. Also, it is responsible for handling errors and returning the appropriate HTTP status code.
  • Use Case: The use case (from Core layer) was created to define the business rules of the application. This layer is responsible for orchestrating the flow of data between the entities and the data sources.
  • Middleware to handle errors: A middleware was created to handle errors and return the appropriate HTTP status code. This middleware is responsible for catching errors and returning the appropriate response to the client.
  • Structured Logger: A structured logger was created to provide detailed logs. This logger is responsible for logging information about the application, such as requests, responses, errors, etc.
  • Database Connection: The database connection was created using GORM, a popular ORM library for Go. This library provides an easy way to interact with the database and perform CRUD operations.
  • Database Migrations: Database migrations were created to manage the database schema. This allows us to version control the database schema and apply changes to the database in a structured way.
  • HTTP Server: The HTTP server was created using the Gin framework, a lightweight web framework for Go. This framework provides a fast and easy way to create web applications in Go.

(back to top)

✨ Features

  • Dockerfile: small image with multi-stage docker build, and multi-platform build (Cross-Compilation)
  • Makefile: to simplify the build and run commands
  • Clean architecture
  • PostgreSQL database
  • Conventional commits
more
  • Unit tests (testify)
  • Tests Suite (testify)
  • Code coverage report (go tool cover)
  • BDD (godog)
  • AWS integration
  • Swagger documentation
  • Postman collection
  • Feature branch workflow
  • Live reload (air)
  • Pagination
  • Health Check (liveness, readiness)
  • Lint (golangci-lint)
  • Vulnerability check (govulncheck)
  • Mocks (gomock)
  • Environment variables
  • Graceful shutdown
  • GitHub Actions (CI/CD)
  • GitHub Container Registry (GHCR)
  • Structured logs (slog)
  • Database migrations (golang-migrate)
  • API versioning
  • Dev Container (VS Code)
  • Semantic Versioning
  • Golden Files
  • Fixtures

πŸ’» Technologies

more

πŸ“œ Requirements

Build/Run with Docker

Build/Run Locally (development)

Warning

You need to have Go version 1.24 or higher installed on your machine to run the application locally

(back to top)

πŸ’Ώ Installation

git clone https://github.com/FIAP-SOAT-G20/tc4-kitchen-service.git
cd tc4-kitchen-service

Set the environment variables

cp .env.example .env

🐳 Docker

make compose-build

(back to top)

πŸƒ Running

Important

After running the application, a mock server container will be created to simulate the payment gateway.
When you create a new payment (with POST payments/:order_id/checkout) the order status will be updated from OPEN to PENDING,
then the mock server will call the webhook POST payments/callback,
and the order status will be updated from PENDING to RECEIVED.
You can verify mock server logs by running docker logs mockserver.10soat-g22.dev.

Important

Order and Products routes are protected by authentication, you need to use the API Gateway connected to the Lambda function to authenticate the user. Lambda Repository: here

Tip

We have created a step-by-step guide to test the application, you can find it here.

API Documentation will be available at:

🐳 Docker

make compose-up

Tip

To stop the application, run compose-down
To remove the application, run compose-clean

Note

The application will be available at http://localhost:8080 Ex: http://localhost:8080/api/v1/health

(back to top)

πŸ› οΈ Development

  1. Install Go: https://golang.org/doc/install
  2. Clone this repository: git clone https://github.com/FIAP-SOAT-G20/tc4-kitchen-service
  3. Change to the project directory: cd tc4-kitchen-service
  4. Checkout to the development branch: make new-branch
  5. Set the environment variables: cp .env.example .env
  6. Install dependencies by running make install
  7. Run the application by running make run-air or make run
  8. Access the application at http://localhost:8080
  9. Make your changes πŸ§‘β€πŸ’»
  10. Dont forget to run the tests by running make test
  11. Check the coverage report by running make coverage
  12. Check the lint by running make lint
  13. Update the swagger documentation by running make swagger
  14. Commit your changes following the Conventional Commits standard
  15. Push to the branch and Open a new PR by running make pull-request
  16. The GitHub Actions will run the tests, lint and vulnerability check automatically
  17. After the PR is approved, merge it to the main branch
  18. Generate a new release tag (here) with semantic versioning

Tip

7: make run will run the application locally, and will build and run PostgreSQL container using Docker Compose
Alternatively, you can run make run-air to run the application using Air (live reload)

Tip

18: When a new release tag is created, the GitHub Actions will build and push the image to the
GitHub Container Registry (GHCR) from GitHub Packages,
the image will be available at ghcr.io/fiap-soat-g20/tc4-kitchen-service:latest
About semantic versioning:
if you are fixing bugs, increment the patch version (v0.0.1)
if you are adding new features, increment the minor version (v0.1.0)
if you are changing the API, increment the major version (v1.0.0)

(back to top)

βœ… Testing

Our project implements a comprehensive testing strategy that ensures code quality and reliability. We use multiple testing approaches to cover different aspects of the application.

Test Coverage

The project maintains a high test coverage of 82.9%, demonstrating our commitment to code quality and reliability.

Test Coverage

Testing Approaches

Unit Tests

  • Framework: Testify
  • Coverage: Core business logic, use cases, and adapters
  • Command: make test or make coverage

BDD (Behavior Driven Development) Tests

We use Godog framework to implement BDD tests that ensure our application behaves correctly from a user perspective.

BDD Test Execution

Test Commands

# Run all tests
make test

# Run tests with coverage
make coverage

# Run BDD tests
make test-bdd

Test Structure

tests/
β”œβ”€β”€ features/          # BDD feature files
β”‚   └── order.feature
└── order_step_definition.go

internal/
β”œβ”€β”€ adapter/
β”‚   β”œβ”€β”€ controller/
β”‚   β”‚   β”œβ”€β”€ *_test.go
β”‚   β”œβ”€β”€ gateway/
β”‚   β”‚   β”œβ”€β”€ *_test.go
β”‚   └── presenter/
β”‚       β”œβ”€β”€ *_test.go
β”œβ”€β”€ core/
β”‚   β”œβ”€β”€ domain/
β”‚   β”‚   β”œβ”€β”€ entity/
β”‚   β”‚   β”‚   β”œβ”€β”€ *_test.go
β”‚   β”‚   └── value_object/
β”‚   β”‚       β”œβ”€β”€ *_test.go
β”‚   └── usecase/
β”‚       β”œβ”€β”€ *_test.go
└── infrastructure/
    β”œβ”€β”€ handler/
    β”‚   β”œβ”€β”€ *_test.go
    β”œβ”€β”€ database/
    β”‚   β”œβ”€β”€ *_test.go
    └── datasource/
        β”œβ”€β”€ *_test.go

Testing Best Practices

  • Mocking: Using gomock for interface mocking
  • Fixtures: Test data fixtures for consistent testing
  • Golden Files: Expected output validation
  • Test Suites: Organized test execution
  • Coverage Reports: Detailed coverage analysis

(back to top)

πŸ‘₯ Contributors

(back to top)

About

FIAP Tech Challenge 4 - 10SOAT - G19 - Fast Food API - Kitchen Service with Golang

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors 2

  •  
  •