Cargo format

This commit is contained in:
Adrian Heine 2019-09-12 22:59:32 +02:00
parent 9bab810b91
commit 8c0224e983
44 changed files with 1784 additions and 611 deletions

View file

@ -4,14 +4,14 @@ use std::io;
use std::ops::Deref;
use command_runner::CommandRunner;
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
use symbols::file::File as FileSymbol;
use resources::Resource;
use symbols::file::File as FileSymbol;
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
#[derive(Debug)]
pub enum NginxServerError<E: Error> {
ExecError(E),
GenericError
GenericError,
}
impl From<io::Error> for NginxServerError<io::Error> {
@ -24,13 +24,13 @@ impl<E: Error> Error for NginxServerError<E> {
fn description(&self) -> &str {
match self {
NginxServerError::ExecError(ref e) => e.description(),
NginxServerError::GenericError => "Generic error"
NginxServerError::GenericError => "Generic error",
}
}
fn cause(&self) -> Option<&dyn Error> {
match self {
NginxServerError::ExecError(ref e) => Some(e),
_ => None
_ => None,
}
}
}
@ -41,7 +41,10 @@ impl<E: Error> fmt::Display for NginxServerError<E> {
}
}
pub struct NginxServer<'a, C: 'a + CommandRunner, T> where T: Deref<Target=str> {
pub struct NginxServer<'a, C: 'a + CommandRunner, T>
where
T: Deref<Target = str>,
{
command_runner: &'a C,
file: FileSymbol<T, String>,
}
@ -49,7 +52,8 @@ pub struct NginxServer<'a, C: 'a + CommandRunner, T> where T: Deref<Target=str>
use std::borrow::Cow;
pub fn server_config(domain: &str, content: &str) -> String {
format!("server {{
format!(
"server {{
listen 443 ssl http2;
server_name {0};
include \"snippets/acme-challenge.conf\";
@ -71,17 +75,25 @@ server {{
return 301 https://$host$request_uri;
}}
}}
", domain, content)
",
domain, content
)
}
pub fn php_server_config_snippet<'a>(socket_path: Cow<'a, str>, static_path: Cow<'a, str>) -> String {
format!("
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)
}}",
static_path, socket_path
)
}
pub trait SocketSpec {
@ -110,14 +122,26 @@ impl SocketSpec for LocalTcpSocket {
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 / {{
let content = server_config(
domain,
&format!(
"location / {{
return 301 $scheme://{}$request_uri;
}}", target));
}}",
target
),
);
NginxServer::new(domain, content, command_runner)
}
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 / {{
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;
}}
@ -125,25 +149,44 @@ location @proxy {{
include fastcgi_params;
proxy_pass http://{};
proxy_redirect off;
}}", socket_path.to_nginx());
}}",
socket_path.to_nginx()
);
let content = server_config(domain, &format!("
let content = server_config(
domain,
&format!(
"
root {};
{}
", static_path, proxy_content));
",
static_path, proxy_content
),
);
NginxServer::new(domain, content, command_runner)
}
pub fn new_php(domain: &'a str, socket_path: Cow<'a, str>, static_path: Cow<'a, str>, command_runner: &'a C) -> Self {
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, &php_server_config_snippet(socket_path, static_path));
NginxServer::new(domain, content, command_runner)
}
pub fn new_static(domain: &'a str, static_path: &'a str, command_runner: &'a C) -> Self {
let content = server_config(domain, &format!("
let content = server_config(
domain,
&format!(
"
root {};
try_files $uri $uri/ $uri.html =404;
", static_path));
",
static_path
),
);
NginxServer::new(domain, content, command_runner)
}
@ -151,12 +194,15 @@ location @proxy {{
let file_path = String::from("/etc/nginx/sites-enabled/") + domain;
NginxServer {
command_runner,
file: FileSymbol::new(file_path, content)
file: FileSymbol::new(file_path, content),
}
}
}
impl<'a, C: CommandRunner, T> Symbol for NginxServer<'a, C, T> where T: Deref<Target=str> {
impl<'a, C: CommandRunner, T> Symbol for NginxServer<'a, C, T>
where
T: Deref<Target = str>,
{
fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
if !try!(self.file.target_reached()) {
return Ok(false);
@ -167,7 +213,9 @@ impl<'a, C: CommandRunner, T> Symbol for NginxServer<'a, C, T> where T: Deref<Ta
fn execute(&self) -> Result<(), Box<dyn Error>> {
try!(self.file.execute());
self.command_runner.run_successfully("systemctl", &["reload-or-restart", "nginx"])
self
.command_runner
.run_successfully("systemctl", &["reload-or-restart", "nginx"])
}
fn get_prerequisites(&self) -> Vec<Resource> {
@ -178,13 +226,19 @@ impl<'a, C: CommandRunner, T> Symbol for NginxServer<'a, C, T> where T: Deref<Ta
Box::new(SymbolAction::new(runner, self))
}
fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> where Self: 'b {
fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b>
where
Self: 'b,
{
Box::new(OwnedSymbolAction::new(runner, *self))
}
}
impl<'a, C: CommandRunner, T> fmt::Display for NginxServer<'a, C, T> where T: Deref<Target=str> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(),fmt::Error>{
impl<'a, C: CommandRunner, T> fmt::Display for NginxServer<'a, C, T>
where
T: Deref<Target = str>,
{
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "Nginx server config")
}
}