aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--README.md6
-rw-r--r--cmd/dispatch/main.go29
-rw-r--r--config/config.go58
3 files changed, 46 insertions, 47 deletions
diff --git a/README.md b/README.md
index 79cddfb..7e8a2d4 100644
--- a/README.md
+++ b/README.md
@@ -12,7 +12,7 @@ For example,
```json
{
- "actions": {
+ "rules": {
"update Catbus UI": {
"triggers": [
{
@@ -22,7 +22,7 @@ For example,
}
}
],
- "outputs": [
+ "actions": [
{
"url": "https://build.eth.moe/deploy",
"formValues": {
@@ -40,7 +40,7 @@ For example,
}
}
],
- "outputs": [
+ "actions": [
{
"mqtt": "tcp://catbus.eth.moe/home/living-room/sofa-light/power",
"value": "on"
diff --git a/cmd/dispatch/main.go b/cmd/dispatch/main.go
index 53aec92..032797d 100644
--- a/cmd/dispatch/main.go
+++ b/cmd/dispatch/main.go
@@ -72,10 +72,12 @@ func triggerAction(config *config.Config) http.HandlerFunc {
ctx := r.Context()
found := false
- for _, actionConfig := range config.Actions {
- if matchAction(actionConfig, r) {
+ for _, rule := range config.RulesByName {
+ if matchTriggers(rule.Triggers, r) {
found = true
- runAction(ctx, actionConfig)
+ for _, action := range rule.Actions {
+ runAction(ctx, action)
+ }
}
}
if !found {
@@ -85,8 +87,8 @@ func triggerAction(config *config.Config) http.HandlerFunc {
}
}
-func matchAction(action config.Action, r *http.Request) bool {
- for _, trigger := range action.Triggers {
+func matchTriggers(triggers []config.Trigger, r *http.Request) bool {
+ for _, trigger := range triggers {
if trigger.URL.Path != r.URL.Path {
return false
}
@@ -105,16 +107,13 @@ func matchAction(action config.Action, r *http.Request) bool {
func runAction(ctx context.Context, action config.Action) {
log, _ := logger.FromContext(ctx)
- for _, output := range action.Outputs {
- if output.Kind != config.HTTPOutput {
- log.Warning("only supports HTTP for now")
- continue
- }
+ if action.Kind != config.HTTPAction {
+ log.Warning("only supports HTTP for now")
+ }
- if _, err := http.PostForm(output.URL.String(), output.FormValues); err != nil {
- log.WithError(err).Error("could not POST form")
- continue
- }
- log.Info("POSTed to URL")
+ if _, err := http.PostForm(action.URL.String(), action.FormValues); err != nil {
+ log.WithError(err).Error("could not POST form")
+ return
}
+ log.Info("POSTed to URL")
}
diff --git a/config/config.go b/config/config.go
index 38bf73c..01f6b82 100644
--- a/config/config.go
+++ b/config/config.go
@@ -12,10 +12,10 @@ import (
)
type (
- Action struct {
+ Rule struct {
Name string
Triggers []Trigger
- Outputs []Output
+ Actions []Action
}
Trigger struct {
@@ -23,9 +23,9 @@ type (
FormValues url.Values
}
- OutputKind int
- Output struct {
- Kind OutputKind
+ ActionKind int
+ Action struct {
+ Kind ActionKind
Name string
URL *url.URL
@@ -36,22 +36,22 @@ type (
}
Config struct {
- Actions map[string]Action
+ RulesByName map[string]Rule
}
config struct {
- Actions map[string]struct {
+ Rules map[string]struct {
Triggers []struct {
URL uurl `json:"url"`
FormValues map[string]string `json:"formValues"`
} `json:"triggers"`
- Outputs []struct {
+ Actions []struct {
URL uurl `json:"url"`
FormValues map[string]string `json:"formValues"`
MQTT uurl `json:"mqtt"`
Value string `json:"value"`
- } `json:"outputs"`
- } `json:"actions"`
+ } `json:"actions"`
+ } `json:"rules"`
}
uurl struct {
@@ -60,8 +60,8 @@ type (
)
const (
- HTTPOutput OutputKind = iota
- MQTTOutput
+ HTTPAction ActionKind = iota
+ MQTTAction
)
func ParseFile(path string) (*Config, error) {
@@ -80,11 +80,11 @@ func ParseFile(path string) (*Config, error) {
func configFromConfig(raw config) (*Config, error) {
c := &Config{
- Actions: map[string]Action{},
+ RulesByName: map[string]Rule{},
}
- for k, v := range raw.Actions {
- action := Action{
+ for k, v := range raw.Rules {
+ rule := Rule{
Name: k,
}
@@ -93,27 +93,27 @@ func configFromConfig(raw config) (*Config, error) {
URL: rawTrigger.URL.URL,
FormValues: urlValuesFromRawValues(rawTrigger.FormValues),
}
- action.Triggers = append(action.Triggers, trigger)
+ rule.Triggers = append(rule.Triggers, trigger)
}
- for _, rawOutput := range v.Outputs {
- output := Output{
- URL: rawOutput.URL.URL,
- FormValues: urlValuesFromRawValues(rawOutput.FormValues),
- MQTT: rawOutput.MQTT.URL,
- Value: rawOutput.Value,
+ for _, rawAction := range v.Actions {
+ action := Action{
+ URL: rawAction.URL.URL,
+ FormValues: urlValuesFromRawValues(rawAction.FormValues),
+ MQTT: rawAction.MQTT.URL,
+ Value: rawAction.Value,
}
switch {
- case output.URL != nil && output.MQTT == nil:
- output.Kind = HTTPOutput
- case output.URL == nil && output.MQTT != nil:
- output.Kind = MQTTOutput
+ case action.URL != nil && action.MQTT == nil:
+ action.Kind = HTTPAction
+ case action.URL == nil && action.MQTT != nil:
+ action.Kind = MQTTAction
default:
- return nil, errors.New("outputs must be URL xor MQTT")
+ return nil, errors.New("actions must be URL xor MQTT")
}
- action.Outputs = append(action.Outputs, output)
+ rule.Actions = append(rule.Actions, action)
}
- c.Actions[k] = action
+ c.RulesByName[k] = rule
}