Run starts the main process of the Server. The cancelation of ctx will shutdown the server immediately with aborting existing connections. Use WithGracefulContext() to support graceful shutdown.
(ctx context.Context, options ...RunOption)
| 107 | // The cancelation of ctx will shutdown the server immediately with aborting |
| 108 | // existing connections. Use WithGracefulContext() to support graceful shutdown. |
| 109 | func (server *Server) Run(ctx context.Context, options ...RunOption) error { |
| 110 | cctx, cancel := context.WithCancel(ctx) |
| 111 | opts := &RunOptions{gracefulCtx: context.Background()} |
| 112 | for _, opt := range options { |
| 113 | opt(opts) |
| 114 | } |
| 115 | |
| 116 | counter := newCounter(time.Duration(server.options.Timeout) * time.Second) |
| 117 | path := "/kubepi/webkubectl/" |
| 118 | customPath := os.Getenv("TERMINAL_PATH") |
| 119 | if len(customPath) > 0 { |
| 120 | path = customPath |
| 121 | } |
| 122 | |
| 123 | if server.options.EnableRandomUrl { |
| 124 | path = "/" + randomstring.Generate(server.options.RandomUrlLength) + "/" |
| 125 | } |
| 126 | |
| 127 | handlers := server.setupHandlers(cctx, cancel, path, counter) |
| 128 | srv, err := server.setupHTTPServer(handlers) |
| 129 | if err != nil { |
| 130 | return errors.Wrapf(err, "failed to setup an HTTP server") |
| 131 | } |
| 132 | |
| 133 | if server.options.PermitWrite { |
| 134 | log.Printf("Permitting clients to write input to the PTY.") |
| 135 | } |
| 136 | if server.options.Once { |
| 137 | log.Printf("Once option is provided, accepting only one client") |
| 138 | } |
| 139 | |
| 140 | if server.options.Port == "0" { |
| 141 | log.Printf("Port number configured to `0`, choosing a random port") |
| 142 | } |
| 143 | hostPort := net.JoinHostPort(server.options.Address, server.options.Port) |
| 144 | listener, err := net.Listen("tcp", hostPort) |
| 145 | if err != nil { |
| 146 | return errors.Wrapf(err, "failed to listen at `%s`", hostPort) |
| 147 | } |
| 148 | |
| 149 | scheme := "http" |
| 150 | if server.options.EnableTLS { |
| 151 | scheme = "https" |
| 152 | } |
| 153 | host, port, _ := net.SplitHostPort(listener.Addr().String()) |
| 154 | log.Printf("HTTP server is listening at: %s", scheme+"://"+host+":"+port) |
| 155 | //if server.options.Address == "0.0.0.0" { |
| 156 | // for _, address := range listAddresses() { |
| 157 | // log.Printf("Alternative URL: %s", scheme+"://"+address+":"+port+path) |
| 158 | // } |
| 159 | //} |
| 160 | |
| 161 | srvErr := make(chan error, 1) |
| 162 | go func() { |
| 163 | if server.options.EnableTLS { |
| 164 | crtFile := homedir.Expand(server.options.TLSCrtFile) |
| 165 | keyFile := homedir.Expand(server.options.TLSKeyFile) |
| 166 | log.Printf("TLS crt file: " + crtFile) |
no test coverage detected