|
@@ -5,6 +5,8 @@ import (
|
|
|
"encoding/xml"
|
|
|
"net/http"
|
|
|
"net/url"
|
|
|
+
|
|
|
+ "git.chuangxin1.com/myth/sacred"
|
|
|
)
|
|
|
|
|
|
// Message HTTP response
|
|
@@ -46,3 +48,41 @@ func (m Message) UserAgent() string {
|
|
|
func (m Message) Referer() string {
|
|
|
return m.req.Referer()
|
|
|
}
|
|
|
+
|
|
|
+func header(w http.ResponseWriter, contentType string) {
|
|
|
+ w.Header().Set(`Content-Type`, contentType)
|
|
|
+ w.Header().Set(`X-Powered-By`, sacred.LibName+`/`+sacred.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
|
|
|
+}
|