Equivalent config for caddy

1. Caddy version (caddy version):

v2.1.1 h1:X9k1+ehZPYYrSqBvf/ocUgdLSRIuiNiMo7CvyGUQKeA=

2. How I run Caddy:

systemctl

a. System environment:

ec2 ubuntu

b. Command:

sudo systemctl start caddy

c. Service/unit/compose file:

paste full file contents here

d. My complete Caddyfile or JSON config:

paste config here, replacing this text
DO NOT REDACT anything except credentials

3. The problem I’m having:

how you will write caddy config for this nginx configuration

          if ($arg_go-get = "1") {
            # This is a go-get operation.
            return 200 '
                <html><head>
                      <meta name="go-import"
                            content="k8s.io/$repo
                                     git https://github.com/kubernetes/$repo">
                      <meta name="go-source"
                            content="k8s.io/$repo
                                     https://github.com/kubernetes/$repo
                                     https://github.com/kubernetes/$repo/tree/master{/dir}
                                     https://github.com/kubernetes/$repo/blob/master{/dir}/{file}#L{line}">
                </head></html>
            ';
          }

when go get k8s.io/client-go?go-get=1, return this piece of html code

You’ll use the respond subdirective with a query matcher

@goget query go-get=1
respond @goget `your html` 200

You can use backticks or double quotes for strings, backticks work better for wrapping HTML content.

@francislavoie thanks, i can implement this task with a new template, but your solution is neat,

in your snippet, how to evaluate the $repo variable in backticks quoted html.

the request path matcher regex can refer this

You can use the path_regexp matcher then use a placeholder to use the match groups in your content. The docs should be self-explanatory.

a.com {
    log
    @repo path_regexp ^/(?<repo>[^/]*)(?<subpath>/.*)?$
    @goget query go-get=1
    respond @goget `
    <html><head>
            <meta name="go-import"
                content="k8s.io/@repo
                            git https://github.com/kubernetes/@repo">
            <meta name="go-source"
                content="k8s.io/@repo
                            https://github.com/kubernetes/@repo
                            https://github.com/kubernetes/@repo/tree/master{/dir}
                            https://github.com/kubernetes/@repo/blob/master{/dir}/{file}#L{line}">
    </head></html>` 200
}

tried @repo and {{ @repo }} in backticks, not replaced

Please read these sections of the docs:

my problem is not how to match but how to pass it to backticks

tried this, but it didn’t work as expected

 @repo path_regexp  repo ^/(?<repo>[^/]*)(?<subpath>/.*)?$
 @goget query go-get=1
  respond @goget  `
    <html><head>
            <meta name="go-import"
                content="k8s.io/@repo
                            git https://github.com/kubernetes/@repo">
            <meta name="go-source"
                content="k8s.io/{http.regexp.repo.1}
                            https://github.com/kubernetes/{http.regexp.repo.1}
                            https://github.com/kubernetes/{http.regexp.repo.1}/tree/master{/dir}
                            https://github.com/kubernetes/{http.regexp.repo.1}/blob/master{/dir}/{file}#L{line}">
    </head></html>`  200

Please re-read the documentation on named matchers. You’re not using them correctly.

@goget {
	path_regexp repo ^/(?<repo>[^/]*)(?<subpath>/.*)?$
	query go-get=1
}
respond @goget `... {http.regexp.repo.1} ...` 200
1 Like

it seems caddy doesn’t support lookback and Perl syntax regex

Oct 26 11:15:29 ip-172-31-58-46 caddy[8046]: run: loading initial config: loading new config: loading http app module: provision http: server srv0: setting up route handlers: route 2: loading handler modules: position 0: loading module 'subroute': provision http.handlers.subroute: setting up subroutes: route 0: loading matcher modules: module name 'path_regexp': provision http.matchers.path_regexp: compiling matcher regexp (?<repo>[^\/]*)(\?go-get=1)$: error parsing regexp: invalid or unsupported Perl syntax: `(?<`

i changed to go flavor, it worked.

thanks !

1 Like

i post my answer here, for anyone came here

if you write kubernetes operator and want to use custom domain on caddy. you can use this caddy config.

replace a.com and <user> respectively

a.com {
    log
    @goget {
        path_regexp repo \/(.*)
        query go-get=1
    }

    respond @goget `<html><head>
            <meta name="go-import"
                content="jdwl.in/{http.regexp.repo.1}
                            git https://github.com/<user>/{http.regexp.repo.1}">
            <meta name="go-source"
                content="jdwl.in/{http.regexp.repo.1}
                            https://github.com/<user>/{http.regexp.repo.1}
                            https://github.com/<user>/{http.regexp.repo.1}/tree/master{/dir}
                            https://github.com/<user>/{http.regexp.repo.1}/blob/master{/dir}/{file}#L{line}">
            </head></html>` 200

   respond  *     "ok"     200
}

note:

if you use both query and path_regexp, don’t match ?go-get=1 in path_regexp

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.