respond

package
v0.0.0-...-419c0c3 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KeyID    = "id"
	KeyEvent = "event"
	KeyData  = "data"
	KeyRetry = "retry"
)

Variables

View Source
var (
	ContentTypeCtxKey = &contextKey{"ContentType"}
)
View Source
var StatusCtxKey = &contextKey{"Status"}

StatusCtxKey is a context key to record a future HTTP response status code.

Functions

func Blob

func Blob(w http.ResponseWriter, r *http.Request, contentType string, data ...[]byte)

func DecodeBody

func DecodeBody[I any](r *http.Request) (*I, error)

func Escape

func Escape(src []byte) []byte

func File

func File(w http.ResponseWriter, r *http.Request, name string)

func FileFS

func FileFS(w http.ResponseWriter, r *http.Request, fsys fs.FS, name string)

func JSON

func JSON(w http.ResponseWriter, r *http.Request, data any)

func PlainText

func PlainText[S Stringer](w http.ResponseWriter, r *http.Request, data S)

func Respond

func Respond(w http.ResponseWriter, r *http.Request, data any)

func ServerEvent

func ServerEvent[T any](w http.ResponseWriter, r *http.Request, source ServerEventSource[T])

func SetContentType

func SetContentType(contentType ContentType) func(next http.Handler) http.Handler

SetContentType is a middleware that forces response Content-Type.

func Status

func Status(r *http.Request, status int)

Status sets a HTTP response status code hint into request context at any point during the request life-cycle. Before the Responder sends its response header it will check the StatusCtxKey

func UnmarshalBody

func UnmarshalBody[I any](r *http.Request, in *I) (err error)

func XML

func XML(w http.ResponseWriter, r *http.Request, data any)

Types

type ContentType

type ContentType int

ContentType is an enumeration of common HTTP content types.

const (
	ContentTypeUnknown ContentType = iota
	ContentTypePlainText
	ContentTypeHTML
	ContentTypeJSON
	ContentTypeXML
	ContentTypeForm
	ContentTypeEventStream
)

ContentTypes handled by this package.

func GetAcceptedContentType

func GetAcceptedContentType(r *http.Request) ContentType

func GetContentType

func GetContentType(s string) ContentType

func GetRequestContentType

func GetRequestContentType(r *http.Request) ContentType

GetRequestContentType is a helper function that returns ContentType based on context or request headers.

type M

type M map[string]any

func E

func E(err error, code ...string) M

type ServerEventSource

type ServerEventSource[T any] struct {
	// Retry 指定浏览器重新发起连接的时间间隔。
	//
	// 两种情况会导致浏览器重新发起连接
	//  - 时间间隔到期,
	//  - 由于网络错误等原因,导致连接出错。
	Retry int

	// 写入 Access-Control-Allow-Origin 头
	AllowOrigin string

	// 心跳间隔
	Heartbeat int

	// 数据
	Data <-chan T
	// contains filtered or unexported fields
}

func (*ServerEventSource[T]) Flush

func (se *ServerEventSource[T]) Flush()

func (*ServerEventSource[T]) Upgrade

func (se *ServerEventSource[T]) Upgrade(w http.ResponseWriter, r *http.Request)

func (*ServerEventSource[T]) Write

func (se *ServerEventSource[T]) Write(name string, line []byte)

func (*ServerEventSource[T]) WriteAnyData

func (se *ServerEventSource[T]) WriteAnyData(data ...any)

func (*ServerEventSource[T]) WriteData

func (se *ServerEventSource[T]) WriteData(data ...[]byte)

func (*ServerEventSource[T]) WriteEvent

func (se *ServerEventSource[T]) WriteEvent(event string)

func (*ServerEventSource[T]) WriteEventData

func (se *ServerEventSource[T]) WriteEventData(event string, data any)

func (*ServerEventSource[T]) WriteFull

func (se *ServerEventSource[T]) WriteFull(id, event string, data ...[]byte)

func (*ServerEventSource[T]) WriteID

func (se *ServerEventSource[T]) WriteID(id string)

type Stringer

type Stringer interface{ ~string | ~[]byte }

Jump to

Keyboard shortcuts

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