-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_config.go
82 lines (69 loc) · 2.1 KB
/
handler_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package microfile
import (
"fmt"
"path/filepath"
"microfile/config"
"microfile/handler"
)
// this would change as more action types are added
var handlerNames = [3]string{"http", "log", "imaginary"}
type HandlerFinder interface {
HandlersFor(path string, config config.Config) []handler.Handler
}
type HandlerConfig struct {
}
func (handlerConfig *HandlerConfig) Validate(config config.Config) error {
numberOfInvalidActions := 0
for _, w := range config.Watches {
for _, a := range w.Actions {
if validHandlerName(a.Type) == false {
numberOfInvalidActions++
}
}
}
if numberOfInvalidActions > 0 {
return fmt.Errorf("Invalid action detected in config. Valid actions are %v", handlerNames)
}
return nil
}
func (handlerConfig *HandlerConfig) HandlersFor(path string, config config.Config) []handler.Handler {
// why do we need to do this? Well, we're probably going to be receiving
// a path from a filesystem event like /tmp/foo/file.txt but we are watching
// /tmp/foo right? So we need to be looking up the handler for the directory basename.
// TODO: this is probably going to be a huge headache when we go recursive? Or maybe not
// if we recurse ourselves and just register all the child paths as watches?
dir := filepath.Dir(path)
handlers := []handler.Handler{}
for _, watch := range config.Watches {
if watch.Path == dir {
for _, action := range watch.Actions {
// this would change as more action types are added
switch action.Type {
case "http":
handler := &handler.HTTP{DefaultURL: config.DefaultURL}
if action.Options["send_file"] == "true" {
handler.SendContents = true
}
handler.Init(action)
handlers = append(handlers, handler)
case "imaginary":
handler := &handler.Imaginary{DefaultURL: config.DefaultURL}
handler.Init(action)
handlers = append(handlers, handler)
case "log":
handler := &handler.Log{}
handlers = append(handlers, handler)
}
}
}
}
return handlers
}
func validHandlerName(name string) bool {
for _, n := range handlerNames {
if n == name {
return true
}
}
return false
}