response

package
v0.10.2 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ResponesFILE

func ResponesFILE(c *gin.Context, filename string)

func ResponseBytes

func ResponseBytes(c *gin.Context, responder Responder, data ...[]byte)

func ResponseBytesList

func ResponseBytesList(c *gin.Context, responder Responder, total int64, data ...[]byte)

func ResponseDATA

func ResponseDATA(c *gin.Context, data []byte, headers ...map[string]string)

func ResponseJSON

func ResponseJSON(c *gin.Context, responder Responder, data ...any)

func ResponseSSE added in v0.10.2

func ResponseSSE(c *gin.Context, event types.Event) error

ResponseSSE sends a Server-Sent Events (SSE) response. This function sets the appropriate headers for SSE and writes the event to the response.

Note: This function sends a single event, not a stream. If you need to send a [DONE] marker after this event (e.g., for AI chat completions), you should use sse.EncodeDone() or call SendSSEDone() if available in your context.

Parameters:

  • c: Gin context
  • event: SSE event to send

Example:

ResponseSSE(c, types.Event{
    Event: "message",
    Data:  "Hello, World!",
})

func ResponseTEXT

func ResponseTEXT(c *gin.Context, responder Responder, data ...any)

func StreamSSE added in v0.10.2

func StreamSSE(c *gin.Context, fn func(io.Writer) bool)

StreamSSE starts a Server-Sent Events stream. The provided function will be called repeatedly until it returns false. The stream will automatically stop if:

  • The function returns false
  • The request context is canceled (timeout, client disconnect, etc.)
  • An error occurs while writing to the client

Note: This function does NOT automatically send a [DONE] marker when the stream ends. If your protocol requires a [DONE] marker (e.g., AI chat completions), you must manually call sse.EncodeDone(c.Writer) after StreamSSE() returns.

Parameters:

  • c: Gin context
  • fn: Function that sends events. Returns false to stop streaming. The function receives the writer and should check context cancellation if needed.

Example:

StreamSSE(c, func(w io.Writer) bool {
    sse.Encode(w, types.Event{
        Event: "message",
        Data:  "Hello",
    })
    return true // Continue streaming
})
// Send [DONE] marker if required by your protocol
sse.EncodeDone(c.Writer)

func StreamSSEWithInterval added in v0.10.2

func StreamSSEWithInterval(c *gin.Context, interval time.Duration, fn func(io.Writer) bool)

StreamSSEWithInterval starts a Server-Sent Events stream with a fixed interval between events. The provided function will be called repeatedly at the specified interval until it returns false. The stream will automatically stop if:

  • The function returns false
  • The request context is canceled (timeout, client disconnect, etc.)
  • An error occurs while writing to the client

Note: This function does NOT automatically send a [DONE] marker when the stream ends. If your protocol requires a [DONE] marker (e.g., AI chat completions), you must manually call sse.EncodeDone(c.Writer) after StreamSSEWithInterval() returns.

Parameters:

  • c: Gin context
  • interval: Time interval between events
  • fn: Function that sends events. Returns false to stop streaming. The function receives the writer and should check context cancellation if needed.

Example:

StreamSSEWithInterval(c, 1*time.Second, func(w io.Writer) bool {
    sse.Encode(w, sse.Event{
        Event: "message",
        Data:  time.Now().String(),
    })
    return true // Continue streaming
})
// Send [DONE] marker if required by your protocol
sse.EncodeDone(c.Writer)

Types

type Code

type Code int32
const (
	CodeSuccess Code = 0
	CodeFailure Code = -1
)

成功处理和失败处理状态码

const (
	CodeInvalidParam Code = 1000 + iota
	CodeBadRequest
	CodeInvalidToken
	CodeNeedLogin
	CodeUnauthorized
	CodeNetworkTimeout
	CodeContextTimeout
	CodeTooManyRequests
	CodeNotFound
	CodeForbidden
	CodeAlreadyExist
)

通用状态码

const (
	CodeInvalidLogin Code = 2000 + iota
	CodeInvalidSignup
	CodeOldPasswordNotMatch
	CodeNewPasswordNotMatch

	CodeNotFoundQueryID
	CodeNotFoundRouteParam
	CodeNotFoundUser
	CodeNotFoundUserID

	CodeAlreadyExistsUser
	CodeAlreadyExistsRole

	CodeTooLargeFile
)

业务状态码

func NewCode

func NewCode(code Code, status int, msg string) Code

func (Code) Code

func (r Code) Code() int

func (Code) Msg

func (r Code) Msg() string

func (Code) Status

func (r Code) Status() int

func (Code) WithErr

func (r Code) WithErr(err error) CodeInstance

func (Code) WithMsg

func (r Code) WithMsg(msg string) CodeInstance

func (Code) WithStatus

func (r Code) WithStatus(status int) CodeInstance

type CodeInstance

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

CodeInstance 表示一个错误码实例,包含自定义的状态和消息

func (CodeInstance) Code

func (ci CodeInstance) Code() int

func (CodeInstance) Msg

func (ci CodeInstance) Msg() string

func (CodeInstance) Status

func (ci CodeInstance) Status() int

func (CodeInstance) WithErr

func (ci CodeInstance) WithErr(err error) CodeInstance

func (CodeInstance) WithMsg

func (ci CodeInstance) WithMsg(msg string) CodeInstance

func (CodeInstance) WithStatus

func (ci CodeInstance) WithStatus(status int) CodeInstance

type Responder

type Responder interface {
	Msg() string
	Status() int
	Code() int
}

Responder 响应接口,统一处理 Code 和 CodeInstance

Jump to

Keyboard shortcuts

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