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.

202 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. pub struct LocalTcpSocket(usize);
  83. impl LocalTcpSocket {
  84. pub const fn new(x: usize) -> Self {
  85. Self(x)
  86. }
  87. }
  88. impl SocketSpec for LocalTcpSocket {
  89. fn to_nginx(&self) -> String {
  90. format!("localhost:{}", self.0)
  91. }
  92. }
  93. pub fn proxy_snippet<S: SocketSpec, STATIC: AsRef<Path>>(
  94. socket_path: &S,
  95. static_path: STATIC,
  96. ) -> String {
  97. format!(
  98. "root {};
  99. location / {{
  100. try_files $uri @proxy;
  101. }}
  102. location @proxy {{
  103. include fastcgi_params;
  104. proxy_pass http://{};
  105. proxy_redirect off;
  106. }}",
  107. static_path.as_ref().to_str().unwrap(),
  108. socket_path.to_nginx()
  109. )
  110. }
  111. pub fn static_snippet<S: AsRef<Path>>(static_path: S) -> String {
  112. format!(
  113. "root {};
  114. try_files $uri $uri/ $uri.html =404;
  115. ",
  116. static_path.as_ref().to_str().unwrap()
  117. )
  118. }
  119. pub fn dokuwiki_snippet() -> String {
  120. "
  121. location ~ /(data/|conf/|bin/|inc/|install.php) { deny all; }
  122. location / { try_files $uri $uri/ @dokuwiki; }
  123. location @dokuwiki {
  124. # rewrites \"doku.php/\" out of the URLs if you set the userewrite setting to .htaccess in dokuwiki config page
  125. rewrite ^/_media/(.*) /lib/exe/fetch.php?media=$1 last;
  126. rewrite ^/_detail/(.*) /lib/exe/detail.php?media=$1 last;
  127. rewrite ^/_export/([^/]+)/(.*) /doku.php?do=export_$1&id=$2 last;
  128. rewrite ^/(.*) /doku.php?id=$1&$args last;
  129. }".into()
  130. }
  131. pub fn nextcloud_snippet() -> String {
  132. "
  133. client_max_body_size 500M;
  134. # Disable gzip to avoid the removal of the ETag header
  135. gzip off;
  136. rewrite ^/caldav(.*)$ /remote.php/caldav$1 redirect;
  137. rewrite ^/carddav(.*)$ /remote.php/carddav$1 redirect;
  138. rewrite ^/webdav(.*)$ /remote.php/webdav$1 redirect;
  139. error_page 403 /core/templates/403.php;
  140. error_page 404 /core/templates/404.php;
  141. location = /robots.txt {
  142. allow all;
  143. log_not_found off;
  144. access_log off;
  145. }
  146. location ~ ^/(?:\\.htaccess|data|config|db_structure\\.xml|README) {
  147. deny all;
  148. }
  149. location / {
  150. # The following 2 rules are only needed with webfinger
  151. rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
  152. rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;
  153. rewrite ^/.well-known/carddav /remote.php/carddav/ redirect;
  154. rewrite ^/.well-known/caldav /remote.php/caldav/ redirect;
  155. rewrite ^(/core/doc/[^\\/]+/)$ $1/index.html;
  156. try_files $uri $uri/ /index.php;
  157. }
  158. # Adding the cache control header for js and css files
  159. # Make sure it is BELOW the location ~ \\.php(?:$|/) { block
  160. location ~* \\.(?:css|js)$ {
  161. add_header Cache-Control \"public, max-age=7200\";
  162. # Optional: Don't log access to assets
  163. access_log off;
  164. }
  165. # Optional: Don't log access to other assets
  166. location ~* \\.(?:jpg|jpeg|gif|bmp|ico|png|swf)$ {
  167. access_log off;
  168. }
  169. "
  170. .into()
  171. }