server.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package mhayaGin
  2. import (
  3. "context"
  4. "net/http"
  5. "time"
  6. "github.com/gin-gonic/gin"
  7. cfile "github.com/mhaya/extend/file"
  8. cfacade "github.com/mhaya/facade"
  9. clog "github.com/mhaya/logger"
  10. )
  11. type (
  12. OptionFunc func(opts *Options)
  13. // Options http server parameter
  14. Options struct {
  15. ReadTimeout time.Duration
  16. ReadHeaderTimeout time.Duration
  17. WriteTimeout time.Duration
  18. IdleTimeout time.Duration
  19. MaxHeaderBytes int
  20. CertFile string
  21. KeyFile string
  22. }
  23. HttpServer struct {
  24. cfacade.IApplication
  25. Options
  26. *gin.Engine
  27. server *http.Server
  28. controllers []IController
  29. }
  30. )
  31. func init() {
  32. SetMode(gin.ReleaseMode)
  33. }
  34. func SetMode(value string) {
  35. gin.SetMode(value)
  36. }
  37. func NewHttpServer(address string, opts ...OptionFunc) *HttpServer {
  38. if address == "" {
  39. clog.Error("listener address is empty.")
  40. return nil
  41. }
  42. httpServer := &HttpServer{
  43. Engine: gin.New(),
  44. }
  45. httpServer.server = &http.Server{
  46. Addr: address,
  47. Handler: httpServer.Engine,
  48. }
  49. httpServer.server.Handler = http.AllowQuerySemicolons(httpServer.server.Handler)
  50. httpServer.Options = defaultOptions()
  51. for _, opt := range opts {
  52. opt(&httpServer.Options)
  53. }
  54. return httpServer
  55. }
  56. func defaultOptions() Options {
  57. return Options{
  58. ReadTimeout: 0,
  59. ReadHeaderTimeout: 0,
  60. WriteTimeout: 0,
  61. IdleTimeout: 0,
  62. MaxHeaderBytes: 0,
  63. CertFile: "",
  64. KeyFile: "",
  65. }
  66. }
  67. func (p *HttpServer) Use(middleware ...GinHandlerFunc) {
  68. p.Engine.Use(BindHandlers(middleware)...)
  69. }
  70. func (p *HttpServer) SetIApplication(app cfacade.IApplication) {
  71. p.IApplication = app
  72. }
  73. func (p *HttpServer) Register(controllers ...IController) *HttpServer {
  74. p.controllers = append(p.controllers, controllers...)
  75. return p
  76. }
  77. func (p *HttpServer) Static(relativePath string, staticDir string) {
  78. dir, ok := cfile.JudgePath(staticDir)
  79. if !ok {
  80. clog.Errorf("static dir path not found. staticDir = %s", staticDir)
  81. return
  82. }
  83. p.Engine.StaticFS(relativePath, http.Dir(dir))
  84. }
  85. func (p *HttpServer) StaticFile(relativePath, filepath string) {
  86. dir, ok := cfile.JudgeFile(filepath)
  87. if !ok {
  88. clog.Errorf("static dir path not found. filePath = %s", filepath)
  89. return
  90. }
  91. p.Engine.StaticFile(relativePath, dir)
  92. }
  93. func (p *HttpServer) Run() {
  94. if p.server.Addr == "" {
  95. clog.Warn("no set listener address.")
  96. return
  97. }
  98. if p.Options.ReadTimeout > 0 {
  99. p.server.ReadTimeout = p.Options.ReadTimeout
  100. }
  101. if p.Options.ReadHeaderTimeout > 0 {
  102. p.server.ReadHeaderTimeout = p.Options.ReadHeaderTimeout
  103. }
  104. if p.Options.WriteTimeout > 0 {
  105. p.server.WriteTimeout = p.Options.WriteTimeout
  106. }
  107. if p.Options.IdleTimeout > 0 {
  108. p.server.IdleTimeout = p.Options.IdleTimeout
  109. }
  110. if p.Options.MaxHeaderBytes > 0 {
  111. p.server.MaxHeaderBytes = p.Options.MaxHeaderBytes
  112. }
  113. for _, controller := range p.controllers {
  114. controller.PreInit(p.IApplication, p.Engine)
  115. controller.Init()
  116. }
  117. p.listener()
  118. }
  119. func (p *HttpServer) listener() {
  120. var err error
  121. if p.Options.CertFile != "" && p.Options.KeyFile != "" {
  122. clog.Infof("https run. https://%s, certFile = %s, keyFile = %s",
  123. p.server.Addr, p.Options.CertFile, p.Options.KeyFile)
  124. err = p.server.ListenAndServeTLS(p.Options.CertFile, p.Options.KeyFile)
  125. } else {
  126. clog.Infof("http run. http://%s", p.server.Addr)
  127. err = p.server.ListenAndServe()
  128. }
  129. if err != http.ErrServerClosed {
  130. clog.Infof("run error = %s", err)
  131. }
  132. }
  133. func (p *HttpServer) Stop() {
  134. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  135. defer cancel()
  136. for _, controller := range p.controllers {
  137. controller.Stop()
  138. }
  139. if err := p.server.Shutdown(ctx); err != nil {
  140. clog.Info(err.Error())
  141. }
  142. clog.Infof("shutdown http server on %s", p.server.Addr)
  143. }
  144. func WithReadTimeout(t time.Duration) OptionFunc {
  145. return func(opts *Options) {
  146. opts.ReadTimeout = t
  147. }
  148. }
  149. func WithReadHeaderTimeout(t time.Duration) OptionFunc {
  150. return func(opts *Options) {
  151. opts.ReadHeaderTimeout = t
  152. }
  153. }
  154. func WithIdleTimeout(t time.Duration) OptionFunc {
  155. return func(opts *Options) {
  156. opts.IdleTimeout = t
  157. }
  158. }
  159. func WithMaxHeaderBytes(val int) OptionFunc {
  160. return func(opts *Options) {
  161. opts.MaxHeaderBytes = val
  162. }
  163. }
  164. func WithCert(certFile, keyFile string) OptionFunc {
  165. return func(opts *Options) {
  166. if certFile == "" || keyFile == "" {
  167. return
  168. }
  169. opts.CertFile = certFile
  170. opts.KeyFile = keyFile
  171. }
  172. }