123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- 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, r *http.Request, url string) {
- //w.Header().Set(`Location`, url)
- //w.WriteHeader(http.StatusFound)
- http.Redirect(w, r, url, 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
- }
|