This is a simple API Gateway for routing API requests from a third-party service to a local service. It also converts the third-party API to a new API endpoint for the local service so that the local service can public API endpoints. The API Gateway is built using Golang Language and uses a PostgreSQL database to store logs of the API requests.
- Client: Sends HTTP requests to the API Gateway.
- API Gateway: Routes requests to either the third-party API or the local service.
- Third-Party API: External service that the API Gateway interacts with (e.g., Goong API).
- Database: PostgreSQL database used to store logs and other data.
3rd-party-gateway/
├── config/
│ ├── postgres.go
│ ├── api_key.go
├── database/
│ ├── docker_compose.yml
│ ├── logs.sql
├── middleware/
│ ├── proxies.go
├── models/
│ ├── models.go
├── services/
│ ├── services_a.go
├── .env
├── .gitignore
├── go.mod
├── main.go
├── README.md
config/
: Contains configuration files for the project.config/postgres.go
: Contains configuration for the PostgreSQL database.config/api_key.go
: Contains API key configuration for the project.database/
: Contains database configuration files.database/docker_compose.yml
: Contains Docker Compose configuration for the PostgreSQL database.database/logs.sql
: Contains SQL queries for creating tables in the PostgreSQL database.middleware/
: Contains middleware functions for the project.middleware/proxies.go
: Contains proxy middleware functions for the project.models/
: Contains models for the project.models/models.go
: Contains models for the project.services/
: Contains services for the project.services/services_a.go
: Contains service_a functions for the project..env
: Contains environment variables for the project..gitignore
: Contains files and directories to ignore.go.mod
: Contains dependencies for the project.main.go
: Contains the main function for the project.README.md
: Contains information about the project.
- Requires Go v1.23+ to run.
- Requires Docker v20.10.7+ to run.
- Requires Docker Compose v1.29.2+ to run.
- Requires PostgreSQL v13.3+ to run.
- Requires Postman v8.10.0+ to run.
-
Clone the repository:
git clone -b medium git@github.com:ngxvu/3rd-party-gateway
-
Change into the project directory:
cd 3rd-party-gateway
-
Create a
.env
file in the project root directory and add the following environment variables:DB_USER=postgres DB_PASSWORD=postgres DB_NAME=api_gateway DB_HOST=localhost DB_PORT=5432 PORT=8081
-
Start the PostgreSQL database using Docker Compose:
docker-compose -f database/docker_compose.yml up -d
-
Create the tables in the PostgreSQL database:
psql -h localhost -U postgres -d api_gateway -a -f database/logs.sql
-
Run the following command to start the API Gateway:
go run main.go
The API Gateway will start running on http://localhost:8081.
7. You can now make requests to the API Gateway using the following endpoints:
- GET /api/v1/service_a/endpoint-1
- GET /api/v1/service_a/endpoint-2
This is just an example of how to use the API Gateway. You can add more services and endpoints as needed.# 3rd-party-gateway