Skip to content

parth1899/go-user-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

User Management API

A RESTful API built in Go for managing users with name and date of birth. Age is calculated dynamically on read operations.

Features

  • Full CRUD operations for users
  • Dynamic age calculation based on date of birth
  • Pagination support for listing users
  • Input validation with custom date format checking
  • Structured logging with request ID and duration tracking
  • Type-safe database access using SQLC
  • Containerized with Docker Compose

Tech Stack

  • Go with Fiber framework
  • PostgreSQL
  • SQLC for query generation
  • go-playground/validator for validation
  • Uber Zap for logging
  • pgx PostgreSQL driver

Project Structure

/cmd/server              # Application entry point
/db/migrations           # Database schema migrations
/db/query                # SQLC query definitions
/db/sqlc                 # Generated SQLC code
/internal
  ├── dto                # Request/response DTOs
  ├── handler            # HTTP handlers
  ├── middleware         # Request ID and logging middleware
  ├── models             # Application models
  ├── repository         # Data access layer
  ├── service            # Business logic
  ├── logger             # Zap logger configuration
  └── db                 # Database connection

Setup and Run

Using Docker Compose (Recommended)

# Build and start the services
docker compose up --build

# Subsequent runs
docker compose up

The API will be available at http://localhost:3000
PostgreSQL will be available at localhost:5432

Manual Run (Development)

  1. Start PostgreSQL (e.g., via Docker):
docker run --name user-api-postgres \
  -e POSTGRES_PASSWORD=password \
  -e POSTGRES_USER=postgres \
  -e POSTGRES_DB=userdb \
  -p 5432:5432 \
  -v pgdata:/var/lib/postgresql/data \
  -d postgres:15
  1. Apply the migration:
docker exec -i user-api-postgres psql -U postgres -d userdb < db/migrations/001_create_users_table.sql
  1. Run the application:
go run cmd/server/main.go

API Endpoints

Method Endpoint Description
POST /users Create a user
GET /users List users (with pagination)
GET /users/:id Get a user by ID
PUT /users/:id Update a user
DELETE /users/:id Delete a user

Pagination

GET /users supports:

  • limit — number of results per page (default: 20, max: 100)
  • offset — number of results to skip (default: 0)

Example: /users?limit=10&offset=20

Example Requests

# Create a user
curl -X POST http://localhost:3000/users \
  -H "Content-Type: application/json" \
  -d '{"name": "Parth", "dob": "2004-05-10"}'

# Get a user by ID
curl http://localhost:3000/users/1

# List users (with pagination)
curl "http://localhost:3000/users?limit=5&offset=0"

# Update a user
curl -X PUT http://localhost:3000/users/1 \
  -H "Content-Type: application/json" \
  -d '{"name": "Parth Kalani", "dob": "2004-08-10"}'

# Delete a user
curl -X DELETE http://localhost:3000/users/1

Observability

  • Each request includes an X-Request-ID response header
  • Logs contain request method, path, status, duration, and request ID

About

A simple RESTful API using the Go Fiber web framework

Resources

Stars

Watchers

Forks

Contributors