mirror of
https://github.com/caddyserver/caddy.git
synced 2025-05-28 14:45:49 +08:00
Refactor Caddyfile adapter and module registration
Use piles from which to draw config values. Module values can return their name, so now we can do two-way mapping from value to name and name to value; whereas before we could only map name to value. This was problematic with the Caddyfile adapter since it receives values and needs to know the name to put in the config.
This commit is contained in:
@ -37,10 +37,7 @@ import (
|
||||
func init() {
|
||||
weakrand.Seed(time.Now().UnixNano())
|
||||
|
||||
err := caddy.RegisterModule(caddy.Module{
|
||||
Name: "http",
|
||||
New: func() interface{} { return new(App) },
|
||||
})
|
||||
err := caddy.RegisterModule(App{})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@ -58,6 +55,14 @@ type App struct {
|
||||
ctx caddy.Context
|
||||
}
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (App) CaddyModule() caddy.ModuleInfo {
|
||||
return caddy.ModuleInfo{
|
||||
Name: "http",
|
||||
New: func() caddy.Module { return new(App) },
|
||||
}
|
||||
}
|
||||
|
||||
// Provision sets up the app.
|
||||
func (app *App) Provision(ctx caddy.Context) error {
|
||||
app.ctx = ctx
|
||||
@ -227,7 +232,7 @@ func (app *App) automaticHTTPS() error {
|
||||
// find all qualifying domain names, de-duplicated
|
||||
domainSet := make(map[string]struct{})
|
||||
for _, route := range srv.Routes {
|
||||
for _, matcherSet := range route.matcherSets {
|
||||
for _, matcherSet := range route.MatcherSets {
|
||||
for _, m := range matcherSet {
|
||||
if hm, ok := m.(*MatchHost); ok {
|
||||
for _, d := range *hm {
|
||||
@ -331,13 +336,13 @@ func (app *App) automaticHTTPS() error {
|
||||
redirTo += "{http.request.uri}"
|
||||
|
||||
redirRoutes = append(redirRoutes, Route{
|
||||
matcherSets: []MatcherSet{
|
||||
MatcherSets: []MatcherSet{
|
||||
{
|
||||
MatchProtocol("http"),
|
||||
MatchHost(domains),
|
||||
},
|
||||
},
|
||||
handlers: []MiddlewareHandler{
|
||||
Handlers: []MiddlewareHandler{
|
||||
StaticResponse{
|
||||
StatusCode: WeakString(strconv.Itoa(http.StatusTemporaryRedirect)), // TODO: use permanent redirect instead
|
||||
Headers: http.Header{
|
||||
|
Reference in New Issue
Block a user