[core] Compilers: Pingora runtime config and Caddy JSON, golden files
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
//! Model → [`EdgeConfig`] for the Pingora data plane.
|
||||
|
||||
use super::{CompileOptions, join};
|
||||
use crate::edge_config::{
|
||||
EDGE_CONFIG_VERSION, EdgeAcme, EdgeConfig, EdgeForwardAuth, EdgeHeaderOp, EdgeHealthCheck,
|
||||
EdgeHost, EdgeTls, EdgeUpstream, EdgeValue,
|
||||
};
|
||||
use crate::{
|
||||
AccessLog, AuthMode, HeaderRule, HeaderValue, Model, Scheme, TlsMode, Upstream, UpstreamHost,
|
||||
};
|
||||
|
||||
pub(super) fn compile(model: &Model, o: &CompileOptions) -> EdgeConfig {
|
||||
let site = model.site();
|
||||
let any_acme = model.hosts().iter().any(|h| h.tls == TlsMode::LetsEncrypt);
|
||||
EdgeConfig {
|
||||
version: EDGE_CONFIG_VERSION,
|
||||
http_listen: o.http_listen.to_string(),
|
||||
https_listen: o.https_listen.to_string(),
|
||||
https_port: o.https_listen.port(),
|
||||
cert_dir: o.cert_dir.clone(),
|
||||
state_dir: o.state_dir.clone(),
|
||||
access_log: o.access_log.clone(),
|
||||
acme: any_acme.then(|| EdgeAcme {
|
||||
directory: site.acme.directory.clone(),
|
||||
email: site.acme.email.clone(),
|
||||
}),
|
||||
forward_auth: site.forward_auth.as_ref().map(|fa| EdgeForwardAuth {
|
||||
upstream: upstream(&fa.upstream),
|
||||
path_and_query: fa.path_and_query.clone(),
|
||||
copy_headers: fa.copy_headers.clone(),
|
||||
}),
|
||||
hosts: model
|
||||
.hosts()
|
||||
.iter()
|
||||
.map(|h| EdgeHost {
|
||||
hostname: h.hostname.to_string(),
|
||||
tls: match h.tls {
|
||||
TlsMode::LetsEncrypt => EdgeTls::Acme,
|
||||
TlsMode::Domi => EdgeTls::Files {
|
||||
cert: join(&o.cert_dir, &format!("{}.crt", h.hostname)),
|
||||
key: join(&o.cert_dir, &format!("{}.key", h.hostname)),
|
||||
},
|
||||
},
|
||||
upstreams: h.upstreams.iter().map(upstream).collect(),
|
||||
forward_auth: h.auth == AuthMode::ForwardAuth,
|
||||
websockets: h.websockets,
|
||||
request_headers: h.request_headers.iter().map(op).collect(),
|
||||
response_headers: h.response_headers.iter().map(op).collect(),
|
||||
health_check: h.health_check.as_ref().map(|hc| EdgeHealthCheck {
|
||||
path: hc.path.clone(),
|
||||
interval_secs: hc.interval_secs,
|
||||
}),
|
||||
access_log: h.access_log == AccessLog::Truncated,
|
||||
})
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
fn upstream(u: &Upstream) -> EdgeUpstream {
|
||||
EdgeUpstream {
|
||||
address: u.authority(),
|
||||
tls: u.scheme == Scheme::Https,
|
||||
// Like Go's TLS client in Caddy: SNI is the host name; IP literals send none.
|
||||
sni: match &u.host {
|
||||
UpstreamHost::Name(n) => n.clone(),
|
||||
UpstreamHost::Ip(_) => String::new(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn op(r: &HeaderRule) -> EdgeHeaderOp {
|
||||
match r {
|
||||
HeaderRule::Set { name, value } => EdgeHeaderOp::Set {
|
||||
name: name.clone(),
|
||||
value: match value {
|
||||
HeaderValue::Literal(v) => EdgeValue::Literal { value: v.clone() },
|
||||
HeaderValue::Secret(s) => EdgeValue::Secret {
|
||||
name: s.to_string(),
|
||||
},
|
||||
},
|
||||
},
|
||||
HeaderRule::Remove { name } => EdgeHeaderOp::Remove { name: name.clone() },
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user