From 7d973372cc41ce55f1ccc8af11aeeddcfae08106 Mon Sep 17 00:00:00 2001 From: Adrian Heine Date: Wed, 10 Aug 2022 15:57:17 +0200 Subject: [PATCH] Add uwsgi --- src/templates/nginx/mod.rs | 52 +++++++++++++++++++++++++++++++++++ src/templates/nginx/server.rs | 40 ++++++++++++++++++++++++--- 2 files changed, 88 insertions(+), 4 deletions(-) diff --git a/src/templates/nginx/mod.rs b/src/templates/nginx/mod.rs index 1134378..aa2992d 100644 --- a/src/templates/nginx/mod.rs +++ b/src/templates/nginx/mod.rs @@ -11,3 +11,55 @@ pub fn acme_challenges_snippet>(path: P) -> String { path.as_ref().to_str().unwrap() ) } + +#[cfg(test)] +mod test { + use super::{server_config, uwsgi_snippet}; + + #[test] + fn test_uwsgi() { + assert_eq!( + server_config( + "testdomain", + "/certpath", + "/keypath", + uwsgi_snippet("/uwsgi.sock", "/static"), + "/challenges_snippet.conf" + ), + "server { + listen 443 ssl http2; + listen [::]:443 ssl http2; + server_name testdomain; + include \"/challenges_snippet.conf\"; + + ssl_certificate /certpath; + ssl_certificate_key /keypath; + add_header Strict-Transport-Security \"max-age=31536000\"; + + root /static; + + location / { + try_files $uri @proxy; + } + + location @proxy { + include uwsgi_params; + uwsgi_pass unix:/uwsgi.sock; + } +} + +# Redirect all HTTP links to the matching HTTPS page +server { + listen 80; + listen [::]:80; + server_name testdomain; + include \"/challenges_snippet.conf\"; + + location / { + return 301 https://$host$request_uri; + } +} +" + ); + } +} diff --git a/src/templates/nginx/server.rs b/src/templates/nginx/server.rs index 9f04bbd..fe3b091 100644 --- a/src/templates/nginx/server.rs +++ b/src/templates/nginx/server.rs @@ -85,14 +85,20 @@ pub fn redir_snippet(target: &str) -> String { } pub trait SocketSpec { - fn to_nginx(&self) -> String; + fn to_proxy_pass(&self) -> String; + fn to_uwsgi_pass(&self) -> String; } impl> SocketSpec for T { #[must_use] - fn to_nginx(&self) -> String { + fn to_proxy_pass(&self) -> String { format!("unix:{}:", self.as_ref().to_str().unwrap()) } + + #[must_use] + fn to_uwsgi_pass(&self) -> String { + format!("unix:{}", self.as_ref().to_str().unwrap()) + } } #[derive(Debug)] @@ -107,7 +113,12 @@ impl LocalTcpSocket { impl SocketSpec for LocalTcpSocket { #[must_use] - fn to_nginx(&self) -> String { + fn to_proxy_pass(&self) -> String { + format!("localhost:{}", self.0) + } + + #[must_use] + fn to_uwsgi_pass(&self) -> String { format!("localhost:{}", self.0) } } @@ -129,7 +140,28 @@ pub fn proxy_snippet>( proxy_redirect off; }}", static_path.as_ref().to_str().unwrap(), - socket_path.to_nginx() + socket_path.to_proxy_pass() + ) +} + +#[must_use] +pub fn uwsgi_snippet>( + socket_path: S, + static_path: STATIC, +) -> String { + format!( + "root {}; + + location / {{ + try_files $uri @proxy; + }} + + location @proxy {{ + include uwsgi_params; + uwsgi_pass {}; + }}", + static_path.as_ref().to_str().unwrap(), + socket_path.to_uwsgi_pass() ) }