// package serv_web -- веб-сервер для графики package serv_web import ( "fmt" "log" "os" "time" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/compress" "github.com/gofiber/template/html/v2" "wartank/pkg/types" ) type СервВеб struct { серв types.ИСервер роутер fiber.App порт string } // НовСервВеб -- возвращает новый веб-сервер func НовСервВеб(серв types.ИСервер) (*СервВеб, error) { if серв == nil { return nil, fmt.Errorf("НовСерВеб(): ИСервер==nil") } порт := os.Getenv("SERVER_HTTP_PORT") if порт == "" { return nil, fmt.Errorf("НовСерВеб(): env SERVER_HTTP_PORT not set") } engine := html.New("./web/tmpl", ".tmpl.html") конф := fiber.Config{ // Prefork: false, ServerHeader: "WarTank", //StrictRouting: false, //CaseSensitive: false, //Immutable: false, //UnescapePath: false, //ETag: false, BodyLimit: 10_000_000, Concurrency: 50, Views: engine, //ViewsLayout: "", //PassLocalsToViews: false, ReadTimeout: 15 * time.Second, WriteTimeout: 15 * time.Second, IdleTimeout: 15 * time.Second, ReadBufferSize: 4096, WriteBufferSize: 4096, //CompressedFileSuffix: "", //ProxyHeader: "", //GETOnly: false, //ErrorHandler: func(*fiber.Ctx, error) error { panic("not implemented") }, //DisableKeepalive: false, //DisableDefaultDate: false, //DisableDefaultContentType: false, //DisableHeaderNormalizing: false, //DisableStartupMessage: false, AppName: "WarTank.App", // StreamRequestBody: false, // DisablePreParseMultipartForm: false, // ReduceMemoryUsage: false, // JSONEncoder: func(interface{}) ([]byte, error) { panic("not implemented") }, // JSONDecoder: func([]byte, interface{}) error { panic("not implemented") }, // XMLEncoder: func(interface{}) ([]byte, error) { panic("not implemented") }, Network: "tcp4", // EnableTrustedProxyCheck: false, // TrustedProxies: []string{}, // EnableIPValidation: false, // EnablePrintRoutes: false, // ColorScheme: fiber.Colors{ // Black: "", // Red: "", // Green: "", // Yellow: "", // Blue: "", // Magenta: "", // Cyan: "", // White: "", // Reset: "", // }, // RequestMethods: []string{}, // EnableSplittingOnParsers: false, } сам := &СервВеб{ серв: серв, роутер: *fiber.New(конф), порт: порт, } compresConfig := compress.Config{ Level: compress.LevelBestCompression, } сам.роутер.Use(compress.New(compresConfig)) сам.роутер.Static("/static", "./web/static", fiber.Static{ Compress: true, ByteRange: true, Browse: true, Index: "index.html", CacheDuration: 30 * time.Second, MaxAge: 3600, }) сам.роутер.Get("/", сам.getIndex) сам.роутер.Get("/bot_list/update", сам.getBotList) сам.роутер.Get("/bot_list", сам.getBotList) return сам, nil } // Пуск -- запускае веб-сервер в работу func (сам *СервВеб) Пуск() { фнПуск := func() { ош := сам.роутер.Listen(сам.порт) if ош != nil { log.Printf("СервВеб.Пуск(): при работе веб-сервера, ош=\n\t%v\n", ош) сам.серв.CancelApp() } } go фнПуск() } // getBotList -- возвращает список ботов func (сам *СервВеб) getBotList(кнт *fiber.Ctx) error { списБот := сам.серв.ServBots().ListBot() return кнт.JSON(списБот) } // getIndex -- возвращает индексную страницу func (сам *СервВеб) getIndex(кнт *fiber.Ctx) error { return кнт.Render("index", fiber.Map{}) }