
Table of Contents
-
About The Project
-
Getting Started
- Usage
- Examples
- Contributing
- License
- Contact
About The Project
iqhater/pkg is a small, framework-free Go library with reusable primitives for API services.
It focuses on common infrastructure pieces without adding routers, dependency injection, database layers, or application scaffolding. Use only the packages you need.
Built With
(back to top)
Getting Started
Prerequisites
Install Go and, optionally, Taskfile for local development commands.
Installation
-
Clone the repo
git clone https://github.com/iqhater/pkg.git
-
Install Go module dependencies and development tools
task install
-
Run tests
task test
-
Run the example server
task run
(back to top)
Usage
Install the package in your project:
go get github.com/iqhater/pkg
Combine middleware with the standard net/http package:
package main
import (
"net/http"
"time"
"github.com/iqhater/pkg/headers"
"github.com/iqhater/pkg/middleware"
)
func main() {
cache := middleware.NewCache("10s")
mid := middleware.Middlewares(
middleware.Recover,
middleware.RequestID,
headers.CORSHeaders(headers.CORSConfig{
AllowOrigins: []string{"http://localhost:4000"},
AllowMethods: []string{http.MethodGet, http.MethodPost, http.MethodOptions},
AllowHeaders: []string{"Accept", "Content-Type", "Authorization"},
}),
middleware.Log,
middleware.Limit(2, 5),
middleware.ContextTimeout(3*time.Second),
headers.SecureHeaders,
middleware.Compress,
cache.CacheResponse,
headers.ContentTypeHeaders("application/json"),
)
http.HandleFunc("GET /api", middleware.Bind(mid, func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"ok":true}`))
}))
http.ListenAndServe(":4000", nil)
}
Taskfile useful commands
Run all tests
task test
Run the project linter
task lint
Build the example binary
task build
Remove example binaries
task clean
(back to top)
Examples
Runnable examples are available in the examples package.
go run .\examples\middlewares\main.go
Example package overview:
middleware — recovery, request IDs, logging, rate limiting, timeouts, caching, and compression.
headers — CORS, security headers, and content type headers.
async/workerpool — generic worker pool for concurrent jobs.
async/eventbus — simple in-process event bus.
generate — random token generation.
Notes
CacheResponse caches responses by request URI and only stores successful 200 OK responses.
Compress prefers Brotli when the client sends Accept-Encoding: br, otherwise it falls back to gzip.
Compress skips WebSocket upgrade requests.
ContextTimeout cancels the request context, but handlers must explicitly check r.Context().Done() for long-running work.
RequestID stores the request ID in request context. Read it from the request passed to the downstream handler.
Limit is based on req.RemoteAddr, so it is useful for simple per-IP rate limiting.
(back to top)
Contributing
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag enhancement.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/amazing-feature)
- Commit your Changes (
git commit -m 'Add amazing feature')
- Push to the Branch (
git push origin feature/amazing-feature)
- Open a Pull Request
(back to top)
License
Distributed under the MIT License. See LICENSE for more information.
(back to top)
email - iqhater@yandex.ru
Project Link: https://github.com/iqhater/pkg
(back to top)