124 lines
2.9 KiB
Go
124 lines
2.9 KiB
Go
package framework
|
|
|
|
import (
|
|
"encoding/json"
|
|
"encoding/xml"
|
|
"fmt"
|
|
"html/template"
|
|
"net/http"
|
|
"net/url"
|
|
)
|
|
|
|
type IResponse interface {
|
|
WriteJSON(status int, obj any) IResponse
|
|
WriteXML(status int, obj any) IResponse
|
|
WriteHTML(status int, filepath string, obj any) IResponse
|
|
WriteText(format string, values ...any) IResponse
|
|
|
|
Redirect(path string) IResponse
|
|
SetHeader(key string, val string) IResponse
|
|
SetCookie(
|
|
key string,
|
|
val string,
|
|
maxAge int,
|
|
path, domain string,
|
|
secure, httpOnly bool,
|
|
) IResponse
|
|
SetStatus(code int) IResponse
|
|
|
|
// set 200
|
|
SetOkStatus() IResponse
|
|
}
|
|
|
|
func (ctx *Context) WriteJSON(status int, obj any) IResponse {
|
|
// There is a timeout, some error message data must have already been
|
|
// written to the output. Stop writing anything into the responseWriter.
|
|
if ctx.HasTimeout() {
|
|
return nil
|
|
}
|
|
data, err := json.Marshal(obj)
|
|
if err != nil {
|
|
return ctx.SetStatus(http.StatusInternalServerError)
|
|
}
|
|
ctx.responseWriter.Header().Set("Content-type", "application/json")
|
|
ctx.responseWriter.WriteHeader(status)
|
|
_, _ = ctx.responseWriter.Write(data)
|
|
return ctx
|
|
}
|
|
|
|
func (ctx *Context) WriteXML(status int, obj any) IResponse {
|
|
// There is a timeout, some error message data must have already been
|
|
// written to the output. Stop writing anything into the responseWriter.
|
|
if ctx.HasTimeout() {
|
|
return nil
|
|
}
|
|
data, err := xml.Marshal(obj)
|
|
if err != nil {
|
|
return ctx.SetStatus(http.StatusInternalServerError)
|
|
}
|
|
ctx.responseWriter.Header().Set("Content-type", "application/json")
|
|
ctx.responseWriter.WriteHeader(status)
|
|
_, _ = ctx.responseWriter.Write(data)
|
|
return ctx
|
|
}
|
|
|
|
func (ctx *Context) WriteHTML(status int, filepath string, obj any) IResponse {
|
|
ctx.SetHeader("Content-Type", "application/html")
|
|
t, _ := template.New("output").ParseFiles(filepath)
|
|
t.Execute(ctx.responseWriter, obj)
|
|
|
|
return ctx
|
|
}
|
|
|
|
func (ctx *Context) WriteText(format string, values ...any) IResponse {
|
|
out := fmt.Sprintf(format, values...)
|
|
ctx.SetHeader("Content-Type", "application/text")
|
|
ctx.responseWriter.Write([]byte(out))
|
|
return ctx
|
|
}
|
|
|
|
func (ctx *Context) Redirect(path string) IResponse {
|
|
http.Redirect(ctx.responseWriter, ctx.request, path, http.StatusTemporaryRedirect)
|
|
return ctx
|
|
}
|
|
|
|
func (ctx *Context) SetHeader(key string, val string) IResponse {
|
|
ctx.responseWriter.Header().Add(key, val)
|
|
return ctx
|
|
}
|
|
|
|
func (ctx *Context) SetCookie(
|
|
key string,
|
|
val string,
|
|
maxAge int,
|
|
path, domain string,
|
|
secure, httpOnly bool,
|
|
) IResponse {
|
|
if path == "" {
|
|
path = "/"
|
|
}
|
|
|
|
http.SetCookie(ctx.responseWriter, &http.Cookie{
|
|
Name: key,
|
|
Value: url.QueryEscape(val),
|
|
MaxAge: maxAge,
|
|
Path: path,
|
|
Domain: domain,
|
|
SameSite: http.SameSiteDefaultMode,
|
|
Secure: secure,
|
|
HttpOnly: httpOnly,
|
|
})
|
|
return ctx
|
|
}
|
|
|
|
func (ctx *Context) SetStatus(code int) IResponse {
|
|
ctx.responseWriter.WriteHeader(code)
|
|
return ctx
|
|
}
|
|
|
|
// set 200
|
|
func (ctx *Context) SetOkStatus() IResponse {
|
|
ctx.responseWriter.WriteHeader(http.StatusOK)
|
|
return ctx
|
|
}
|