This commit is contained in:
Adrian Heine 2019-05-03 23:23:12 +02:00
parent abb6947853
commit ed9e29b8b8
8 changed files with 279 additions and 25 deletions

View file

@ -50,17 +50,6 @@ use std::borrow::Cow;
pub fn server_config(domain: &str, content: &str) -> String {
format!("server {{
listen 80;
server_name {0};
include \"snippets/acme-challenge.conf\";
location / {{
# Redirect all HTTP links to the matching HTTPS page
return 301 https://$host$request_uri;
}}
}}
server {{
listen 443 ssl http2;
server_name {0};
include \"snippets/acme-challenge.conf\";
@ -71,9 +60,54 @@ server {{
{1}
}}
# Redirect all HTTP links to the matching HTTPS page
server {{
listen 80;
server_name {0};
include \"snippets/acme-challenge.conf\";
location / {{
return 301 https://$host$request_uri;
}}
}}
", domain, content)
}
pub fn php_server_config_snippet<'a>(socket_path: Cow<'a, str>, static_path: Cow<'a, str>) -> String {
format!("
root {};
index index.html index.php;
location ~ [^/]\\.php(/|$) {{
fastcgi_pass unix:{};
include \"snippets/fastcgi-php.conf\";
}}", static_path, socket_path)
}
pub trait SocketSpec {
fn to_nginx(&self) -> String;
}
impl SocketSpec for &str {
fn to_nginx(&self) -> String {
format!("unix:{}:", self)
}
}
pub struct LocalTcpSocket(usize);
impl LocalTcpSocket {
pub fn new(x: usize) -> Self {
LocalTcpSocket(x)
}
}
impl SocketSpec for LocalTcpSocket {
fn to_nginx(&self) -> String {
format!("localhost:{}", self.0)
}
}
impl<'a, C: CommandRunner> NginxServer<'a, C, String> {
pub fn new_redir(domain: &'a str, target: &'a str, command_runner: &'a C) -> Self {
let content = server_config(domain, &format!("location / {{
@ -82,16 +116,16 @@ impl<'a, C: CommandRunner> NginxServer<'a, C, String> {
NginxServer::new(domain, content, command_runner)
}
pub fn new_proxy(domain: &'a str, socket_path: &'a str, static_path: &'a str, command_runner: &'a C) -> Self {
pub fn new_proxy<S: SocketSpec>(domain: &'a str, socket_path: S, static_path: &'a str, command_runner: &'a C) -> Self {
let proxy_content = format!("location / {{
try_files $uri @proxy;
}}
location @proxy {{
include fastcgi_params;
proxy_pass http://unix:{}:;
proxy_pass http://{};
proxy_redirect off;
}}", socket_path);
}}", socket_path.to_nginx());
let content = server_config(domain, &format!("
root {};
@ -101,14 +135,7 @@ location @proxy {{
}
pub fn new_php(domain: &'a str, socket_path: Cow<'a, str>, static_path: Cow<'a, str>, command_runner: &'a C) -> Self {
let content = server_config(domain, &format!("
root {};
index index.html index.php;
location ~ [^/]\\.php(/|$) {{
fastcgi_pass unix:{};
include \"snippets/fastcgi-php.conf\";
}}
", static_path, socket_path));
let content = server_config(domain, &php_server_config_snippet(socket_path, static_path));
NginxServer::new(domain, content, command_runner)
}