This project is an educational example of how to build a modular, production-ready HTTP server in Go using only the standard library. It demonstrates fundamental concepts like routing, request handling, and structuring a web service application without relying on third-party frameworks like Gin or Echo.
The goal is to provide a clear and well-commented codebase that explains the "why" behind the structure, making it a great starting point for anyone looking to understand the mechanics of Go's net/http package.
The project is organized into several packages to promote modularity and separation of concerns.
HTTPGolang/
├── cmd/
│ └── server/
│ └── main.go # Main application entry point. Initializes and starts the server.
├── pkg/
│ ├── server/
│ │ └── server.go # A wrapper around the standard http.Server for easy configuration.
│ ├── router/
│ │ ├── router.go # A custom HTTP router to map requests to handlers.
│ │ └── router_test.go # Tests for the router.
│ ├── httpcontext/
│ │ └── context.go # A custom context with helper functions for handlers (e.g., sending JSON).
│ └── handlers/
│ ├── handlers.go # Application-specific business logic (API handlers).
│ └── handlers_test.go# Tests for the API handlers.
├── go.mod # Defines the Go module and its properties.
├── .gitignore # Specifies files for Git to ignore.
└── README.md # This file.- Go version 1.22 or higher.
-
Clone the repository:
git clone https://github.com/hanzalaareeb/GO-http-server.git cd HTTPGolang -
Tidy dependencies: This will ensure your go.mod file is in sync. Since there are no external dependencies, it will just verify the setup.
go mod tidy
To start the HTTP server, run the main.go file from the root of the project:
go run cmd/server/main.goThe server will start and listen on port 8080.
2024/06/07 12:00:00 Initializing router...
2024/06/07 12:00:00 Registered route: GET /health
2024/06/07 12:00:00 Registered route: GET /users
2024/06/07 12:00:00 Registered route: POST /users
2024/06/07 12:00:00 Registering application handlers...
2024/06/07 12:00:00 Server starting on port :8080...
2024/06/07 12:00:00 Application started. Press Ctrl+C to exit.To run the unit tests for all packages, execute the following command from the root of the project:
go test ./... -vThe -v flag enables verbose output, showing the status of each test.
The server exposes the following endpoints:
| Method | Path | Description | Example curl Command |
|---|---|---|---|
| GET | /health | Checks the health of the service. | curl http://localhost:8080/health |
| GET | /users | Retrieves a static list of users. | curl http://localhost:8080/users |
| POST | /users | Simulates the creation of a new user. | curl -X POST http://localhost:8080/users |
| ANY | /anything | (Non-existent) Returns a 404 Not Found response. | curl http://localhost:8080/non-existent-route |