Help in optimizing this Caddyfile

1. The problem I’m having:

  1. I would like to optimize/refactor this config file to make it more dynamic and modular (less repetitive).
  2. I want the reverse_proxy to run only if a channel is matched as currently it will run always when the route matches.

3. Caddy version:

v2.10.0

d. My complete Caddy config:

(common_headers) {
	header_up Host {http.reverse_proxy.upstream.hostport}
	header_up -Via
}
:8080 {
    header -Via
    # Group 1 Channels
	route /1/* {
		@channel1_root path_regexp ^/1/1/$
		rewrite @channel1_root /FirstCh/hls/video.m3u8
		@channel1 path_regexp ^/1/1/(.*)$
		rewrite @channel1 /FirstCh/hls/{re.2}

		@channel2_root path_regexp ^/1/2/$
		rewrite @channel2_root /SecondCh/hls/video.m3u8
		@channel2 path_regexp ^/1/2/(.*)$
		rewrite @channel2 /SecondCh/hls/{re.2}

		reverse_proxy http://127.0.0.1 {
			import common_headers
		}
	}
    # Group 2 Channels
    route /2/* {
		@channel1_root path_regexp ^/2/1/$
		rewrite @channel1_root /FirstCh/hls/video.m3u8
		@channel1 path_regexp ^/2/1/(.*)$
		rewrite @channel1 /FirstCh/hls/{re.2}

		@channel2_root path_regexp ^/2/2/$
		rewrite @channel2_root /SecondCh/hls/video.m3u8
		@channel2 path_regexp ^/2/2/(.*)$
		rewrite @channel2 /SecondCh/hls/{re.2}

		reverse_proxy http://another-host {
			import common_headers
		}
	}
}

Your help is very much appreciated.