Repository navigation
Expand file tree
/
Copy pathhttp-reverse-proxy.ae
More file actions
65 lines (59 loc) · 2.22 KB
/
Copy pathhttp-reverse-proxy.ae
File metadata and controls
65 lines (59 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Reverse-proxy demo — std.http.proxy.
//
// Forwards every request to a single upstream HTTP server. The
// proxy strips Hop-by-Hop headers, adds X-Forwarded-{For,Proto,
// Host} + Via, rewrites Host: to the upstream's host, and returns
// the upstream's response verbatim (status, headers, body).
//
// Run two processes:
//
// # 1) Start an upstream (any HTTP server). For demo purposes
// # we use python3's stdlib server:
// python3 -m http.server 9000 --directory /tmp
//
// # 2) Build + run this example:
// ae build examples/stdlib/http-reverse-proxy.ae -o /tmp/proxy
// /tmp/proxy
//
// # 3) Test:
// curl http://localhost:8080/ # served by python
// curl -H 'X-Forwarded-For: 1.2.3.4' http://localhost:8080/
// # → upstream sees X-Forwarded-For: 1.2.3.4, <client_ip>
//
// For the production pool shape (load balancing, health checks,
// cache, circuit breaker), see docs/http-reverse-proxy.md.
import std.http
import std.http.proxy
const PORT = 8080
main() {
server = http.server_create(PORT)
if server == 0 {
println("server_create failed")
return
}
// Loopback only, which is what the URLs above reach. The default host
// is every interface: pass "0.0.0.0" here to serve the network. The
// example sweep runs this, and on Windows each new executable that
// listens on every interface opens a firewall prompt (#2639).
http.server_set_host(server, "127.0.0.1")
// Keep-alive so curl --http1.1 can reuse the TCP connection
// across multiple proxied requests.
ka_err = http.server_set_keepalive(server, 1, 0, 30000ms)
if ka_err != "" {
println("keepalive: ${ka_err}")
return
}
// Single upstream, RR (no other to balance against), 30s
// request timeout, default opts (XFF + XFP + XFH on; Host
// rewritten to upstream; 8 MiB body cap).
proxy_err = proxy.mount_simple(server, "/", "http://localhost:9000", 30)
if proxy_err != "" {
println("mount_simple: ${proxy_err}")
return
}
println("listening on http://localhost:${PORT}/ (forwarding to localhost:9000)")
err = http.server_start(server)
if err != "" {
println("server_start: ${err}")
}
}