mirror of
				https://github.com/kevinveenbirkenbach/computer-playbook.git
				synced 2025-11-04 12:18:17 +00:00 
			
		
		
		
	Optimized redirect
This commit is contained in:
		@@ -8,9 +8,8 @@ class FilterModule(object):
 | 
				
			|||||||
        """
 | 
					        """
 | 
				
			||||||
        Build a flat list of redirect mappings for all apps:
 | 
					        Build a flat list of redirect mappings for all apps:
 | 
				
			||||||
          - source: each alias domain
 | 
					          - source: each alias domain
 | 
				
			||||||
          - target: the first canonical domain (app.domains.canonical[0] or default)
 | 
					          - target: the first canonical domain
 | 
				
			||||||
 | 
					        Skip mappings where source == target, since they make no sense.
 | 
				
			||||||
        Logic for computing aliases and canonicals is identical to alias_domains_map + canonical_domains_map.
 | 
					 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        def parse_entry(domains_cfg, key, app_id):
 | 
					        def parse_entry(domains_cfg, key, app_id):
 | 
				
			||||||
            if key not in domains_cfg:
 | 
					            if key not in domains_cfg:
 | 
				
			||||||
@@ -55,12 +54,9 @@ class FilterModule(object):
 | 
				
			|||||||
        for app_id, cfg in apps.items():
 | 
					        for app_id, cfg in apps.items():
 | 
				
			||||||
            domains_cfg = cfg.get('domains')
 | 
					            domains_cfg = cfg.get('domains')
 | 
				
			||||||
            if domains_cfg is None:
 | 
					            if domains_cfg is None:
 | 
				
			||||||
                # no domains key → no aliases
 | 
					 | 
				
			||||||
                alias_map[app_id] = []
 | 
					                alias_map[app_id] = []
 | 
				
			||||||
                continue
 | 
					                continue
 | 
				
			||||||
 | 
					 | 
				
			||||||
            if isinstance(domains_cfg, dict) and not domains_cfg:
 | 
					            if isinstance(domains_cfg, dict) and not domains_cfg:
 | 
				
			||||||
                # empty domains dict → only default
 | 
					 | 
				
			||||||
                alias_map[app_id] = [default_domain(app_id, primary_domain)]
 | 
					                alias_map[app_id] = [default_domain(app_id, primary_domain)]
 | 
				
			||||||
                continue
 | 
					                continue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -79,13 +75,16 @@ class FilterModule(object):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            alias_map[app_id] = aliases
 | 
					            alias_map[app_id] = aliases
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # 3) Build flat list of {source, target} entries
 | 
					        # 3) Build flat list of {source, target} entries,
 | 
				
			||||||
 | 
					        #    skipping self-mappings
 | 
				
			||||||
        mappings = []
 | 
					        mappings = []
 | 
				
			||||||
        for app_id, sources in alias_map.items():
 | 
					        for app_id, sources in alias_map.items():
 | 
				
			||||||
            # pick first canonical domain as target
 | 
					 | 
				
			||||||
            canon_list = canonical_map.get(app_id, [])
 | 
					            canon_list = canonical_map.get(app_id, [])
 | 
				
			||||||
            target = canon_list[0] if canon_list else default_domain(app_id, primary_domain)
 | 
					            target = canon_list[0] if canon_list else default_domain(app_id, primary_domain)
 | 
				
			||||||
            for src in sources:
 | 
					            for src in sources:
 | 
				
			||||||
 | 
					                if src == target:
 | 
				
			||||||
 | 
					                    # skip self-redirects
 | 
				
			||||||
 | 
					                    continue
 | 
				
			||||||
                mappings.append({
 | 
					                mappings.append({
 | 
				
			||||||
                    'source': src,
 | 
					                    'source': src,
 | 
				
			||||||
                    'target': target
 | 
					                    'target': target
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,2 +1,2 @@
 | 
				
			|||||||
base_domain:      "{{ domain | regex_replace('^(?:.*\\.)?(.+\\..+)$', '\\1') }}"
 | 
					base_domain:      "{{ domain | regex_replace('^(?:.*\\.)?(.+\\..+)$', '\\1') }}"
 | 
				
			||||||
verification_url: "{{ web_protocol }}://{{domains.matomo}}/index.php?module=API&method=SitesManager.getSitesIdFromSiteUrl&url=https://{{base_domain}}&format=json&token_auth={{applications.matomo.credentials.auth_token}}"
 | 
					verification_url: "{{ web_protocol }}://{{domains | get_domain('mastodon')}}/index.php?module=API&method=SitesManager.getSitesIdFromSiteUrl&url=https://{{base_domain}}&format=json&token_auth={{applications.matomo.credentials.auth_token}}"
 | 
				
			||||||
@@ -30,9 +30,7 @@ class TestDomainMappings(unittest.TestCase):
 | 
				
			|||||||
    def test_empty_domains_cfg(self):
 | 
					    def test_empty_domains_cfg(self):
 | 
				
			||||||
        apps = {'app1': {'domains': {}}}
 | 
					        apps = {'app1': {'domains': {}}}
 | 
				
			||||||
        default = 'app1.example.com'
 | 
					        default = 'app1.example.com'
 | 
				
			||||||
        expected = [
 | 
					        expected = []
 | 
				
			||||||
            {'source': default, 'target': default}
 | 
					 | 
				
			||||||
        ]
 | 
					 | 
				
			||||||
        result = self.filter.domain_mappings(apps, self.primary)
 | 
					        result = self.filter.domain_mappings(apps, self.primary)
 | 
				
			||||||
        self.assertEqual(result, expected)
 | 
					        self.assertEqual(result, expected)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -45,7 +43,6 @@ class TestDomainMappings(unittest.TestCase):
 | 
				
			|||||||
        default = 'app1.example.com'
 | 
					        default = 'app1.example.com'
 | 
				
			||||||
        expected = [
 | 
					        expected = [
 | 
				
			||||||
            {'source': 'alias.com',    'target': default},
 | 
					            {'source': 'alias.com',    'target': default},
 | 
				
			||||||
            {'source': default,        'target': default},
 | 
					 | 
				
			||||||
        ]
 | 
					        ]
 | 
				
			||||||
        result = self.filter.domain_mappings(apps, self.primary)
 | 
					        result = self.filter.domain_mappings(apps, self.primary)
 | 
				
			||||||
        # order not important
 | 
					        # order not important
 | 
				
			||||||
@@ -84,10 +81,7 @@ class TestDomainMappings(unittest.TestCase):
 | 
				
			|||||||
            'app2': {'domains': {'canonical': ['c2.com']}},
 | 
					            'app2': {'domains': {'canonical': ['c2.com']}},
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        expected = [
 | 
					        expected = [
 | 
				
			||||||
            # app1
 | 
					 | 
				
			||||||
            {'source': 'a1.com',              'target': 'app1.example.com'},
 | 
					            {'source': 'a1.com',              'target': 'app1.example.com'},
 | 
				
			||||||
            {'source': 'app1.example.com',    'target': 'app1.example.com'},
 | 
					 | 
				
			||||||
            # app2
 | 
					 | 
				
			||||||
            {'source': 'app2.example.com',    'target': 'c2.com'},
 | 
					            {'source': 'app2.example.com',    'target': 'c2.com'},
 | 
				
			||||||
        ]
 | 
					        ]
 | 
				
			||||||
        result = self.filter.domain_mappings(apps, self.primary)
 | 
					        result = self.filter.domain_mappings(apps, self.primary)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user