serv_web.go 4.1 KB

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