Skip to content

akilapramod/EchoMarket

Repository files navigation

EchoMarket - Learning Go Microservices

A simple microservices project to learn Go, REST APIs, and PostgreSQL.

What You'll Learn

  • Building REST APIs in Go
  • Database operations with PostgreSQL
  • Layered architecture (handlers → services → repository)
  • Docker basics
  • Testing APIs

Project Structure

user-service/
├── main.go              # Entry point - starts the server
├── models/              # Data structures
│   └── user.go
├── database/            # Database connection
│   └── database.go
├── repository/          # Database queries
│   └── user_repository.go
├── services/            # Business logic
│   └── user_service.go
├── handlers/            # HTTP request handlers
│   └── user_handler.go
└── routes/              # URL routing
    └── routes.go

Quick Start

1. Start PostgreSQL

docker-compose up -d

2. Install Dependencies

cd user-service
go mod download

3. Run the Service

go run main.go

You should see:

🚀 Starting User Service...
✅ Database connected successfully
✅ Users table ready
✅ User Service running on http://localhost:8080

Test the API

Health Check

curl http://localhost:8080/health

Create a User

curl -X POST http://localhost:8080/users \
  -H "Content-Type: application/json" \
  -d '{"name":"Alice","email":"alice@example.com"}'

Response:

{
  "id": 1,
  "name": "Alice",
  "email": "alice@example.com",
  "created_at": "2025-01-28T12:00:00Z"
}

Get a User by ID

curl http://localhost:8080/users/1

Get All Users

curl http://localhost:8080/users

Understanding the Code Flow

When you create a user (POST /users):

  1. Handler (handlers/user_handler.go)

    • Receives HTTP request
    • Parses JSON
    • Calls service layer
  2. Service (services/user_service.go)

    • Validates data
    • Calls repository layer
  3. Repository (repository/user_repository.go)

    • Executes SQL query
    • Returns data
  4. Response flows back through the layers

What's Next?

Once you're comfortable with this:

  1. Add more features

    • Update user endpoint
    • Delete user endpoint
    • Search users by email
  2. Add validation

    • Email format validation
    • Duplicate email check
  3. Add tests

    • Unit tests for services
    • Integration tests for handlers
  4. Build a second service

    • Product service
    • Order service

Common Issues

"connection refused"

  • Make sure PostgreSQL is running: docker ps
  • Check if port 5432 is available

"package not found"

  • Run go mod download in user-service directory

"table already exists"

  • That's fine! The code handles this with IF NOT EXISTS

Learning Resources

License

MIT

About

A simple microservices project to learn Go, REST APIs, and PostgreSQL.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages