Skip to content

Repository files navigation

⚡ microfast

A blazing-fast, production-ready HTTP server built with Go and fasthttp.

CI Go Report Card License Go Reference


Why microfast?

Feature Description
🚀 High Performance Built on fasthttp — up to 10x faster than net/http
🔒 Secure by Default Anti-panic recovery, security headers, rate limiting out of the box
📊 Observable Built-in Prometheus metrics (/metrics) and health probe (/healthz)
♻️ Graceful Shutdown Handles SIGINT/SIGTERM, waits for in-flight requests with timeout
🧩 Modular Middleware Composable chain — add logging, CORS, auth in one line
🐳 Cloud Native Tiny scratch-based Docker image, zero external dependencies at runtime
⚙️ Zero Config Works out of the box with sensible defaults, fully tunable via env vars

Quick Start

Prerequisites

  • Go 1.22+ (tested with 1.23)

Install & Run

git clone https://github.com/888zxc/microfast.git
cd microfast
make run

That's it. The server is now running on http://localhost:8080.

Try It

curl http://localhost:8080/               # Welcome page
curl http://localhost:8080/healthz        # Health check → "OK"
curl http://localhost:8080/version        # Build version (JSON)
curl "http://localhost:8080/api/echo?msg=hello"  # Echo API
curl http://localhost:8080/metrics        # Prometheus metrics

Configuration

All settings are configurable via environment variables. No config files needed:

Variable Default Description
SERVER_PORT 8080 Listen port
LIMIT_PER_SEC 20000 Max requests per second
READ_TIMEOUT 30s Request read timeout
WRITE_TIMEOUT 30s Response write timeout
IDLE_TIMEOUT 120s Keep-alive idle timeout
SHUTDOWN_TIMEOUT 30s Graceful shutdown deadline
MAX_CONNS_PER_IP 10000 Per-IP connection limit
MAX_REQS_PER_CONN 1000 Requests per connection

Example:

SERVER_PORT=9000 LIMIT_PER_SEC=50000 ./microfast-server

Project Structure

microfast/
├── cmd/server/main.go          # Entry point
├── config/                     # Environment-based configuration
├── internal/
│   ├── handler/                # Route handlers + Prometheus metrics
│   ├── middleware/              # Composable middleware chain
│   ├── limiter/                # Lock-free rate limiter (atomic CAS)
│   ├── logger/                 # Structured logging (zap)
│   └── server/                 # Server lifecycle & graceful shutdown
├── pkg/version/                # Build-time version injection
├── scripts/bench.go            # Load testing tool
├── openapi.yaml                # OpenAPI 3.0 spec
├── Makefile                    # Build, test, lint, docker commands
└── dockerfile                  # Multi-stage scratch image

Development

Build

make build                    # Build binary
make build-version            # Build with version injection

Test

make test                     # Run tests with race detector
make bench                    # Run benchmarks
make lint                     # Run golangci-lint

Docker

make docker-build             # Build Docker image
make docker-run               # Run in container

Middleware Stack

The request pipeline is fully composable:

Request → Recovery → RequestID → Logging → RateLimit → SecureHeaders → CORS → Handler

Each middleware is an independent function — add, remove, or reorder freely in internal/server/server.go.


Benchmarking

Built-in load generator:

# Start server first, then:
go run scripts/bench.go

# Or use wrk / hey:
wrk -t8 -c1000 -d10s http://localhost:8080/
hey -n 100000 -c 500 http://localhost:8080/healthz

API Reference

See openapi.yaml for the full OpenAPI 3.0 specification.
Preview it live at Swagger Editor.

Endpoint Method Description
/ GET Welcome page
/healthz GET Health check (for K8s probes)
/version GET Build version (JSON)
/api/echo GET Echo ?msg= back as JSON
/metrics GET Prometheus metrics

Contributing

Contributions are welcome! See CONTRIBUTING.md for guidelines.

Security

To report a vulnerability, see SECURITY.md.

License

MIT © 命本如此


⭐ Star this repo if you find it useful!

About

高性能服务器,通过GO语言开发

Resources

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages