Get host in module

Hello, is there a way to get host in module’s provision method?

Let’s say our module’s called “test” and for the following caddyfile

http://localhost {
    route {
        test
    }
}

i’m trying to get “localhost” or “http://localhost” value in Provision method

Usually you’ll get the Host from the HTTP request header. In Provision you don’t know what it will be yet. What are you trying to do?

I’m trying to upgrade module from caddy v1 to v2 and old version uses caddy.Controller.Key, which analogue i can’t find in v2

What are you using Controller.Key for though? Do you have a link to the source code?

Controller.Key is used as a key in a storage, so each host have it’s own “space”. I don’t have a link to the source code, but i can try to provide an example

v1

caddy.RegisterPlugin(
	Name,
	caddy.Plugin{
		Action: func(c *caddy.Controller) error {
			val := someGlobalStorage.Get(c.Key)

			return nil
		},
	},
)

v2

func (m *SomeModule) Provision(ctx caddy.Context) error {
	val := someGlobalStorage.Get(...)

	return nil
}

What’s the purpose of this global storage? What are you trying to do, specifically?

This feels like an XY problem - Wikipedia situation. You’ll need to be more specific so that we’re on the same page.

Problem is to distinguish modules in different hosts. For example:

http://localhost {
    route {
        test
    }
}

http://foo.bar {
    route {
        test
    }
}

there will be 2 test module’s instances and i’d like to get host in each Provision call. I can’t describe global storage’s purpose without diving into internal kitchen but let’s say it’s some config storage, so we get configuration based on host

Well, that’s not possible because the site address is a matcher in a subroute above your handler, and your handler doesn’t have access to that.

If you need to identify your specific handler by some unique ID, then you should pass it as an argument to that handler in its config.

But seriously, it’s hard to give specific help without specific details. If you’re vague about what you’re trying to achieve, we can only be vague with our answers.

I think this answers my question, thanks for your help

1 Like

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