Clean up nginx servers
This commit is contained in:
parent
2ccc56a097
commit
f505689c9c
1 changed files with 32 additions and 25 deletions
|
|
@ -49,20 +49,27 @@ pub struct NginxServer<'a, C> where C: Deref<Target=str> {
|
|||
use std::borrow::Cow;
|
||||
|
||||
impl<'a> NginxServer<'a, String> {
|
||||
pub fn new(socket_path: Option<&'a str>, domain: &'a str, static_path: &'a str, redir_domains: &[&'a str], command_runner: &'a CommandRunner) -> Self {
|
||||
let file_path: Cow<str> = Cow::from(String::from("/etc/nginx/sites-enabled/") + domain);
|
||||
pub fn server_config(domain: &str, content: &str) -> String {
|
||||
format!("server {{
|
||||
listen 80;
|
||||
listen 443 ssl;
|
||||
ssl_certificate /etc/ssl/local_certs/{0}.crt;
|
||||
ssl_certificate_key /etc/ssl/private/{0}.key;
|
||||
server_name {0};
|
||||
include \"snippets/acme-challenge.conf\";
|
||||
|
||||
let redir_content = redir_domains.iter().map(|redir_domain| format!("server {{
|
||||
listen 80;
|
||||
|
||||
server_name {};
|
||||
return 302 $scheme://{}$request_uri;
|
||||
{1}
|
||||
}}
|
||||
", domain, content)
|
||||
}
|
||||
|
||||
", redir_domain, domain)).fold(String::new(), |s, v| s + &v);
|
||||
pub fn new_redir(domain: &'a str, target: &'a str, command_runner: &'a CommandRunner) -> Self {
|
||||
let content = NginxServer::server_config(domain, &format!("return 302 $scheme://{}$request_uri;", target));
|
||||
NginxServer::new(domain, content, command_runner)
|
||||
}
|
||||
|
||||
let proxy_content = if let Some(socket) = socket_path {
|
||||
format!("location / {{
|
||||
pub fn new_proxy(domain: &'a str, socket_path: &'a str, static_path: &'a str, command_runner: &'a CommandRunner) -> Self {
|
||||
let proxy_content = format!("location / {{
|
||||
try_files $uri @proxy;
|
||||
}}
|
||||
|
||||
|
|
@ -70,28 +77,28 @@ location @proxy {{
|
|||
include fastcgi_params;
|
||||
proxy_pass http://unix:{}:;
|
||||
proxy_redirect off;
|
||||
}}", socket)
|
||||
} else { "\ntry_files $uri $uri/ $uri.html =404;".to_string() }; // FIXME: This is a crude hack
|
||||
|
||||
let content = String::from(redir_content) + &format!("server {{
|
||||
listen 80;
|
||||
listen 443 ssl;
|
||||
ssl_certificate /etc/ssl/local_certs/{0}.crt;
|
||||
ssl_certificate_key /etc/ssl/private/{0}.key;
|
||||
server_name {};
|
||||
}}", socket_path);
|
||||
|
||||
let content = NginxServer::server_config(domain, &format!("
|
||||
root {};
|
||||
include \"snippets/acme-challenge.conf\";
|
||||
{}
|
||||
}}
|
||||
", domain, static_path, proxy_content);
|
||||
NginxServer::new_generic(FileSymbol::new(file_path, content), command_runner)
|
||||
", static_path, proxy_content));
|
||||
NginxServer::new(domain, content, command_runner)
|
||||
}
|
||||
|
||||
pub fn new_generic(file: FileSymbol<String, Cow<'a, str>>, command_runner: &'a CommandRunner) -> Self {
|
||||
pub fn new_static(domain: &'a str, static_path: &'a str, command_runner: &'a CommandRunner) -> Self {
|
||||
let content = NginxServer::server_config(domain, &format!("
|
||||
root {};
|
||||
try_files $uri $uri/ $uri.html =404;
|
||||
", static_path));
|
||||
NginxServer::new(domain, content, command_runner)
|
||||
}
|
||||
|
||||
pub fn new(domain: &'a str, content: String, command_runner: &'a CommandRunner) -> Self {
|
||||
let file_path: Cow<str> = Cow::from(String::from("/etc/nginx/sites-enabled/") + domain);
|
||||
NginxServer {
|
||||
command_runner: command_runner,
|
||||
file: file
|
||||
file: FileSymbol::new(file_path, content)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue