| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- // package serv_http -- востроенный HTTP-сервер
- package serv_http
- import (
- "fmt"
- "os"
- "time"
- "github.com/gofiber/fiber/v2"
- "git.p78su.freemyip.com/svi/gostore/pkg/types"
- )
- // ServHttp -- встроенный HTTP-сервер
- type ServHttp struct {
- serv types.IServHttp
- fiberApp *fiber.App
- port string
- }
- // NewServHttp -- возвращает новый HTTP-сервер
- func NewServHttp(serv types.IService) (types.IServHttp, error) {
- if serv == nil {
- return nil, fmt.Errorf("NewServHttp(): IService==nil")
- }
- port := os.Getenv("STORE_HTTP_PORT")
- if port == "" {
- return nil, fmt.Errorf("NewServHttp(): env STORE_HTTP_PORT not set")
- }
- config := fiber.Config{
- Prefork: false,
- ServerHeader: "GoStore",
- StrictRouting: false,
- CaseSensitive: false,
- Immutable: false,
- UnescapePath: false,
- ETag: false,
- BodyLimit: 10_000_000,
- Concurrency: 0,
- Views: nil,
- ViewsLayout: "",
- PassLocalsToViews: false,
- ReadTimeout: time.Second * 60,
- WriteTimeout: time.Second * 60,
- IdleTimeout: time.Second * 10,
- ReadBufferSize: 0,
- WriteBufferSize: 0,
- CompressedFileSuffix: "",
- ProxyHeader: "",
- GETOnly: false,
- // ErrorHandler: func(*fiber.Ctx, error) error { panic("not implemented") },
- DisableKeepalive: false,
- DisableDefaultDate: false,
- DisableDefaultContentType: false,
- DisableHeaderNormalizing: false,
- DisableStartupMessage: false,
- AppName: "GoStore",
- 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,
- }
- app := fiber.New(config)
- sf := &ServHttp{
- serv: serv,
- fiberApp: app,
- port: port,
- }
- sf.fiberApp.Get("/time", sf.getTime)
- return sf, nil
- }
- // getTime -- возвращает ответ с меткой времени (для KeepAlive)
- func (sf *ServHttp) getTime(ctx *fiber.Ctx) error {
- ctx.Response().Header.Add("Cache-Control", "no-cache") // Cache-Control: no-cache
- return ctx.SendString(time.Now().Local().String())
- }
- // Run -- запускает веб-сервер в работу
- func (sf *ServHttp) Run() error {
- err := sf.fiberApp.Listen(":" + sf.port)
- if err != nil {
- return fmt.Errorf("ServHttp.Run(): in listen port(%q), err=\n\t%w", sf.port, err)
- }
- return nil
- }
|