summaryrefslogtreecommitdiff
path: root/input/http
diff options
context:
space:
mode:
authorSilvan Jegen <s.jegen@gmail.com>2017-01-20 21:03:08 +0100
committerSilvan Jegen <s.jegen@gmail.com>2017-01-20 21:03:08 +0100
commit5c24009f2b1fd5a7e5abd4dc33ebdec0c6eaaf25 (patch)
tree7447a384efe6a5b45106fde621e53bdf38ad608a /input/http
parentba15251035961020363fa5aff95ff0a361023af9 (diff)
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.
Diffstat (limited to 'input/http')
-rw-r--r--input/http/http.go61
1 files changed, 61 insertions, 0 deletions
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
+}