serv_http.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // package serv_http -- востроенный HTTP-сервер
  2. package serv_http
  3. import (
  4. "fmt"
  5. "os"
  6. "time"
  7. "github.com/gofiber/fiber/v2"
  8. "github.com/gofiber/fiber/v2/middleware/cache"
  9. "github.com/gofiber/fiber/v2/middleware/compress"
  10. "github.com/gofiber/fiber/v2/utils"
  11. "github.com/gofiber/template/html/v2"
  12. "git.p78su.freemyip.com/svi/gostore/internal/serv_http/router_anonym"
  13. "git.p78su.freemyip.com/svi/gostore/pkg/types"
  14. )
  15. // ServHttp -- встроенный HTTP-сервер
  16. type ServHttp struct {
  17. serv types.IService
  18. fiberApp *fiber.App
  19. port string
  20. store types.IStore
  21. anonym *router_anonym.RouterAnonym
  22. }
  23. // NewServHttp -- возвращает новый HTTP-сервер
  24. func NewServHttp(serv types.IService) (*ServHttp, error) {
  25. if serv == nil {
  26. return nil, fmt.Errorf("NewServHttp(): IService==nil")
  27. }
  28. port := os.Getenv("HTTP_PORT")
  29. if port == "" {
  30. return nil, fmt.Errorf("NewServHttp(): env HTTP_PORT not set")
  31. }
  32. engineTmpl := html.New("./static/tmpl", ".tmpl.html")
  33. appConf := fiber.Config{
  34. Prefork: false,
  35. ServerHeader: "GoStore",
  36. StrictRouting: false,
  37. CaseSensitive: false,
  38. Immutable: false,
  39. UnescapePath: false,
  40. ETag: false,
  41. BodyLimit: 10_000_000,
  42. Concurrency: 0,
  43. Views: engineTmpl,
  44. ViewsLayout: "",
  45. PassLocalsToViews: false,
  46. ReadTimeout: time.Second * 60,
  47. WriteTimeout: time.Second * 60,
  48. IdleTimeout: time.Second * 10,
  49. ReadBufferSize: 0,
  50. WriteBufferSize: 0,
  51. CompressedFileSuffix: ".fiber.gz",
  52. ProxyHeader: "",
  53. GETOnly: false,
  54. // ErrorHandler: func(*fiber.Ctx, error) error { panic("not implemented") },
  55. DisableKeepalive: false,
  56. DisableDefaultDate: false,
  57. DisableDefaultContentType: false,
  58. DisableHeaderNormalizing: false,
  59. DisableStartupMessage: false,
  60. AppName: "GoStore",
  61. StreamRequestBody: false,
  62. DisablePreParseMultipartForm: false,
  63. ReduceMemoryUsage: false,
  64. // JSONEncoder: func(interface{}) ([]byte, error) { panic("not implemented") },
  65. // JSONDecoder: func([]byte, interface{}) error { panic("not implemented") },
  66. // XMLEncoder: func(interface{}) ([]byte, error) { panic("not implemented") },
  67. Network: "tcp4",
  68. EnableTrustedProxyCheck: false,
  69. TrustedProxies: []string{},
  70. EnableIPValidation: false,
  71. EnablePrintRoutes: true,
  72. // ColorScheme: fiber.Colors{
  73. // Black: "",
  74. // Red: "",
  75. // Green: "",
  76. // Yellow: "",
  77. // Blue: "",
  78. // Magenta: "",
  79. // Cyan: "",
  80. // White: "",
  81. // Reset: "",
  82. // },
  83. // RequestMethods: []string{},
  84. EnableSplittingOnParsers: false,
  85. }
  86. app := fiber.New(appConf)
  87. app.Static("/static", "./static")
  88. compressConf := compress.Config{
  89. Level: compress.LevelBestCompression,
  90. }
  91. app.Use(compress.New(compressConf))
  92. cacheConf := cache.Config{
  93. Next: nil,
  94. Expiration: 30 * time.Minute,
  95. CacheHeader: "X-Cache",
  96. CacheControl: true,
  97. KeyGenerator: func(c *fiber.Ctx) string {
  98. return utils.CopyString(c.Path())
  99. },
  100. ExpirationGenerator: nil,
  101. StoreResponseHeaders: false,
  102. Storage: nil,
  103. MaxBytes: 0,
  104. Methods: []string{fiber.MethodGet, fiber.MethodHead},
  105. }
  106. app.Use(cache.New(cacheConf))
  107. sf := &ServHttp{
  108. serv: serv,
  109. fiberApp: app,
  110. port: port,
  111. store: serv.Store(),
  112. }
  113. var err error
  114. sf.anonym, err = router_anonym.NewRouterAnonym(sf)
  115. if err != nil {
  116. return nil, err
  117. }
  118. sf.fiberApp.Get("/", sf.get)
  119. return sf, nil
  120. }
  121. // Возвращает индексную страницу хранилища
  122. func (sf *ServHttp) get(ctx *fiber.Ctx) error {
  123. return ctx.Render("index", fiber.Map{})
  124. }
  125. // Service -- возвращает сервис
  126. func (sf *ServHttp) Service() types.IService {
  127. return sf.serv
  128. }
  129. // FiberApp -- возвращает fiber-приложение
  130. func (sf *ServHttp) FiberApp() *fiber.App {
  131. return sf.fiberApp
  132. }
  133. // Run -- запускает веб-сервер в работу
  134. func (sf *ServHttp) Run() error {
  135. defer sf.serv.CancelApp()
  136. err := sf.fiberApp.Listen(":" + sf.port)
  137. if err != nil {
  138. return fmt.Errorf("ServHttp.Run(): in listen port(%q), err=\n\t%w", sf.port, err)
  139. }
  140. return nil
  141. }