http2
The http2 module runs an HTTP/2 server. It’s built on Go’s net/http and
x/net/http2, so HPACK and framing are handled for you. The handler has the
same shape as the http_server library — most code is
portable between them. Import it with import http2.
Functions
| Function | Signature | Description |
|---|---|---|
serve | http2.serve(port: int, handler, certFile: string, keyFile: string) → void | Serve HTTP/2 over TLS (for real browsers) |
serveCleartext | http2.serveCleartext(port: int, handler) → void | Serve cleartext HTTP/2 (h2c), no TLS |
The handler receives a request object and returns a string (200 OK) or a
Response:
fn handle(req) {
// req.method / req.path / req.query / req.headers / req.body
return "string"
// or: Response{status: 200, headers: [], body: "..."}
}
Example
import http2
fn handle(req) {
if req.path == "/" {
return "hello over HTTP/2"
}
return Response{status: 404, headers: [], body: "not found"}
}
// h2c — test with: curl --http2-prior-knowledge http://localhost:8080/
http2.serveCleartext(8080, handle)
// TLS h2:
// http2.serve(8443, handle, "cert.pem", "key.pem")