1234567891011121314151617181920212223242526272829303132333435363738 |
- 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)
- }
- // 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
- }
|