Switch CommandRunner to OsStr

This commit is contained in:
Adrian Heine 2017-09-06 21:33:05 +02:00
parent b8bac1142c
commit 3b45b0f77c
22 changed files with 220 additions and 148 deletions

View file

@ -41,16 +41,15 @@ impl<E: Error> fmt::Display for NginxServerError<E> {
}
}
pub struct NginxServer<'a, C> where C: Deref<Target=str> {
command_runner: &'a CommandRunner,
file: FileSymbol<C, Cow<'a, str>>,
pub struct NginxServer<'a, C: 'a + CommandRunner, T> where T: Deref<Target=str> {
command_runner: &'a C,
file: FileSymbol<T, Cow<'a, str>>,
}
use std::borrow::Cow;
impl<'a> NginxServer<'a, String> {
pub fn server_config(domain: &str, content: &str) -> String {
format!("server {{
pub fn server_config(domain: &str, content: &str) -> String {
format!("server {{
listen 80;
server_name {0};
include \"snippets/acme-challenge.conf\";
@ -73,16 +72,17 @@ server {{
{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 / {{
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 / {{
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 {
pub fn new_proxy(domain: &'a str, socket_path: &'a str, static_path: &'a str, command_runner: &'a C) -> Self {
let proxy_content = format!("location / {{
try_files $uri @proxy;
}}
@ -93,15 +93,15 @@ location @proxy {{
proxy_redirect off;
}}", socket_path);
let content = NginxServer::server_config(domain, &format!("
let content = 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!("
pub fn new_php(domain: &'a str, socket_path: &'a str, static_path: &'a str, command_runner: &'a C) -> Self {
let content = server_config(domain, &format!("
root {};
index index.html index.php;
location ~ [^/]\\.php(/|$) {{
@ -112,15 +112,15 @@ location @proxy {{
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!("
pub fn new_static(domain: &'a str, static_path: &'a str, command_runner: &'a C) -> Self {
let content = 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 {
pub fn new(domain: &'a str, content: String, command_runner: &'a C) -> Self {
let file_path: Cow<str> = Cow::from(String::from("/etc/nginx/sites-enabled/") + domain);
NginxServer {
command_runner: command_runner,
@ -129,7 +129,7 @@ location @proxy {{
}
}
impl<'a, C> Symbol for NginxServer<'a, C> where C: 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<Error>> {
if !try!(self.file.target_reached()) {
return Ok(false);
@ -156,7 +156,7 @@ impl<'a, C> Symbol for NginxServer<'a, C> where C: Deref<Target=str> {
}
}
impl<'a, C> fmt::Display for NginxServer<'a, C> where C: Deref<Target=str> {
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")
}