pkg

module
v0.0.0-...-3d2ea00 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 3, 2026 License: MIT

README

MIT License Tests


iqhater/pkg

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
  1. About The Project
  2. Getting Started
  3. Usage
  4. Examples
  5. Contributing
  6. License
  7. 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

  • Go

(back to top)

Getting Started

Prerequisites

Install Go and, optionally, Taskfile for local development commands.

Installation

  1. Clone the repo

    git clone https://github.com/iqhater/pkg.git
    
  2. Install Go module dependencies and development tools

    task install
    
  3. Run tests

    task test
    
  4. 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.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/amazing-feature)
  3. Commit your Changes (git commit -m 'Add amazing feature')
  4. Push to the Branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

(back to top)

License

Distributed under the MIT License. See LICENSE for more information.

(back to top)

Contact

email - iqhater@yandex.ru

Project Link: https://github.com/iqhater/pkg

(back to top)

Directories

Path Synopsis
async
eventbus command
headers command
middlewares command
random_token command
workerpool command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL