serv_http.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // package serv_web -- веб-сервер для графики
  2. package serv_web
  3. import (
  4. "fmt"
  5. "os"
  6. "time"
  7. "github.com/gofiber/fiber/v2"
  8. "github.com/gofiber/fiber/v2/middleware/compress"
  9. "github.com/gofiber/template/html/v2"
  10. "wartank/pkg/types"
  11. )
  12. type СервВеб struct {
  13. серв types.ИСервер
  14. роутер fiber.App
  15. порт string
  16. }
  17. // НовСервВеб -- возвращает новый веб-сервер
  18. func НовСервВеб(серв types.ИСервер) (*СервВеб, error) {
  19. if серв == nil {
  20. return nil, fmt.Errorf("НовСерВеб(): ИСервер==nil")
  21. }
  22. порт := os.Getenv("SERVER_HTTP_PORT")
  23. if порт == "" {
  24. return nil, fmt.Errorf("НовСерВеб(): env SERVER_HTTP_PORT not set")
  25. }
  26. engine := html.New("./web/tmpl", ".tmpl.html")
  27. конф := fiber.Config{
  28. // Prefork: false,
  29. ServerHeader: "WarTank",
  30. //StrictRouting: false,
  31. //CaseSensitive: false,
  32. //Immutable: false,
  33. //UnescapePath: false,
  34. //ETag: false,
  35. BodyLimit: 10_000_000,
  36. Concurrency: 50,
  37. Views: engine,
  38. //ViewsLayout: "",
  39. //PassLocalsToViews: false,
  40. ReadTimeout: 15 * time.Second,
  41. WriteTimeout: 15 * time.Second,
  42. IdleTimeout: 15 * time.Second,
  43. ReadBufferSize: 4096,
  44. WriteBufferSize: 4096,
  45. //CompressedFileSuffix: "",
  46. //ProxyHeader: "",
  47. //GETOnly: false,
  48. //ErrorHandler: func(*fiber.Ctx, error) error { panic("not implemented") },
  49. //DisableKeepalive: false,
  50. //DisableDefaultDate: false,
  51. //DisableDefaultContentType: false,
  52. //DisableHeaderNormalizing: false,
  53. //DisableStartupMessage: false,
  54. AppName: "WarTank.App",
  55. // StreamRequestBody: false,
  56. // DisablePreParseMultipartForm: false,
  57. // ReduceMemoryUsage: false,
  58. // JSONEncoder: func(interface{}) ([]byte, error) { panic("not implemented") },
  59. // JSONDecoder: func([]byte, interface{}) error { panic("not implemented") },
  60. // XMLEncoder: func(interface{}) ([]byte, error) { panic("not implemented") },
  61. Network: "tcp4",
  62. // EnableTrustedProxyCheck: false,
  63. // TrustedProxies: []string{},
  64. // EnableIPValidation: false,
  65. // EnablePrintRoutes: false,
  66. // ColorScheme: fiber.Colors{
  67. // Black: "",
  68. // Red: "",
  69. // Green: "",
  70. // Yellow: "",
  71. // Blue: "",
  72. // Magenta: "",
  73. // Cyan: "",
  74. // White: "",
  75. // Reset: "",
  76. // },
  77. // RequestMethods: []string{},
  78. // EnableSplittingOnParsers: false,
  79. }
  80. сам := &СервВеб{
  81. серв: серв,
  82. роутер: *fiber.New(конф),
  83. порт: порт,
  84. }
  85. compresConfig := compress.Config{
  86. Level: compress.LevelBestCompression,
  87. }
  88. сам.роутер.Use(compress.New(compresConfig))
  89. сам.роутер.Static("/static", "./web/static", fiber.Static{
  90. Compress: true,
  91. ByteRange: true,
  92. Browse: true,
  93. Index: "index.html",
  94. CacheDuration: 30 * time.Second,
  95. MaxAge: 3600,
  96. })
  97. сам.роутер.Get("/", сам.getIndex)
  98. сам.роутер.Get("/bot_list/update", сам.getBotList)
  99. сам.роутер.Get("/bot_list", сам.getBotList)
  100. return сам, nil
  101. }
  102. // Пуск -- запускае веб-сервер в работу
  103. func (сам *СервВеб) Пуск() {
  104. фнПуск := func() {
  105. ош := сам.роутер.Listen(сам.порт)
  106. if ош != nil {
  107. сам.серв.CancelApp()
  108. }
  109. }
  110. go фнПуск()
  111. }
  112. // getBotList -- возвращает список ботов
  113. func (сам *СервВеб) getBotList(кнт *fiber.Ctx) error {
  114. списБот := сам.серв.ServBots().ListBot()
  115. return кнт.JSON(списБот)
  116. }
  117. // getIndex -- возвращает индексную страницу
  118. func (сам *СервВеб) getIndex(кнт *fiber.Ctx) error {
  119. return кнт.Render("index", fiber.Map{})
  120. }