package web import ( "context" "encoding/json" "encoding/xml" "fmt" "log" "net/http" "os" "os/signal" "syscall" "time" "git.chuangxin1.com/cx/tyr" ) func header(w http.ResponseWriter, contentType string) { w.Header().Set(`Content-Type`, contentType) w.Header().Set(`X-Powered-By`, tyr.LibName+`/`+tyr.LibVersion) w.WriteHeader(http.StatusOK) } // Redirect redirect func Redirect(w http.ResponseWriter, url string) { w.Header().Set(`Location`, url) w.WriteHeader(http.StatusFound) } // GetRealIP get real IP from Request func GetRealIP(req *http.Request) (ip string) { if ips := req.Header["X-Real-Ip"]; ips != nil { ip = ips[0] } return } // SetCookie set http cookie func SetCookie(w http.ResponseWriter, name, value, path string, maxAge int) { cookie := &http.Cookie{ Name: name, Value: value, Path: path, HttpOnly: false, MaxAge: maxAge} http.SetCookie(w, cookie) } // 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`) w.Write(response.([]byte)) return nil } // WriteCtxJSON response JSON data. func WriteCtxJSON(ctx context.Context, w http.ResponseWriter, response interface{}) error { return WriteJSON(w, response) } // WriteCtxXML response XML data. func WriteCtxXML(ctx context.Context, w http.ResponseWriter, response interface{}) error { return WriteXML(w, response) } // WriteCtxBytes response text data. func WriteCtxBytes(ctx context.Context, w http.ResponseWriter, response interface{}) error { return WriteBytes(w, response) } // ResponseJSON reply JSON func ResponseJSON(reply *tyr.ReplyData, req *http.Request, w http.ResponseWriter) error { return WriteJSON(w, reply) } func newServe(addr string, router http.Handler) *http.Server { return &http.Server{ Addr: addr, Handler: router, ReadTimeout: 30 * time.Second, WriteTimeout: 30 * time.Second, MaxHeaderBytes: 1 << 20, } } // ListenAndServe new server and start func ListenAndServe(addr string, router http.Handler) { s := newServe(addr, router) errc := make(chan error) go func() { c := make(chan os.Signal) signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) errc <- fmt.Errorf("%s", <-c) }() go func() { log.Println("HTTP Server listen on", addr) errc <- s.ListenAndServe() log.Println("Exit HTTP server", "Quit") }() log.Println("Exit", <-errc) } // ListenAndServeTLS new server and start func ListenAndServeTLS(addr, certFile, keyFile string, router http.Handler) { s := newServe(addr, router) errc := make(chan error) go func() { c := make(chan os.Signal) signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) errc <- fmt.Errorf("%s", <-c) }() go func() { log.Println("HTTP Server listen on", addr) errc <- s.ListenAndServeTLS(certFile, keyFile) log.Println("Exit HTTP server", "Quit") }() log.Println("Exit", <-errc) }