A simple REST API built with Go, Fiber, and GORM for managing facts (questions and answers). Supports PostgreSQL and Oracle databases, containerized with Docker Compose, and includes hot reloading for development.
- RESTful API for CRUD operations on facts
- Support for PostgreSQL and Oracle databases
- Docker containerization with multi-stage builds
- Hot reloading with Air during development
- Graceful shutdown handling
- Clean architecture with handlers, models, and database layers
- Environment-based configuration
- Comprehensive error handling
- Docker and Docker Compose (for containerized development)
- Go 1.21+ (for local development)
- PostgreSQL or Oracle database
-
Clone the repository:
git clone https://github.com/vizchamz/go-rest-docker.git cd go-rest-docker -
Copy the environment template:
cp .env-sample .env
-
Update the
.envfile with your database credentials:APPLICATION_DB=postgres POSTGRES_DB_HOST=localhost POSTGRES_DB_USER=your_db_user POSTGRES_DB_PASSWORD=your_db_password POSTGRES_DB_NAME=your_db_name
-
Run with Docker Compose:
docker-compose up --build
-
Clone and setup:
git clone https://github.com/vizchamz/go-rest-docker.git cd go-rest-docker cp .env-sample .env -
Install dependencies:
go mod tidy
-
Setup local PostgreSQL database and update
.env -
Build the application:
go build -o main ./cmd
-
Run the application:
./main
Or run with hot reload during development:
air
The API will be available at http://localhost:3000.
Returns a welcome message.
Response:
"Hello, Visal Dharmasiri!"Lists all facts in the database.
Response:
[
{
"ID": 1,
"CreatedAt": "2026-01-06T06:27:03.025298+05:30",
"UpdatedAt": "2026-01-06T06:27:03.025298+05:30",
"DeletedAt": null,
"question": "What is the capital of France?",
"answer": "Paris"
}
]Creates a new fact.
Request Body:
{
"question": "What is the capital of France?",
"answer": "Paris"
}Response:
{
"ID": 1,
"CreatedAt": "2026-01-06T06:27:03.025298+05:30",
"UpdatedAt": "2026-01-06T06:27:03.025298+05:30",
"DeletedAt": null,
"question": "What is the capital of France?",
"answer": "Paris"
}-
Install Go dependencies:
go mod tidy
-
Setup database:
- For PostgreSQL: Create a database and user
- Update
.envwith your database credentials
-
Install Air for hot reloading:
go install github.com/cosmtrek/air@latest
-
Run development server:
air
The project includes Docker support for both development and production:
- Development:
docker-compose up --build - Production: Use the multi-stage Dockerfile for optimized builds
.
├── .air.toml # Air hot reload configuration
├── .dockerignore # Docker ignore file
├── .env-sample # Environment variables template
├── Dockerfile # Multi-stage Docker build
├── docker-compose.yml # Docker Compose configuration
├── cmd/
│ ├── main.go # Application entry point with graceful shutdown
│ └── routes.go # Route definitions
├── database/
│ └── database.go # Database connection and GORM setup
├── handlers/
│ └── facts.go # HTTP handlers for facts API
├── models/
│ └── models.go # GORM data models
├── go.mod # Go module dependencies
├── go.sum # Go module checksums
└── README.md # This file
| Variable | Description | Default |
|---|---|---|
APPLICATION_DB |
Database type (postgres or oracle) |
postgres |
| Variable | Description | Example |
|---|---|---|
POSTGRES_DB_HOST |
Database host | localhost |
POSTGRES_DB_USER |
Database username | myuser |
POSTGRES_DB_PASSWORD |
Database password | mypass |
POSTGRES_DB_NAME |
Database name | mydb |
| Variable | Description | Example |
|---|---|---|
ORACLE_DB_HOST |
Database host | localhost |
ORACLE_DB_PORT |
Database port | 1521 |
ORACLE_DB_SID |
Database SID | ORCL |
ORACLE_DB_USER |
Database username | myuser |
ORACLE_DB_PASSWORD |
Database password | mypass |
curl -X POST http://localhost:3000/fact \
-H "Content-Type: application/json" \
-d '{"question": "What is 2+2?", "answer": "4"}'curl http://localhost:3000/factcurl http://localhost:3000/For production deployment on a server without Docker:
-
Build the optimized binary:
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main ./cmd
-
Run the application:
./main
The Dockerfile includes multi-stage builds for optimized production images:
# Build production image
docker build -t go-rest-api .
# Run production container
docker run -p 3000:3000 --env-file .env go-rest-apiFor production deployment with database:
docker-compose -f docker-compose.yml up -d