httplib

package
v0.0.0-...-2932577 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// RequestLogger ключ контекста для логгера.
	RequestLogger ctxKey = "logger"
)

Variables

This section is empty.

Functions

func CreateHandler

func CreateHandler(h Handler, l *util.Logger) http.HandlerFunc

CreateHandler создает обертку, которая преобразует переданный обработчик в стандартный http.Handler

func CreateWSHandler

func CreateWSHandler(h WSHandler, l *util.Logger) http.HandlerFunc

CreateWSHandler создает обертку, которая преобразует переданный обработчик в стандартный http.Handler

func NewErrorBody

func NewErrorBody(c string, msg string) []byte

NewErrorBody создает новое тело ответа, содержащего ошибку

func StartServer

func StartServer(h http.Handler, cfg *HTTPConfig, logger *util.Logger, stopChannel <-chan struct{})

StartServer запускает http сервер с обработчиком h.

Types

type ContentType

type ContentType string

ContentType тип возвращаемого значения

const (
	ContentTypeRaw  ContentType = "application/octet-stream"
	ContentTypeJSON ContentType = "application/json"
	ContentTypeHTML ContentType = "text/html"
)

Возможные типы ContentType

type ErrorBody

type ErrorBody struct {
	Code    string `json:"code"`
	Message string `json:"message"`
}

ErrorBody тело ответа, при возникновении ошибки.

type HTTPConfig

type HTTPConfig struct {
	Host string `yaml:"host"`
	Port int    `yaml:"port"`
}

HTTPConfig настройки для работы встроенного http сервера.

func NewtHTTPConfig

func NewtHTTPConfig() *HTTPConfig

NewtHTTPConfig создает HTTPConfig с настройками по-умолчанию.

func (*HTTPConfig) GetAddr

func (c *HTTPConfig) GetAddr() string

GetAddr возвращает адрес в виде host:port

type Handler

type Handler func(r *http.Request) (*Response, error)

Handler обработчик запроса. err в данном случае означает системную ошибку.

type Response

type Response struct {
	StatusCode  int
	Body        []byte
	ContentType ContentType
}

Response представляет ответ обработчика.

func NewBadRequestResponse

func NewBadRequestResponse(body []byte) *Response

NewBadRequestResponse возвращает Response с кодом 400 Bad Request

func NewConflictResponse

func NewConflictResponse(body []byte) *Response

NewConflictResponse возвращает Response с кодом 409 Conflict

func NewInternalErrorResponse

func NewInternalErrorResponse(body []byte) *Response

NewInternalErrorResponse возвращает Response с кодом 500 Internal Server Error

func NewNotFoundResponse

func NewNotFoundResponse(body []byte) *Response

NewNotFoundResponse возвращает Response с кодом 404 Not Found

func NewOKResponse

func NewOKResponse(body []byte, t ContentType) *Response

NewOKResponse возвращает 200 OK, если body ненулевое и 204 No Content, если body нулевое.

func (*Response) WriteTo

func (r *Response) WriteTo(w http.ResponseWriter) error

WriteTo записывает response в w

type WSHandler

type WSHandler func(w http.ResponseWriter, r *http.Request) error

WSHandler обработчик на открытие вебсокета. err в данном случае означает системную ошибку.

Jump to

Keyboard shortcuts

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