97 lines
2.4 KiB
Markdown
97 lines
2.4 KiB
Markdown
# HWS (H Web Server) - v0.2.3
|
|
|
|
A lightweight, opinionated HTTP web server framework for Go built on top of the standard library's net/http.
|
|
|
|
## Features
|
|
|
|
- Built on Go 1.22+ routing patterns with method and path matching
|
|
- Structured error handling with customizable error pages
|
|
- Integrated logging with zerolog via hlog
|
|
- Middleware support with predictable execution order
|
|
- GZIP compression support
|
|
- Safe static file serving (prevents directory listing)
|
|
- Environment variable configuration with ConfigFromEnv
|
|
- Request timing and logging middleware
|
|
- Graceful shutdown support
|
|
- Built-in health check endpoint
|
|
|
|
## Installation
|
|
|
|
```bash
|
|
go get git.haelnorr.com/h/golib/hws
|
|
```
|
|
|
|
## Quick Start
|
|
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"git.haelnorr.com/h/golib/hws"
|
|
)
|
|
|
|
func main() {
|
|
// Load configuration from environment variables
|
|
config, _ := hws.ConfigFromEnv()
|
|
|
|
// Create server
|
|
server, _ := hws.NewServer(config)
|
|
|
|
// Define routes
|
|
routes := []hws.Route{
|
|
{
|
|
Path: "/",
|
|
Method: hws.MethodGET,
|
|
Handler: http.HandlerFunc(homeHandler),
|
|
},
|
|
{
|
|
Path: "/api/users/{id}",
|
|
Method: hws.MethodGET,
|
|
Handler: http.HandlerFunc(getUserHandler),
|
|
},
|
|
}
|
|
|
|
// Add routes and middleware
|
|
server.AddRoutes(routes...)
|
|
server.AddMiddleware()
|
|
|
|
// Start server
|
|
ctx := context.Background()
|
|
server.Start(ctx)
|
|
|
|
// Wait for server to be ready
|
|
<-server.Ready()
|
|
}
|
|
|
|
func homeHandler(w http.ResponseWriter, r *http.Request) {
|
|
w.Write([]byte("Hello, World!"))
|
|
}
|
|
|
|
func getUserHandler(w http.ResponseWriter, r *http.Request) {
|
|
id := r.PathValue("id")
|
|
w.Write([]byte("User ID: " + id))
|
|
}
|
|
```
|
|
|
|
## Documentation
|
|
|
|
For detailed documentation, see the [HWS Wiki](https://git.haelnorr.com/h/golib/wiki/HWS.md).
|
|
|
|
Additional API documentation is available at [GoDoc](https://pkg.go.dev/git.haelnorr.com/h/golib/hws).
|
|
|
|
## License
|
|
|
|
This project is licensed under the MIT License - see the LICENSE file for details.
|
|
|
|
## Contributing
|
|
|
|
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
|
|
## Related Projects
|
|
|
|
- [hwsauth](https://git.haelnorr.com/h/golib/hwsauth) - JWT authentication middleware for HWS
|
|
- [hlog](https://git.haelnorr.com/h/golib/hlog) - Structured logging with zerolog
|
|
- [jwt](https://git.haelnorr.com/h/golib/jwt) - JWT token generation and validation
|