response

package
v0.10.14 Latest Latest
Warning

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

Go to latest
Published: May 18, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bytes added in v0.10.13

func Bytes(c *gin.Context, coder types.Coder, data ...[]byte)

func BytesList added in v0.10.13

func BytesList(c *gin.Context, coder types.Coder, total int64, data ...[]byte)

func Data added in v0.10.13

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

func File added in v0.10.13

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

func JSON added in v0.10.13

func JSON(c *gin.Context, coder types.Coder, data ...any)

func SSE added in v0.10.13

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

SSE 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:

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

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)

func Text added in v0.10.13

func Text(c *gin.Context, coder types.Coder, data ...any)

Types

type Code

type Code int32

Code is a stable numeric API error code.

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

Success / failure sentinel codes.

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

General API error codes.

const (
	CodeInvalidLogin Code = 2000 + iota
	CodeInvalidSignup
	CodeOldPasswordNotMatch
	CodeNewPasswordNotMatch

	CodeNotFoundQueryID
	CodeNotFoundRouteParam
	CodeNotFoundUser
	CodeNotFoundUserID

	CodeAlreadyExistsUser
	CodeAlreadyExistsRole

	CodeTooLargeFile

	CodeAccountInactive
	CodeAccountLocked
)

Domain / business error codes.

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 is a Code with optional per-response HTTP status and message overrides. Nil pointer fields mean "use the value from Code (including customCodeValueMap / defaultCodeValueMap)".

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

Jump to

Keyboard shortcuts

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