serv_http.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // package serv_http -- востроенный HTTP-сервер
  2. package serv_http
  3. import (
  4. "fmt"
  5. "os"
  6. "time"
  7. "github.com/gofiber/fiber/v2"
  8. "git.p78su.freemyip.com/svi/gostore/pkg/types"
  9. )
  10. // ServHttp -- встроенный HTTP-сервер
  11. type ServHttp struct {
  12. serv types.IServHttp
  13. fiberApp *fiber.App
  14. port string
  15. }
  16. // NewServHttp -- возвращает новый HTTP-сервер
  17. func NewServHttp(serv types.IService) (types.IServHttp, error) {
  18. if serv == nil {
  19. return nil, fmt.Errorf("NewServHttp(): IService==nil")
  20. }
  21. port := os.Getenv("STORE_HTTP_PORT")
  22. if port == "" {
  23. return nil, fmt.Errorf("NewServHttp(): env STORE_HTTP_PORT not set")
  24. }
  25. config := fiber.Config{
  26. Prefork: false,
  27. ServerHeader: "GoStore",
  28. StrictRouting: false,
  29. CaseSensitive: false,
  30. Immutable: false,
  31. UnescapePath: false,
  32. ETag: false,
  33. BodyLimit: 10_000_000,
  34. Concurrency: 0,
  35. Views: nil,
  36. ViewsLayout: "",
  37. PassLocalsToViews: false,
  38. ReadTimeout: time.Second * 60,
  39. WriteTimeout: time.Second * 60,
  40. IdleTimeout: time.Second * 10,
  41. ReadBufferSize: 0,
  42. WriteBufferSize: 0,
  43. CompressedFileSuffix: "",
  44. ProxyHeader: "",
  45. GETOnly: false,
  46. // ErrorHandler: func(*fiber.Ctx, error) error { panic("not implemented") },
  47. DisableKeepalive: false,
  48. DisableDefaultDate: false,
  49. DisableDefaultContentType: false,
  50. DisableHeaderNormalizing: false,
  51. DisableStartupMessage: false,
  52. AppName: "GoStore",
  53. StreamRequestBody: false,
  54. DisablePreParseMultipartForm: false,
  55. ReduceMemoryUsage: false,
  56. // JSONEncoder: func(interface{}) ([]byte, error) { panic("not implemented") },
  57. // JSONDecoder: func([]byte, interface{}) error { panic("not implemented") },
  58. // XMLEncoder: func(interface{}) ([]byte, error) { panic("not implemented") },
  59. Network: "tcp4",
  60. EnableTrustedProxyCheck: false,
  61. TrustedProxies: []string{},
  62. EnableIPValidation: false,
  63. EnablePrintRoutes: false,
  64. // ColorScheme: fiber.Colors{
  65. // Black: "",
  66. // Red: "",
  67. // Green: "",
  68. // Yellow: "",
  69. // Blue: "",
  70. // Magenta: "",
  71. // Cyan: "",
  72. // White: "",
  73. // Reset: "",
  74. // },
  75. // RequestMethods: []string{},
  76. EnableSplittingOnParsers: false,
  77. }
  78. app := fiber.New(config)
  79. sf := &ServHttp{
  80. serv: serv,
  81. fiberApp: app,
  82. port: port,
  83. }
  84. sf.fiberApp.Get("/time", sf.getTime)
  85. return sf, nil
  86. }
  87. // getTime -- возвращает ответ с меткой времени (для KeepAlive)
  88. func (sf *ServHttp) getTime(ctx *fiber.Ctx) error {
  89. ctx.Response().Header.Add("Cache-Control", "no-cache") // Cache-Control: no-cache
  90. return ctx.SendString(time.Now().Local().String())
  91. }
  92. // Run -- запускает веб-сервер в работу
  93. func (sf *ServHttp) Run() error {
  94. err := sf.fiberApp.Listen(":" + sf.port)
  95. if err != nil {
  96. return fmt.Errorf("ServHttp.Run(): in listen port(%q), err=\n\t%w", sf.port, err)
  97. }
  98. return nil
  99. }