http

package
v1.60.1 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2025 License: MIT Imports: 8 Imported by: 2

README

Package http

Пакет http предоставляет инструменты для создания и управления HTTP-сервером с поддержкой middleware, обработки запросов и гибкой настройки. Интегрируется с системой логирования и обработки ошибок.

Types

Server

Структура Server управляет жизненным циклом HTTP-сервера, включая запуск, остановку и динамическое обновление обработчиков.

Methods:

NewServer(logger log.Logger, opts ...ServerOption) *Server

Конструктор сервера. Дополнительно принимает опции:

  • WithServer(server *http.Server) ServerOption – использование объекта предварительно настроенного HTTP-сервера из стандартной библиотеки net/http.
(s *Server) Upgrade(handler http.Handler)

Атомарно заменить текущий обработчик запросов на новый.

(s *Server) ListenAndServe(address string) error

Запустить сервер на указанном адресе.

(s *Server) Serve(listener net.Listener) error

Запустить сервер на существующем listener.

(s *Server) Shutdown(ctx context.Context) error

Остановить сервер, завершив все активные соединения.

Usage

Default usage flow
package main

import (
	"context"
	"log"
	"net/http"

	http2 "github.com/txix-open/isp-kit/http"
	log2 "github.com/txix-open/isp-kit/log"
	"github.com/txix-open/isp-kit/shutdown"
)

func main() {
	logger, err := log2.New()
	if err != nil {
		log.Fatal(err)
	}

	srv := http2.NewServer(logger)
	mux := http.NewServeMux()
	mux.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
		_, _ = w.Write([]byte("hello world!!"))
	})

	srv.Upgrade(mux)

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HandlerFunc

type HandlerFunc func(ctx context.Context, w http.ResponseWriter, r *http.Request) error

type Middleware

type Middleware func(next HandlerFunc) HandlerFunc

type Server

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

func NewServer

func NewServer(logger log.Logger, opts ...ServerOption) *Server

nolint:mnd

func (*Server) ListenAndServe

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

func (*Server) Serve

func (s *Server) Serve(listener net.Listener) error

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

func (*Server) Upgrade

func (s *Server) Upgrade(handler http.Handler)

type ServerOption

type ServerOption func(*Server)

func WithServer

func WithServer(server *http.Server) ServerOption

Directories

Path Synopsis
bench module

Jump to

Keyboard shortcuts

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