A library for writing host-specific, single-binary configuration management and deployment tools
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.

203 lines
4.9 KiB

  1. use std::fmt::Display;
  2. use std::path::Path;
  3. pub fn default_server<P: AsRef<Path>>(challenges_snippet_path: P) -> String {
  4. format!(
  5. "server {{
  6. listen 80 default_server;
  7. listen [::]:80 default_server;
  8. include \"{}\";
  9. }}",
  10. challenges_snippet_path.as_ref().to_str().unwrap()
  11. )
  12. }
  13. pub fn server_config<D: Display, C: AsRef<Path>, K: AsRef<Path>, T: Display, S: AsRef<Path>>(
  14. domain: D,
  15. cert_path: C,
  16. key_path: K,
  17. content: T,
  18. challenges_snippet_path: S,
  19. ) -> String {
  20. format!(
  21. "server {{
  22. listen 443 ssl http2;
  23. listen [::]:443 ssl http2;
  24. server_name {};
  25. include \"{}\";
  26. ssl_certificate {};
  27. ssl_certificate_key {};
  28. add_header Strict-Transport-Security \"max-age=31536000\";
  29. {}
  30. }}
  31. # Redirect all HTTP links to the matching HTTPS page
  32. server {{
  33. listen 80;
  34. listen [::]:80;
  35. server_name {0};
  36. include \"{1}\";
  37. location / {{
  38. return 301 https://$host$request_uri;
  39. }}
  40. }}
  41. ",
  42. domain,
  43. challenges_snippet_path.as_ref().to_str().unwrap(),
  44. cert_path.as_ref().to_str().unwrap(),
  45. key_path.as_ref().to_str().unwrap(),
  46. content
  47. )
  48. }
  49. pub fn php_snippet<SOCKET: AsRef<Path>, STATIC: AsRef<Path>>(
  50. index: &'static str,
  51. socket_path: SOCKET,
  52. static_path: STATIC,
  53. ) -> String {
  54. format!(
  55. "root {};
  56. index {};
  57. location ~ [^/]\\.php(/|$) {{
  58. fastcgi_pass unix:{};
  59. include \"snippets/fastcgi-php.conf\";
  60. }}",
  61. static_path.as_ref().to_str().unwrap(),
  62. index,
  63. socket_path.as_ref().to_str().unwrap()
  64. )
  65. }
  66. pub fn redir_snippet(target: &str) -> String {
  67. format!(
  68. "location / {{
  69. return 301 $scheme://{}$request_uri;
  70. }}",
  71. target
  72. )
  73. }
  74. pub trait SocketSpec {
  75. fn to_nginx(&self) -> String;
  76. }
  77. impl<T: AsRef<Path>> SocketSpec for T {
  78. fn to_nginx(&self) -> String {
  79. format!("unix:{}:", self.as_ref().to_str().unwrap())
  80. }
  81. }
  82. #[derive(Debug)]
  83. pub struct LocalTcpSocket(usize);
  84. impl LocalTcpSocket {
  85. pub const fn new(x: usize) -> Self {
  86. Self(x)
  87. }
  88. }
  89. impl SocketSpec for LocalTcpSocket {
  90. fn to_nginx(&self) -> String {
  91. format!("localhost:{}", self.0)
  92. }
  93. }
  94. pub fn proxy_snippet<S: SocketSpec, STATIC: AsRef<Path>>(
  95. socket_path: &S,
  96. static_path: STATIC,
  97. ) -> String {
  98. format!(
  99. "root {};
  100. location / {{
  101. try_files $uri @proxy;
  102. }}
  103. location @proxy {{
  104. include fastcgi_params;
  105. proxy_pass http://{};
  106. proxy_redirect off;
  107. }}",
  108. static_path.as_ref().to_str().unwrap(),
  109. socket_path.to_nginx()
  110. )
  111. }
  112. pub fn static_snippet<S: AsRef<Path>>(static_path: S) -> String {
  113. format!(
  114. "root {};
  115. try_files $uri $uri/ $uri.html =404;
  116. ",
  117. static_path.as_ref().to_str().unwrap()
  118. )
  119. }
  120. pub fn dokuwiki_snippet() -> String {
  121. "
  122. location ~ /(data/|conf/|bin/|inc/|install.php) { deny all; }
  123. location / { try_files $uri $uri/ @dokuwiki; }
  124. location @dokuwiki {
  125. # rewrites \"doku.php/\" out of the URLs if you set the userewrite setting to .htaccess in dokuwiki config page
  126. rewrite ^/_media/(.*) /lib/exe/fetch.php?media=$1 last;
  127. rewrite ^/_detail/(.*) /lib/exe/detail.php?media=$1 last;
  128. rewrite ^/_export/([^/]+)/(.*) /doku.php?do=export_$1&id=$2 last;
  129. rewrite ^/(.*) /doku.php?id=$1&$args last;
  130. }".into()
  131. }
  132. pub fn nextcloud_snippet() -> String {
  133. "
  134. client_max_body_size 500M;
  135. # Disable gzip to avoid the removal of the ETag header
  136. gzip off;
  137. rewrite ^/caldav(.*)$ /remote.php/caldav$1 redirect;
  138. rewrite ^/carddav(.*)$ /remote.php/carddav$1 redirect;
  139. rewrite ^/webdav(.*)$ /remote.php/webdav$1 redirect;
  140. error_page 403 /core/templates/403.php;
  141. error_page 404 /core/templates/404.php;
  142. location = /robots.txt {
  143. allow all;
  144. log_not_found off;
  145. access_log off;
  146. }
  147. location ~ ^/(?:\\.htaccess|data|config|db_structure\\.xml|README) {
  148. deny all;
  149. }
  150. location / {
  151. # The following 2 rules are only needed with webfinger
  152. rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
  153. rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;
  154. rewrite ^/.well-known/carddav /remote.php/carddav/ redirect;
  155. rewrite ^/.well-known/caldav /remote.php/caldav/ redirect;
  156. rewrite ^(/core/doc/[^\\/]+/)$ $1/index.html;
  157. try_files $uri $uri/ /index.php;
  158. }
  159. # Adding the cache control header for js and css files
  160. # Make sure it is BELOW the location ~ \\.php(?:$|/) { block
  161. location ~* \\.(?:css|js)$ {
  162. add_header Cache-Control \"public, max-age=7200\";
  163. # Optional: Don't log access to assets
  164. access_log off;
  165. }
  166. # Optional: Don't log access to other assets
  167. location ~* \\.(?:jpg|jpeg|gif|bmp|ico|png|swf)$ {
  168. access_log off;
  169. }
  170. "
  171. .into()
  172. }