package gateway import ( "context" "fmt" "net" "strings" "esway/internal/middleware/analysis" "esway/internal/middleware/logger" "esway/internal/opt" "esway/internal/tool" "github.com/loveuer/nf" ) func Start(ctx context.Context) error { ch := make(chan struct{}) app := nf.New(nf.Config{ BodyLimit: 4 * 1024 * 1024, DisableLogger: true, DisableBanner: true, DisableMessagePrint: true, }) h, err := proxy(ctx) if err != nil { return err } app.Use(logger.New(logger.Config{IgnoreFn: func(c *nf.Ctx) bool { path := c.Path() if strings.HasPrefix(path, "/.") || strings.HasPrefix(path, "/_") { return true } return false }})) app.Use(analysis.New()) app.Any("/*any", h) ln, err := net.Listen("tcp", opt.Cfg.Listen.Gateway) if err != nil { return fmt.Errorf("gateway listen at %s err: %s", opt.Cfg.Listen.Gateway, err.Error()) } go func() { ch <- struct{}{} fmt.Printf("esway: gateway listen at %s\n", opt.Cfg.Listen.Gateway) _ = app.RunListener(ln) }() <-ch go func() { ch <- struct{}{} <-ctx.Done() _ = app.Shutdown(tool.Timeout(2)) }() <-ch return nil }