12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- package myth
- import (
- "encoding/json"
- "encoding/xml"
- "net/http"
- )
- func header(w http.ResponseWriter, contentType string) {
- w.Header().Set(`Content-Type`, contentType)
- w.Header().Set(`X-Powered-By`, LibName+`/`+LibVersion)
- w.WriteHeader(http.StatusOK)
- }
- // SetHeader set http response header
- func SetHeader(w http.ResponseWriter, key, value string) {
- w.Header().Set(key, value)
- }
- // Redirect redirect
- func Redirect(w http.ResponseWriter, url string) {
- w.Header().Set(`Location`, url)
- w.WriteHeader(http.StatusFound)
- }
- // WriteJSON response JSON data.
- func WriteJSON(w http.ResponseWriter, response interface{}) error {
- header(w, `application/json; charset=utf-8`)
- return json.NewEncoder(w).Encode(response)
- }
- // WriteXML response XML data.
- func WriteXML(w http.ResponseWriter, response interface{}) error {
- header(w, `application/xml; charset=utf-8`)
- return xml.NewEncoder(w).Encode(response)
- }
- // WriteBytes response bytes
- func WriteBytes(w http.ResponseWriter, response interface{}) error {
- header(w, `text/html; charset=utf-8`)
- _, err := w.Write(response.([]byte))
- return err
- }
|