A RESTful API service for managing users, built with Go and PostgreSQL. This service provides endpoints for creating, reading, updating, and deleting users. It also includes Swagger documentation for easy API exploration and testing.
The Users Service is designed to manage user data efficiently. It leverages:
- Go: A high-performance backend framework.
- PostgreSQL: A robust relational database for storing user data.
- Docker Compose: Simplifies containerized deployment of the application and database.
- Swagger: Provides interactive API documentation.
users-service/
├── cmd/
│ └── server/
│ └── main.go # Main entry point of the application
├── docs/ # Swagger documentation (auto-generated)
│ ├── docs.go
│ ├── swagger.json
│ └── swagger.yaml
├── internal/
│ ├── controller/
│ │ ├── user_controller.go # Handles HTTP requests for user operations
│ │ └── user_controller_test.go # Unit tests for user_controller.go
│ ├── model/
│ │ └── user.go # Defines the User model
│ ├── repository/
│ │ ├── user_repository.go # Interacts with the database
│ │ └── user_repository_test.go # Unit tests for user_repository.go
│ ├── service/
│ ├── user_service.go # Business logic for user operations
│ └── user_service_test.go # Unit tests for user_service.go
├── pkg/
│ └── database/
│ └── postgres.go # Database connection and schema setup
├── generate_users.sh # Script to generate a JSON file of test users
├── post-request-users.sh # Script to bulk insert users into the database via API
├── docker-compose.yml # Docker Compose configuration for app and PostgreSQL
├── Dockerfile # Dockerfile to build the Go application
├── go.mod # Go module dependencies
├── go.sum # Go module checksums
└── README.md # Project documentation (this file)
- Docker and Docker Compose installed on your system.
jqinstalled (used inpost-request-users.sh).
To build and run the service:
docker compose up --build -dThis will:
- Build the Go application.
- Start the application container (
app) on port8080. - Start a PostgreSQL container (
postgres) on port5432.
Check if the service is running:
curl http://localhost:8080/usersRun the generate_users.sh script to create a file called users.json with 100 test users:
./generate_users.shUse the post-request-users.sh script to bulk insert the generated users into the database via the API:
./post-request-users.shSwagger UI is available at:
http://localhost:8080/swagger/index.html
You can explore all API endpoints, view request/response schemas, and test the API directly from your browser.
| Method | Endpoint | Description |
|---|---|---|
| GET | /users |
List all users |
| POST | /users |
Create a new user |
| PUT | /users/{id} |
Update an existing user |
| DELETE | /users/{id} |
Delete a user |
The following environment variables are used in docker-compose.yml:
| Variable | Default Value | Description |
|---|---|---|
DB_HOST |
postgres |
Database host |
DB_PORT |
5432 |
Database port |
DB_USER |
postgres |
Database username |
DB_PASSWORD |
postgres |
Database password |
DB_NAME |
users_db |
Database name |
If you want to run the service locally without Docker:
- Start a PostgreSQL server locally.
- Set environment variables (
DB_HOST, etc.). - Run the Go application:
go run cmd/server/main.go
Run unit tests for all components:
go test ./... -v