123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- 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)
- }
|