Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 01:00 UTC
| 1 | // Copyright 2015 Matthew Holt and The Caddy Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | // Package eventsconfig is for configuring caddyevents.App with the |
| 16 | // Caddyfile. This code can't be in the caddyevents package because |
| 17 | // the httpcaddyfile package imports caddyhttp, which imports |
| 18 | // caddyevents: hence, it creates an import cycle. |
| 19 | package eventsconfig |
| 20 | |
| 21 | import ( |
| 22 | "encoding/json" |
| 23 | |
| 24 | "github.com/caddyserver/caddy/v2/caddyconfig" |
| 25 | "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" |
| 26 | "github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile" |
| 27 | "github.com/caddyserver/caddy/v2/modules/caddyevents" |
| 28 | ) |
| 29 | |
| 30 | func init() { |
| 31 | httpcaddyfile.RegisterGlobalOption("events", parseApp) |
| 32 | } |
| 33 | |
| 34 | // parseApp configures the "events" global option from Caddyfile to set up the events app. |
| 35 | // Syntax: |
| 36 | // |
| 37 | // events { |
| 38 | // on <event> <handler_module...> |
| 39 | // } |
| 40 | // |
| 41 | // If <event> is *, then it will bind to all events. |
| 42 | func parseApp(d *caddyfile.Dispenser, _ any) (any, error) { |
| 43 | d.Next() // consume option name |
| 44 | app := new(caddyevents.App) |
| 45 | for d.NextBlock(0) { |
| 46 | switch d.Val() { |
| 47 | case "on": |
| 48 | if !d.NextArg() { |
| 49 | return nil, d.ArgErr() |
| 50 | } |
| 51 | eventName := d.Val() |
| 52 | if eventName == "*" { |
| 53 | eventName = "" |
| 54 | } |
| 55 | |
| 56 | if !d.NextArg() { |
| 57 | return nil, d.ArgErr() |
| 58 | } |
| 59 | handlerName := d.Val() |
| 60 | modID := "events.handlers." + handlerName |
| 61 | unm, err := caddyfile.UnmarshalModule(d, modID) |
| 62 | if err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | |
| 66 | app.Subscriptions = append(app.Subscriptions, &caddyevents.Subscription{ |
| 67 | Events: []string{eventName}, |
| 68 | HandlersRaw: []json.RawMessage{ |
| 69 | caddyconfig.JSONModuleObject(unm, "handler", handlerName, nil), |
| 70 | }, |
| 71 | }) |
| 72 | |
| 73 | default: |
| 74 | return nil, d.ArgErr() |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return httpcaddyfile.App{ |
| 79 | Name: "events", |
| 80 | Value: caddyconfig.JSON(app, nil), |
| 81 | }, nil |
| 82 | } |