From 5c24009f2b1fd5a7e5abd4dc33ebdec0c6eaaf25 Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Fri, 20 Jan 2017 21:03:08 +0100 Subject: Introduce registry We introduce a registry that contains maps to builder functions. These builder functions return the interface implementation of the modules specified in the configuration. We also make the input module type use the registry. All other module types still have to be converted. --- conf/parser.go | 7 ++++++ input/http.go | 45 ------------------------------------ input/http/http.go | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ input/inputinterface.go | 7 ++++++ input/interface.go | 7 ------ input/stdin.go | 40 -------------------------------- input/stdin/stdin.go | 46 +++++++++++++++++++++++++++++++++++++ main.go | 4 ++-- registry/registry.go | 20 ++++++++++++++++ 9 files changed, 143 insertions(+), 94 deletions(-) delete mode 100644 input/http.go create mode 100644 input/http/http.go create mode 100644 input/inputinterface.go delete mode 100644 input/interface.go delete mode 100644 input/stdin.go create mode 100644 input/stdin/stdin.go create mode 100644 registry/registry.go diff --git a/conf/parser.go b/conf/parser.go index 6aca3aa..bdec888 100644 --- a/conf/parser.go +++ b/conf/parser.go @@ -19,6 +19,8 @@ import ( "fmt" "io" "os" + + "github.com/Shugyousha/stasher/registry" ) // Having a Config to Manager function could be nice? Or we could just @@ -55,6 +57,11 @@ func NewConfig(r io.Reader) *Config { func (p *parser) startparsing() { inputmdesc := p.module("input") fmt.Fprintf(os.Stderr, "input moduledesc: %#v\n", inputmdesc) + inputfunc, ok := registry.Inputregistry[inputmdesc.name] + if !ok { + fmt.Fprintf(os.Stderr, "input module is not known: %q\n", inputmdesc.name) + } + inputfunc(nil) filtermdesc := p.module("filter") fmt.Fprintf(os.Stderr, "filter moduledesc: %#v\n", filtermdesc) diff --git a/input/http.go b/input/http.go deleted file mode 100644 index 285f1f6..0000000 --- a/input/http.go +++ /dev/null @@ -1,45 +0,0 @@ -package input - -import ( - "fmt" - "io/ioutil" - "net/http" - - "github.com/Shugyousha/stasher/work" -) - -type HTTPInput struct { - retchan chan *work.Work - prefix string - port string -} - -func NewHTTPInput(prefix, port string) *HTTPInput { - return &HTTPInput{prefix: prefix, port: port} -} - -func (hi *HTTPInput) httphandler(w http.ResponseWriter, r *http.Request) { - if r.Method != "POST" { - fmt.Printf("Expected POST method was: %q\n", r.Method) - return - } - all, err := ioutil.ReadAll(r.Body) - if err != nil { - fmt.Printf("Error when reading HTTP request body: %q\n", err) - return - } - hi.retchan <- &work.Work{Data: all} -} - -func (hi *HTTPInput) Start() chan *work.Work { - hi.retchan = make(chan *work.Work, 100) - - go func() { - http.HandleFunc("/"+hi.prefix, hi.httphandler) - err := http.ListenAndServe(hi.port, nil) - fmt.Printf("Error when serving HTTP: %q\n", err) - close(hi.retchan) - }() - - return hi.retchan -} diff --git a/input/http/http.go b/input/http/http.go new file mode 100644 index 0000000..71a0340 --- /dev/null +++ b/input/http/http.go @@ -0,0 +1,61 @@ +package http + +import ( + "fmt" + "io/ioutil" + "net/http" + + "github.com/Shugyousha/stasher/input" + "github.com/Shugyousha/stasher/registry" + "github.com/Shugyousha/stasher/work" +) + +func init() { + registry.Inputregistry["http"] = New +} + +type HTTPInput struct { + retchan chan *work.Work + prefix string + port string +} + +func New(conf map[string]string) input.Input { + prefix := conf["prefix"] + if prefix == "" { + fmt.Printf("Need a prefix when setting up http input\n") + return nil + } + port := conf["port"] + if port == "" { + fmt.Printf("Need a port number when setting up http input\n") + return nil + } + return &HTTPInput{prefix: prefix, port: port} +} + +func (hi *HTTPInput) httphandler(w http.ResponseWriter, r *http.Request) { + if r.Method != "POST" { + fmt.Printf("Expected POST method was: %q\n", r.Method) + return + } + all, err := ioutil.ReadAll(r.Body) + if err != nil { + fmt.Printf("Error when reading HTTP request body: %q\n", err) + return + } + hi.retchan <- &work.Work{Data: all} +} + +func (hi *HTTPInput) Start() chan *work.Work { + hi.retchan = make(chan *work.Work, 100) + + go func() { + http.HandleFunc("/"+hi.prefix, hi.httphandler) + err := http.ListenAndServe(hi.port, nil) + fmt.Printf("Error when serving HTTP: %q\n", err) + close(hi.retchan) + }() + + return hi.retchan +} diff --git a/input/inputinterface.go b/input/inputinterface.go new file mode 100644 index 0000000..1743249 --- /dev/null +++ b/input/inputinterface.go @@ -0,0 +1,7 @@ +package input + +import "github.com/Shugyousha/stasher/work" + +type Input interface { + Start() chan *work.Work +} diff --git a/input/interface.go b/input/interface.go deleted file mode 100644 index 1743249..0000000 --- a/input/interface.go +++ /dev/null @@ -1,7 +0,0 @@ -package input - -import "github.com/Shugyousha/stasher/work" - -type Input interface { - Start() chan *work.Work -} diff --git a/input/stdin.go b/input/stdin.go deleted file mode 100644 index 25840ef..0000000 --- a/input/stdin.go +++ /dev/null @@ -1,40 +0,0 @@ -package input - -import ( - "bufio" - "fmt" - "io" - "os" - - "github.com/Shugyousha/stasher/work" -) - -type StdinInput struct { - retchan chan *work.Work -} - -func NewStdin() *StdinInput { - return &StdinInput{} -} - -func (i *StdinInput) Start() chan *work.Work { - i.retchan = make(chan *work.Work, 100) - r := bufio.NewReader(os.Stdin) - - go func() { - for { - bs, err := r.ReadBytes(byte('\n')) - if err == io.EOF { - break - } - if err != nil { - fmt.Printf("Error when reading input from Stdin: %q", err) - os.Exit(1) - } - i.retchan <- &work.Work{Data: bs} - } - close(i.retchan) - }() - - return i.retchan -} diff --git a/input/stdin/stdin.go b/input/stdin/stdin.go new file mode 100644 index 0000000..fa4d21f --- /dev/null +++ b/input/stdin/stdin.go @@ -0,0 +1,46 @@ +package stdin + +import ( + "bufio" + "fmt" + "io" + "os" + + "github.com/Shugyousha/stasher/input" + "github.com/Shugyousha/stasher/registry" + "github.com/Shugyousha/stasher/work" +) + +func init() { + registry.Inputregistry["input"] = New +} + +type StdinInput struct { + retchan chan *work.Work +} + +func New(map[string]string) input.Input { + return &StdinInput{} +} + +func (i *StdinInput) Start() chan *work.Work { + i.retchan = make(chan *work.Work, 100) + r := bufio.NewReader(os.Stdin) + + go func() { + for { + bs, err := r.ReadBytes(byte('\n')) + if err == io.EOF { + break + } + if err != nil { + fmt.Printf("Error when reading input from Stdin: %q", err) + os.Exit(1) + } + i.retchan <- &work.Work{Data: bs} + } + close(i.retchan) + }() + + return i.retchan +} diff --git a/main.go b/main.go index 7a3bb88..a52b5ac 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,7 @@ import ( "github.com/Shugyousha/stasher/conf" "github.com/Shugyousha/stasher/filter" - "github.com/Shugyousha/stasher/input" + "github.com/Shugyousha/stasher/input/stdin" "github.com/Shugyousha/stasher/output" ) @@ -26,7 +26,7 @@ func main() { ffmap["F"] = func(s string) string { return strings.ToUpper(s) } m := Manager{ - Input: input.NewStdin(), + Input: stdin.New(nil), Filter: filter.NewStringFilter(ffmap), Output: &output.StdoutOutput{}, } diff --git a/registry/registry.go b/registry/registry.go new file mode 100644 index 0000000..5b285b8 --- /dev/null +++ b/registry/registry.go @@ -0,0 +1,20 @@ +package registry + +import ( + "github.com/Shugyousha/stasher/input" + "github.com/Shugyousha/stasher/work" +) + +var ( + Inputregistry map[string]func(map[string]string) input.Input + Filterregistry map[string]func(map[string]string) Filter + Outputregistry map[string]func(map[string]string) Output +) + +type Filter interface { + Filter(*work.Work) *work.Work +} + +type Output interface { + Output(*work.Work) +} -- cgit v1.2.3