You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
156 lines
4.5 KiB
156 lines
4.5 KiB
use std::error::Error;
|
|
use std::fmt;
|
|
use std::io;
|
|
use std::ops::Deref;
|
|
|
|
use command_runner::CommandRunner;
|
|
use symbols::Symbol;
|
|
use symbols::file::File as FileSymbol;
|
|
use resources::Resource;
|
|
|
|
#[derive(Debug)]
|
|
pub enum NginxServerError<E: Error> {
|
|
ExecError(E),
|
|
GenericError
|
|
}
|
|
|
|
impl From<io::Error> for NginxServerError<io::Error> {
|
|
fn from(err: io::Error) -> NginxServerError<io::Error> {
|
|
NginxServerError::ExecError(err)
|
|
}
|
|
}
|
|
|
|
impl<E: Error> Error for NginxServerError<E> {
|
|
fn description(&self) -> &str {
|
|
match self {
|
|
&NginxServerError::ExecError(ref e) => e.description(),
|
|
&NginxServerError::GenericError => "Generic error"
|
|
}
|
|
}
|
|
fn cause(&self) -> Option<&Error> {
|
|
match self {
|
|
&NginxServerError::ExecError(ref e) => Some(e),
|
|
_ => None
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<E: Error> fmt::Display for NginxServerError<E> {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
write!(f, "{}", self.description())
|
|
}
|
|
}
|
|
|
|
pub struct NginxServer<'a, C> where C: Deref<Target=str> {
|
|
command_runner: &'a CommandRunner,
|
|
file: FileSymbol<C, Cow<'a, str>>,
|
|
}
|
|
|
|
use std::borrow::Cow;
|
|
|
|
impl<'a> NginxServer<'a, String> {
|
|
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\";
|
|
|
|
ssl_certificate /etc/ssl/local_certs/{0}.crt;
|
|
ssl_certificate_key /etc/ssl/private/{0}.key;
|
|
add_header Strict-Transport-Security \"max-age=31536000\";
|
|
|
|
{1}
|
|
}}
|
|
", domain, content)
|
|
}
|
|
|
|
pub fn new_redir(domain: &'a str, target: &'a str, command_runner: &'a CommandRunner) -> Self {
|
|
let content = NginxServer::server_config(domain, &format!("location / {{
|
|
return 301 $scheme://{}$request_uri;
|
|
}}", target));
|
|
NginxServer::new(domain, content, command_runner)
|
|
}
|
|
|
|
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;
|
|
}}
|
|
|
|
location @proxy {{
|
|
include fastcgi_params;
|
|
proxy_pass http://unix:{}:;
|
|
proxy_redirect off;
|
|
}}", socket_path);
|
|
|
|
let content = NginxServer::server_config(domain, &format!("
|
|
root {};
|
|
{}
|
|
", static_path, proxy_content));
|
|
NginxServer::new(domain, content, command_runner)
|
|
}
|
|
|
|
pub fn new_php(domain: &'a str, socket_path: &'a str, static_path: &'a str, command_runner: &'a CommandRunner) -> Self {
|
|
let content = NginxServer::server_config(domain, &format!("
|
|
root {};
|
|
index index.html index.php;
|
|
location ~ [^/]\\.php(/|$) {{
|
|
fastcgi_pass unix:{};
|
|
include \"snippets/fastcgi-php.conf\";
|
|
}}
|
|
", static_path, socket_path));
|
|
NginxServer::new(domain, content, command_runner)
|
|
}
|
|
|
|
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: FileSymbol::new(file_path, content)
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'a, C> Symbol for NginxServer<'a, C> where C: Deref<Target=str> {
|
|
fn target_reached(&self) -> Result<bool, Box<Error>> {
|
|
if !try!(self.file.target_reached()) {
|
|
return Ok(false);
|
|
}
|
|
// TODO: Could try to find out if the server is in the live config
|
|
Ok(true)
|
|
}
|
|
|
|
fn execute(&self) -> Result<(), Box<Error>> {
|
|
try!(self.file.execute());
|
|
try!(self.command_runner.run_with_args("systemctl", &["reload-or-restart", "nginx"]));
|
|
Ok(())
|
|
}
|
|
|
|
fn get_prerequisites(&self) -> Vec<Box<Resource>> {
|
|
self.file.get_prerequisites()
|
|
}
|
|
}
|
|
|
|
impl<'a, C> fmt::Display for NginxServer<'a, C> where C: Deref<Target=str> {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(),fmt::Error>{
|
|
write!(f, "Nginx server config")
|
|
}
|
|
}
|