Documentation
¶
Index ¶
- Constants
- Variables
- func BadRequest(w http.ResponseWriter, msg string) error
- func Forbidden(w http.ResponseWriter, msg string) error
- func MethodNotAllowed(w http.ResponseWriter, msg string) error
- func NotFound(w http.ResponseWriter, msg string) error
- func RequestTimeout(w http.ResponseWriter, msg string) error
- func UnAuthorized(w http.ResponseWriter, msg string) error
- type ApiRspWriter
- type HTML
- type JSON
- type JSONP
- type RetCode
- type TEXT
- type TextRspWriter
- type XML
Constants ¶
const ( MimeText = "text/plain; charset=utf-8" MimeXML = "text/xml; charset=utf-8" MimeHtml = "text/html; charset=utf-8" MimeJson = "application/json; charset=utf-8" MimeJsonP = "text/plain; charset=utf-8" )
TODO: add support of more charset.
Variables ¶
var DefaultPadding func(RetCode, interface{}) map[string]interface{} = defaultContentPadding
default response content padding func. if you want define your content padding format, you can set variable with your padding func. only JSON|XML|JSONP will use the padding func
var ( // when Harsp write the byte to the response, when it failed, this error will return in any response func. ErrWriteFailed = errors.New("failed to write message to http response") )
var ( // this is default success code, you also can reset the variable what you want be, but this must be RetCode struct. SuccessRet = RetCode{Code: "0", Msg: "Success"} )
Functions ¶
func BadRequest ¶
func BadRequest(w http.ResponseWriter, msg string) error
this func return a http response with status code 400 this means the http client request is Invalid, it's usually point the request param.
func Forbidden ¶
func Forbidden(w http.ResponseWriter, msg string) error
this func return a http response with status code 403. which means the client don't has the power to get the resource.
func MethodNotAllowed ¶
func MethodNotAllowed(w http.ResponseWriter, msg string) error
this func return a http response with status code 406 this means the http request method is not allowed, witch usually used in the application of RESTful.
func NotFound ¶
func NotFound(w http.ResponseWriter, msg string) error
this func return a http response with status code 404. witch means the resource what the client want is don't exist.
func RequestTimeout ¶
func RequestTimeout(w http.ResponseWriter, msg string) error
this func return a http response with status code 408 this means the http request is timeout
func UnAuthorized ¶
func UnAuthorized(w http.ResponseWriter, msg string) error
this func return a http response with status code 401 this means the client is unauthorized, like UnLogin.
Types ¶
type ApiRspWriter ¶
type ApiRspWriter interface {
// when you want to return a success msg to the http client, you can use this func.
// this func use the default errCode string 0, if you want change the default value,
// your must to set the package variable of SuccessCode to a new value,
// you can do it when you start your application, once you do that, it will always affected.
// @param: w http.ResponseWriter, all the data will use the writer write into the response
// @param: data interface, this is the response content
// @return: w http.ResponseWriter
Success(http.ResponseWriter) (http.ResponseWriter, error)
// when you want to return a failed msg to the http client, you can use this func.
// you can define your errCode in your application, when you use this func to send a failed response,
// you can assign which errCode you wish to return.
// @param: w http.ResponseWriter, all the data will use the writer write into the response
// @param: data interface, this is the response content
// @return: w http.ResponseWriter
Failed(http.ResponseWriter) (http.ResponseWriter, error)
// contains filtered or unexported methods
}
type HTML ¶
type HTML struct {
Data string
}
func (HTML) Send ¶
func (this HTML) Send(w http.ResponseWriter) (http.ResponseWriter, error)
this func implements the rspWriter func Send. this Send a Json data to the client.
type JSON ¶
type JSON struct {
Rc RetCode
Data interface{}
}
func (JSON) Failed ¶
func (this JSON) Failed(w http.ResponseWriter) (http.ResponseWriter, error)
this implements the rspWriter func Failed. this func send a json data to the client with the errCode.
func (JSON) Success ¶
func (this JSON) Success(w http.ResponseWriter) (http.ResponseWriter, error)
this implements the rspWriter func Success. this Send a Success response to the client with the default success Json data format or you set the default success Json data format.
type JSONP ¶
type JSONP struct {
Rc RetCode
Data interface{}
// when you use jsonp data response, you must be set a callback func.
Callback string
}
func (JSONP) Failed ¶
func (this JSONP) Failed(w http.ResponseWriter) (http.ResponseWriter, error)
when you want to return a failed msg to the http client, you can use this func. you can define your errCode in your application, when you use this func to send a failed response, you can assign which errCode you wish to return. @param: w http.ResponseWriter, all the data will use the writer write into the response @param: data interface, this is the response content @return: w http.ResponseWriter
func (JSONP) Success ¶
func (this JSONP) Success(w http.ResponseWriter) (http.ResponseWriter, error)
when you want to return a success msg to the http client, you can use this func. this func use the default errCode string 0, if you want change the default value, your must to set the package variable of SuccessCode to a new value, you can do it when you start your application, once you do that, it will always affected. @param: w http.ResponseWriter, all the data will use the writer write into the response @param: data interface, this is the response content @return: w http.ResponseWriter
type RetCode ¶
type RetCode struct {
// return error code
// use this code to show this request result.
Code string
// return error message
// this is request result message.
Msg string
}
all the response errCode and msg must be the RetCode struct.
type TEXT ¶
type TEXT struct {
Data string
}
func (TEXT) Send ¶
func (this TEXT) Send(w http.ResponseWriter) (http.ResponseWriter, error)
this func implements the rspWriter func Send. this Send a Json data to the client.
type TextRspWriter ¶
type TextRspWriter interface {
// send a text data, which contains text|html.
// it returns the writer, then you can use the writer continue to write.
// @param: w http.ResponseWriter, all the data will use the writer write into the response
// @param: data string, this is the response content
// @return: w http.ResponseWriter
Send(http.ResponseWriter) (http.ResponseWriter, error)
}
type XML ¶
type XML struct {
Rc RetCode
Data interface{}
}
func (XML) Failed ¶
func (this XML) Failed(w http.ResponseWriter) (http.ResponseWriter, error)
when you want to return a failed msg to the http client, you can use this func. you can define your errCode in your application, when you use this func to send a failed response, you can assign which errCode you wish to return. @param: w http.ResponseWriter, all the data will use the writer write into the response @param: data interface, this is the response content @return: w http.ResponseWriter
func (XML) Success ¶
func (this XML) Success(w http.ResponseWriter) (http.ResponseWriter, error)
when you want to return a success msg to the http client, you can use this func. this func use the default errCode string 0, if you want change the default value, your must to set the package variable of SuccessCode to a new value, you can do it when you start your application, once you do that, it will always affected. @param: w http.ResponseWriter, all the data will use the writer write into the response @param: data interface, this is the response content @return: w http.ResponseWriter