|
| 1 | ++++ |
| 2 | +title = "Starting HTTP(S) server with Echo" |
| 3 | +description = "Serving Echo from HTTP(S) server" |
| 4 | +[menu.main] |
| 5 | +name = "HTTP(s) Server" |
| 6 | +parent = "guide" |
| 7 | ++++ |
| 8 | + |
| 9 | +Echo provides following convenience methods to start HTTP server with Echo as a request handler: |
| 10 | + |
| 11 | +* `Echo.Start(address string)` |
| 12 | +* `Echo.StartTLS(address string, certFile, keyFile interface{})` |
| 13 | +* `Echo.StartAutoTLS(address string)` |
| 14 | +* `Echo.StartH2CServer(address string, h2s *http2.Server)` |
| 15 | +* `Echo.StartServer(s *http.Server)` |
| 16 | + |
| 17 | +## HTTP Server |
| 18 | + |
| 19 | +`Echo.Start` is convenience method that starts http server with Echo serving requests on port 8080. |
| 20 | +```go |
| 21 | +func main() { |
| 22 | + e := echo.New() |
| 23 | + // add middleware and routes |
| 24 | + // ... |
| 25 | + if err := e.Start(":8080"); err != http.ErrServerClosed { |
| 26 | + log.Fatal(err) |
| 27 | + } |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +Following is equivalent to `Echo.Start` |
| 32 | +```go |
| 33 | +func main() { |
| 34 | + e := echo.New() |
| 35 | + // add middleware and routes |
| 36 | + // ... |
| 37 | + s := http.Server{ |
| 38 | + Addr: ":8080", |
| 39 | + Handler: e, |
| 40 | + //ReadTimeout: 30 * time.Second, // customize http.Server timeouts |
| 41 | + } |
| 42 | + if err := s.ListenAndServe(); err != http.ErrServerClosed { |
| 43 | + log.Fatal(err) |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +## HTTPS Server |
| 49 | + |
| 50 | +`Echo.StartTLS` is convenience method that starts HTTPS server with Echo serving requests on port 8443 and uses |
| 51 | +`server.crt` and `server.key` as TLS certificate pair. |
| 52 | +```go |
| 53 | +func main() { |
| 54 | + e := echo.New() |
| 55 | + // add middleware and routes |
| 56 | + // ... |
| 57 | + if err := e.StartTLS(":8443", "server.crt", "server.key"); err != http.ErrServerClosed { |
| 58 | + log.Fatal(err) |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +Following is equivalent to `Echo.StartTLS` |
| 64 | +```go |
| 65 | +func main() { |
| 66 | + e := echo.New() |
| 67 | + // add middleware and routes |
| 68 | + // ... |
| 69 | + s := http.Server{ |
| 70 | + Addr: ":8443", |
| 71 | + Handler: e, // set Echo as handler |
| 72 | + TLSConfig: &tls.Config{ |
| 73 | + //MinVersion: 1, // customize TLS configuration |
| 74 | + }, |
| 75 | + //ReadTimeout: 30 * time.Second, // use custom timeouts |
| 76 | + } |
| 77 | + if err := s.ListenAndServeTLS("server.crt", "server.key"); err != http.ErrServerClosed { |
| 78 | + log.Fatal(err) |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +## Auto TLS Server with Let’s Encrypt |
| 84 | + |
| 85 | +See [Auto TLS Recipe](/cooobook/auto-tls#server) |
| 86 | + |
| 87 | +## HTTP/2 Cleartext Server (HTTP2 over HTTP) |
| 88 | + |
| 89 | +`Echo.StartH2CServer` is convenience method that starts a custom HTTP/2 cleartext server on port 8080 |
| 90 | +```go |
| 91 | +func main() { |
| 92 | + e := echo.New() |
| 93 | + // add middleware and routes |
| 94 | + // ... |
| 95 | + s := &http2.Server{ |
| 96 | + MaxConcurrentStreams: 250, |
| 97 | + MaxReadFrameSize: 1048576, |
| 98 | + IdleTimeout: 10 * time.Second, |
| 99 | + } |
| 100 | + if err := e.StartH2CServer(":8080", s); err != http.ErrServerClosed { |
| 101 | + log.Fatal(err) |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +Following is equivalent to `Echo.StartH2CServer` |
| 107 | +```go |
| 108 | +func main() { |
| 109 | + e := echo.New() |
| 110 | + // add middleware and routes |
| 111 | + // ... |
| 112 | + h2s := &http2.Server{ |
| 113 | + MaxConcurrentStreams: 250, |
| 114 | + MaxReadFrameSize: 1048576, |
| 115 | + IdleTimeout: 10 * time.Second, |
| 116 | + } |
| 117 | + s := http.Server{ |
| 118 | + Addr: ":8080", |
| 119 | + Handler: h2c.NewHandler(e, h2s), |
| 120 | + } |
| 121 | + if err := s.ListenAndServe(); err != http.ErrServerClosed { |
| 122 | + log.Fatal(err) |
| 123 | + } |
| 124 | +} |
| 125 | +``` |
0 commit comments