A lightweight collection of reusable Go packages for HTTP services, middleware, background processing, and simple utilities.
Explore the docs »
View Project
·
Report Bug
·
Request Feature
Table of Contents
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.
Install Go and, optionally, Taskfile for local development commands.
- Go
- Taskfile (recommended)
- golangci-lint (optional)
-
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
Install the package in your project:
go get github.com/iqhater/pkgCombine 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)
}Run all tests
task testRun the project linter
task lintBuild the example binary
task buildRemove example binaries
task cleanRunnable examples are available in the examples package.
go run .\examples\middlewares\main.goExample 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.
CacheResponsecaches responses by request URI and only stores successful200 OKresponses.Compressprefers Brotli when the client sendsAccept-Encoding: br, otherwise it falls back to gzip.Compressskips WebSocket upgrade requests.ContextTimeoutcancels the request context, but handlers must explicitly checkr.Context().Done()for long-running work.RequestIDstores the request ID in request context. Read it from the request passed to the downstream handler.Limitis based onreq.RemoteAddr, so it is useful for simple per-IP rate limiting.
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
Distributed under the MIT License. See LICENSE for more information.
email - iqhater@yandex.ru
Project Link: https://github.com/iqhater/pkg