Skip to content

Repository files navigation

Users Service

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.


Description

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.

File Structure

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)

How to Build and Run

1. Prerequisites

  • Docker and Docker Compose installed on your system.
  • jq installed (used in post-request-users.sh).

2. Build and Run with Docker Compose

To build and run the service:

docker compose up --build -d

This will:

  1. Build the Go application.
  2. Start the application container (app) on port 8080.
  3. Start a PostgreSQL container (postgres) on port 5432.

3. Verify the Service

Check if the service is running:

curl http://localhost:8080/users

How to Use

1. Generate Test Users

Run the generate_users.sh script to create a file called users.json with 100 test users:

./generate_users.sh

2. Insert Users into the Database

Use the post-request-users.sh script to bulk insert the generated users into the database via the API:

./post-request-users.sh

3. Access Swagger Docs

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


API Endpoints

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

Environment Variables

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

Development Notes

Running Locally Without Docker

If you want to run the service locally without Docker:

  1. Start a PostgreSQL server locally.
  2. Set environment variables (DB_HOST, etc.).
  3. Run the Go application:
    go run cmd/server/main.go

Running Tests

Run unit tests for all components:

go test ./... -v

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages