GitLab Commit is coming up on August 3-4. Learn how to innovate together using GitLab, the DevOps platform. Register for free: gitlabcommitvirtual2021.com

Commit c803c8f9 authored by Alberto's avatar Alberto
Browse files

Moving out some functions for dealing to HTTP redirects

... so that they can be used elsewhere
parent 996a28eb
......@@ -13,6 +13,10 @@
orphu = "orphu.arstecnica.it";
in {
options.metapensiero = {
lib = mkOption {
type = types.attrs;
default = import ./lib {inherit config lib pkgs;};
};
monitorHost = mkOption {
type = types.str;
default = orphu;
......
# -*- coding: utf-8 -*-
# :Project: nixos-modules -- Library entrypoint
# :Created: ven 15 gen 2021, 01:57:03
# :Author: Alberto Berti <alberto@metapensiero.it>
# :License: GNU General Public License version 3 or later
# :Copyright: © 2021 Alberto Berti
#
{ config, lib, pkgs, ... }: {
nginx = import ./nginx.nix {inherit config lib pkgs;};
}
# -*- coding: utf-8 -*-
# :Project: nixos-modules -- nginx utils
# :Created: ven 15 gen 2021, 01:36:17
# :Author: Alberto Berti <alberto@metapensiero.it>
# :License: GNU General Public License version 3 or later
# :Copyright: © 2021 Alberto Berti
#
/*
A set of functions to help with configuring nginx virtualhosts.
*/
{ config, lib, pkgs, ... }:
let
inherit (lib) concatStringsSep mapAttrsToList;
in {
/* Define an nginx redirection from a path with multiple paths to destination.
Example:
redirs = { "/a/path" = {
"foo=bar&bar=foo" = "/dest/path";
"bar=baz" = http://example.com;
};
};
mkQueryRedirects redirs;
=> location /a/path {
if ($args ~ foo=bar&bar=foo) {
return 301 /dest/path;
}
if ($args ~ bar=baz) {
return 301 http://example.com;
}
}
The following function doesn't use a much simpler approach with the `map`
directive of nginx because that needs a "stream" context that requires a
reconfiguration of all the server (i.e. instead of having an external `http`
directive a `stream` one has to be used).
*/
mkQueryRedirects = queryRedirs:
let
mkArgsIf = query: dest: ''
if ($args ~ ${query}) {
return 301 ${dest};
}
'';
mkRedirLocation = path: redirMaps: ''
location ${path} {
${concatStringsSep "\n" (mapAttrsToList
(from: to: mkArgsIf from to) redirMaps)}
}
'';
in
concatStringsSep "\n" (mapAttrsToList
(loc: redirData: mkRedirLocation loc redirData.queryMappings) queryRedirs);
/* Create a simple redirect definition given a mapping with
{ match = destination;} entries. Match can be a regexp and destination
can be a path or an url.
*/
mkRedirects = redirects:
concatStringsSep "\n" (mapAttrsToList
(old: new: "rewrite ${old} ${new} permanent;") redirects);
}
......@@ -9,28 +9,8 @@
{config, lib, options, ...}:
let
inherit (lib) attrValues concatStringsSep flip mapAttrs mapAttrsToList mkIf mkMerge;
mkRedirects = redirects:
concatStringsSep "\n" (mapAttrsToList
(old: new: "rewrite ${old} ${new} permanent;") redirects);
inherit (config.metapensiero.lib.nginx) mkRedirects mkQueryRedirects;
sep = "\n";
mkRedirLocation = path: redirMaps: ''
location ${path} {
${concatStringsSep sep (mapAttrsToList
(from: to: mkArgsIf from to) redirMaps)}
}
'';
mkArgsIf = query: dest: ''
if ($args ~ ${query}) {
return 301 ${dest};
}
'';
# the following function doesn't use a much simpler approach with the `map`
# directive of nginx because that needs a "stream" context that requires a
# reconfiguration of all the server (i.e. instead of having an external `http`
# directive a `stream` one has to be used).
mkQueryRedirects = queryRedirs:
concatStringsSep "\n" (mapAttrsToList
(loc: redirData: mkRedirLocation loc redirData.queryMappings) queryRedirs);
mkLogging = logsBase: vh: ''
error_log ${logsBase}/${vh.serverName}-error.log;
access_log ${logsBase}/${vh.serverName}-access.log combined;
......
......@@ -236,29 +236,31 @@
config.siteName = mkDefault name;
};
in {
options.metapensiero.sites = {
logsBase = mkOption {
description = ''
The directory where logging files will be placed
'';
default = "/var/log/nginx";
example = "/var/log/nginx";
type = types.str;
};
logsRetentionWeeks = mkOption {
description = ''
How many weeks of logging files to keep
'';
default = 52;
example = 52;
type = types.int;
};
sites = mkOption {
description = ''
Sites running on this system
'';
default = {};
type = with types; attrsOf (submodule sitesSubmodule);
options.metapensiero = {
sites = {
logsBase = mkOption {
description = ''
The directory where logging files will be placed
'';
default = "/var/log/nginx";
example = "/var/log/nginx";
type = types.str;
};
logsRetentionWeeks = mkOption {
description = ''
How many weeks of logging files to keep
'';
default = 52;
example = 52;
type = types.int;
};
sites = mkOption {
description = ''
Sites running on this system
'';
default = {};
type = with types; attrsOf (submodule sitesSubmodule);
};
};
};
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment