AFAIRE
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.8 KiB

/*
------------------------------------------------------------------------------------------------------------------------
####### kong ####### Copyright (c) 2021-2022 losyme ################################################ MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package context
import (
"encoding/json"
"net/http"
)
const _jsonContentType = "application/json; charset=utf-8"
type MSI map[string]interface{}
type RendererType string
const RendererTypeJSON RendererType = "JSON"
func (c *Context) RenderStatus(status int) {
c.Status = status
c.Response.WriteHeader(status)
}
func (c *Context) RenderStatusInternalServerError() {
c.RenderStatus(http.StatusInternalServerError)
}
func (c *Context) RenderStatusMethodNotAllowed() {
c.RenderStatus(http.StatusMethodNotAllowed)
}
func (c *Context) RenderStatusNoContent() {
c.RenderStatus(http.StatusNoContent)
}
func (c *Context) RenderStatusNotFound() {
c.RenderStatus(http.StatusNotFound)
}
func (c *Context) RenderStatusOK() {
c.RenderStatus(http.StatusOK)
}
func (c *Context) RenderStatusUnauthorized() {
c.RenderStatus(http.StatusUnauthorized)
}
func (c *Context) RenderData(data []byte) error {
_, err := c.Response.Write(data)
return err
}
func (c *Context) RenderJSON(status int, data interface{}) error {
body, err := json.Marshal(data)
if err != nil {
return err
}
c.SetHeaderContentType(_jsonContentType)
c.RenderStatus(status)
return c.RenderData(body)
}
func (c *Context) Render(status int, data interface{}) error {
switch c.rrType {
default:
return c.RenderJSON(status, data)
}
}
/*
######################################################################################################## @(°_°)@ #######
*/