Documentation
¶
Index ¶
- func ResponesFILE(c *gin.Context, filename string)
- func ResponseBytes(c *gin.Context, responder Responder, data ...[]byte)
- func ResponseBytesList(c *gin.Context, responder Responder, total int64, data ...[]byte)
- func ResponseDATA(c *gin.Context, data []byte, headers ...map[string]string)
- func ResponseJSON(c *gin.Context, responder Responder, data ...any)
- func ResponseSSE(c *gin.Context, event types.Event) error
- func ResponseTEXT(c *gin.Context, responder Responder, data ...any)
- func StreamSSE(c *gin.Context, fn func(io.Writer) bool)
- func StreamSSEWithInterval(c *gin.Context, interval time.Duration, fn func(io.Writer) bool)
- type Code
- type CodeInstance
- type Responder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ResponesFILE ¶
func ResponseBytesList ¶
func ResponseSSE ¶ added in v0.10.2
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 StreamSSE ¶ added in v0.10.2
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
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 ( CodeInvalidParam Code = 1000 + iota CodeBadRequest CodeInvalidToken CodeNeedLogin CodeNetworkTimeout CodeContextTimeout CodeTooManyRequests CodeNotFound CodeForbidden CodeAlreadyExist )
通用状态码
const ( CodeInvalidLogin Code = 2000 + iota CodeInvalidSignup CodeOldPasswordNotMatch CodeNewPasswordNotMatch CodeNotFoundQueryID CodeNotFoundRouteParam CodeNotFoundUser CodeNotFoundUserID CodeAlreadyExistsUser CodeAlreadyExistsRole CodeTooLargeFile )
业务状态码
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