2 Star 1 Fork 0

mrwhen/gone

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
one_response.go 3.29 KB
Copy Edit Raw Blame History
哎呦-不错的猪公 authored 2022-12-24 11:40 +08:00 . fix: 修改module name
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
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/mrwhen/gone.git
git@gitee.com:mrwhen/gone.git
mrwhen
gone
gone
v0.0.1-alpha

Search