infra

package
v1.67.2 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2026 License: MIT Imports: 4 Imported by: 1

README

Package infra

Пакет infra предоставляет обертку над http-сервером из стандартной библиотеки net/http.

Types

Server

Структура-обертка http-сервера.

Methods:

NewServer() *Server

Создать экземпляр http-сервера.

(s *Server) Handle(pattern string, handler http.Handler)

Зарегистрировать обработчик.

(s *Server) HandleFunc(pattern string, handler http.HandlerFunc)

Зарегистрировать функцию-обработчик.

(s *Server) ListenAndServe(address string) error

Запустить http-сервер. Обрабатывает http.ErrServerClosed как нормальное завершение.

(s *Server) Shutdown() error

Остановить работу http-сервера.

Usage

Default usage flow
package main

import (
	"log"
	"net/http"

	"github.com/txix-open/isp-kit/infra"
	"github.com/txix-open/isp-kit/shutdown"
)

func main() {
	srv := infra.NewServer()
	srv.HandleFunc("/foo", func(w http.ResponseWriter, _ *http.Request) {
		w.WriteHeader(http.StatusOK)
	})

	shutdown.On(func() { /* waiting for SIGINT & SIGTERM signals */
		log.Println("shutting down...")
		srv.Shutdown()
		log.Println("shutdown completed")
	})

	err := srv.ListenAndServe(":8080")
	if err != nil {
		log.Fatal(err)
	}
}

Documentation

Overview

Package infra provides a wrapper around the standard library's net/http HTTP server with graceful shutdown support.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is a wrapper around http.Server that simplifies HTTP server setup and provides convenient methods for route registration and graceful shutdown.

func NewServer

func NewServer() *Server

NewServer creates a new HTTP server instance.

func (*Server) Handle

func (s *Server) Handle(pattern string, handler http.Handler)

Handle registers an HTTP handler for the given pattern.

func (*Server) HandleFunc

func (s *Server) HandleFunc(pattern string, handler http.HandlerFunc)

HandleFunc registers an HTTP handler function for the given pattern.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe(address string) error

ListenAndServe starts the HTTP server on the specified address. It treats http.ErrServerClosed as a successful shutdown and returns nil. Returns an error wrapped with context if the server fails to start.

func (*Server) Shutdown

func (s *Server) Shutdown() error

Shutdown gracefully stops the HTTP server by closing all idle connections and waiting for outstanding requests to complete. It uses a background context with no timeout, so callers should implement their own timeout logic if needed.

Directories

Path Synopsis
Package pprof provides utilities for integrating Go's pprof profiling tools with an HTTP server.
Package pprof provides utilities for integrating Go's pprof profiling tools with an HTTP server.

Jump to

Keyboard shortcuts

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