summaryrefslogtreecommitdiff
path: root/input
diff options
context:
space:
mode:
authorSilvan Jegen <s.jegen@gmail.com>2016-12-09 21:12:18 +0100
committerSilvan Jegen <s.jegen@gmail.com>2016-12-09 21:12:18 +0100
commit26412f3077698dabcdbdf6a6e40276c56a6f74b5 (patch)
treec10ed9b5dbe3612da22247ec6c5afcd1879495eb /input
parent70299d51aeb8dc53a15eb06d46d4d2419619fd47 (diff)
Reorganize files
Diffstat (limited to 'input')
-rw-r--r--input/http.go45
-rw-r--r--input/stdin.go36
2 files changed, 81 insertions, 0 deletions
diff --git a/input/http.go b/input/http.go
new file mode 100644
index 0000000..285f1f6
--- /dev/null
+++ b/input/http.go
@@ -0,0 +1,45 @@
+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/stdin.go b/input/stdin.go
new file mode 100644
index 0000000..5acfa5e
--- /dev/null
+++ b/input/stdin.go
@@ -0,0 +1,36 @@
+package input
+
+import (
+ "bufio"
+ "fmt"
+ "io"
+ "os"
+
+ "github.com/Shugyousha/stasher/work"
+)
+
+type StdinInput struct {
+ retchan chan *work.Work
+}
+
+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
+}