| 61 | } |
| 62 | |
| 63 | func serveCmd(cfgPath *string) *cobra.Command { |
| 64 | return &cobra.Command{ |
| 65 | Use: "serve", |
| 66 | Short: "Run the HTTP gateway", |
| 67 | RunE: func(cmd *cobra.Command, args []string) error { |
| 68 | cfg, err := config.Load(*cfgPath) |
| 69 | if err != nil { |
| 70 | return err |
| 71 | } |
| 72 | srv := server.New(cfg) |
| 73 | defer func() { |
| 74 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 75 | defer cancel() |
| 76 | _ = srv.Shutdown(ctx) |
| 77 | }() |
| 78 | |
| 79 | addr := fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port) |
| 80 | readTO, writeTO, idleTO := server.HTTPLayerTimeouts(cfg) |
| 81 | httpServer := &http.Server{ |
| 82 | Addr: addr, |
| 83 | Handler: srv.Routes(), |
| 84 | ReadHeaderTimeout: 5 * time.Second, |
| 85 | ReadTimeout: readTO, |
| 86 | WriteTimeout: writeTO, |
| 87 | IdleTimeout: idleTO, |
| 88 | } |
| 89 | go func() { |
| 90 | log.Printf("infercore listening on %s", addr) |
| 91 | if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed { |
| 92 | log.Fatalf("server failed: %v", err) |
| 93 | } |
| 94 | }() |
| 95 | sig := make(chan os.Signal, 1) |
| 96 | signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM) |
| 97 | <-sig |
| 98 | return httpServer.Shutdown(context.Background()) |
| 99 | }, |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | func traceCmd(cfgPath *string) *cobra.Command { |
| 104 | return &cobra.Command{ |