Fetch the repository succeeded.
package gin
import (
"encoding/xml"
"fmt"
"html/template"
"net/http"
"net/url"
"gitee.com/mrwhen/gone/framework/gin/internal/json"
)
/*
return IResponse for pipeline call
*/
// IResponse interface
type IResponse interface {
// json output
IJson(obj interface{}) IResponse
// jsonp outpit
IJsonp(obj interface{}) IResponse
// xml output
IXml(obj interface{}) IResponse
// html output
IHtml(template string, obj interface{}) IResponse
// text output
IText(format string, obj ...interface{}) IResponse
// redirect
IRedirect(path string) IResponse
// set response header
ISetHeader(key string, val string) IResponse
// set response cookie
ISetCookie(key string, val string, maxAge int, path, domain string, secure, httpOnly bool) IResponse
// set response code
ISetStatus(code int) IResponse
// set 200 status code
ISetOkStatus() IResponse
}
func (ctx *Context) ISetHeader(key string, val string) IResponse {
ctx.Writer.Header().Add(key, val)
return ctx
}
func (ctx *Context) ISetStatus(code int) IResponse {
ctx.Writer.WriteHeader(code)
return ctx
}
func (ctx *Context) ISetOkStatus() IResponse {
ctx.Writer.WriteHeader(http.StatusOK)
return ctx
}
func (ctx *Context) IJson(obj interface{}) IResponse {
byt, err := json.Marshal(obj)
if err != nil {
return ctx.ISetStatus(http.StatusInternalServerError)
}
ctx.Header("Content-Type", "application/json")
ctx.Writer.Write(byt)
return ctx
}
func (ctx *Context) IJsonp(obj interface{}) IResponse {
callbackFunc, _ := ctx.DefaultQueryString("callback", "callback_function")
ctx.Header("Content-Type", "application/javascript")
// 进行下字符转义,防止XSS攻击
callback := template.JSEscapeString(callbackFunc)
// 输出函数名
_, err := ctx.Writer.Write([]byte(callback))
if err != nil {
return ctx
}
_, err = ctx.Writer.Write([]byte("("))
if err != nil {
return ctx
}
ret, err := json.Marshal(obj)
if err != nil {
return ctx
}
_, err = ctx.Writer.Write(ret)
if err != nil {
return ctx
}
_, err = ctx.Writer.Write([]byte(")"))
if err != nil {
return ctx
}
return ctx
}
func (ctx *Context) IXml(obj interface{}) IResponse {
byt, err := xml.Marshal(obj)
if err != nil {
ctx.Status(http.StatusInternalServerError)
}
ctx.Header("Content-Type", "application/xml")
ctx.Writer.Write(byt)
return ctx
}
func (ctx *Context) IHtml(file string, obj interface{}) IResponse {
t, err := template.New("output").ParseFiles(file)
if err != nil {
return ctx
}
if err := t.Execute(ctx.Writer, obj); err != nil {
return ctx
}
ctx.Header("Content-Type", "application/html")
return ctx
}
func (ctx *Context) IText(format string, values ...interface{}) IResponse {
out := fmt.Sprintf(format, values...)
ctx.Header("Content-Type", "applocation/text")
ctx.Writer.Write([]byte(out))
return ctx
}
func (ctx *Context) IRedirect(path string) IResponse {
http.Redirect(ctx.Writer, ctx.Request, path, http.StatusMovedPermanently)
return ctx
}
func (ctx *Context) ISetCookie(key string, val string, maxAge int, path, domain string, secure, httpOnly bool) IResponse {
if path == "" {
path = "/"
}
http.SetCookie(ctx.Writer, &http.Cookie{
Name: key,
Value: url.QueryEscape(val),
MaxAge: maxAge,
Path: path,
Domain: domain,
SameSite: 1,
Secure: secure,
HttpOnly: httpOnly,
})
return ctx
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。