Skip to content

hrshtmlng/go-http-server

Repository files navigation

Go HTTP Service

A production-style HTTP service written in Go, demonstrating idiomatic project structure, middleware, observability, and graceful shutdown.

This project is designed as a learning and reference implementation for building cloud-native backend services.


✨ Features

  • ⚡ HTTP server with routing
  • 🧩 Middleware chain (logging + request ID)
  • 📊 Metrics endpoint
  • 🪵 Structured logging
  • 🧵 Request tracing via context
  • 🐳 Docker support
  • 🛑 Graceful shutdown
  • 🗂️ Idiomatic Go project layout

🏗️ Project Structure

.
├── cmd/server            # Application entrypoint
│   └── main.go
│
├── internal
│   ├── http              # HTTP layer (router, middleware, handlers)
│   ├── observability     # Logging, metrics, request IDs
│   └── config            # Configuration management
│
├── Dockerfile
├── go.mod
└── README.md

🚀 Getting Started

Prerequisites

  • Go ≥ 1.25
  • (Optional) Docker

Run locally

go run ./cmd/server

Server starts on:

http://localhost:8080

📡 Endpoints

Health Check

GET /health

Response:

200 OK

Metrics

GET /metrics

Example output:

requests_total 5
uptime_seconds 42

⚙️ Configuration

The server reads configuration from environment variables.

Variable Default Description
PORT 8080 Server port

Example:

PORT=9090 go run ./cmd/server

🐳 Run with Docker

Build image:

docker build -t go-http-server .

Run container:

docker run -p 8080:8080 go-http-server

🧠 Observability

This service implements the three pillars of observability:

  • Logs → structured request logs
  • Tracing → request IDs
  • Metrics/metrics endpoint

🛑 Graceful Shutdown

The server listens for system signals (SIGINT, SIGTERM) and shuts down gracefully:

  • stops accepting new requests
  • allows in-flight requests to finish
  • exits cleanly

🧪 Development

Run the server:

go run ./cmd/server

Format code:

go fmt ./...

📚 Learning Goals

This project demonstrates:

  • Idiomatic Go project structure
  • Middleware design
  • Context propagation
  • Service lifecycle management
  • Observability patterns

🤝 Contributing

Contributions are welcome!
Feel free to open issues or submit pull requests.


📄 License

MIT License

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors