mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 02:17:05 +00:00
add serve http/2 without tls example
This commit is contained in:
52
_examples/http-server/h2c/main.go
Normal file
52
_examples/http-server/h2c/main.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
|
||||
"golang.org/x/net/http2"
|
||||
)
|
||||
|
||||
/*
|
||||
Serve HTTP/2 without TLS and keep support for HTTP/1.1
|
||||
*/
|
||||
|
||||
// $ go get golang.org/x/net/http2
|
||||
// # Take a look at the golang.org/x/net/http2/h2c package as well,
|
||||
// # you may want to use it.
|
||||
// $ go run main.go
|
||||
// $ brew install curl-openssl
|
||||
// # Add curl-openssl to the front of your path.
|
||||
// Test with the following commands:
|
||||
// $ curl -v --http2 http://localhost:8080
|
||||
// $ curl -v --http1.1 http://localhost:8080
|
||||
func main() {
|
||||
// Initialize Iris Application.
|
||||
app := iris.New()
|
||||
|
||||
// Build the API.
|
||||
app.Any("/", index)
|
||||
|
||||
// Finally, listen and serve on port 8080 using h2c.
|
||||
app.Run(iris.Raw(func() error {
|
||||
host := app.NewHost(&http.Server{
|
||||
Addr: ":8080",
|
||||
})
|
||||
|
||||
err := http2.ConfigureServer(host.Server, &http2.Server{
|
||||
MaxConcurrentStreams: 250,
|
||||
PermitProhibitedCipherSuites: true,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return host.ListenAndServe()
|
||||
}))
|
||||
}
|
||||
|
||||
func index(ctx iris.Context) {
|
||||
ctx.WriteString("Hello, World!\n")
|
||||
}
|
||||
Reference in New Issue
Block a user