forked from slene/margo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
m_share.go
49 lines (40 loc) · 846 Bytes
/
m_share.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
package main
import (
"bytes"
"io/ioutil"
"net/http"
)
type mShare struct {
Src string
}
func (m mShare) Call() (interface{}, string) {
res := M{}
s := bytes.TrimSpace([]byte(m.Src))
if len(s) == 0 {
return res, "Nothing to share"
}
u := "http://play.golang.org"
body := bytes.NewBufferString(m.Src)
req, err := http.NewRequest("POST", u+"/share", body)
req.Header.Set("User-Agent", "GoSublime")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return res, err.Error()
}
defer resp.Body.Close()
s, err = ioutil.ReadAll(resp.Body)
if err != nil {
return res, err.Error()
}
res["url"] = u + "/p/" + string(s)
e := ""
if resp.StatusCode != 200 {
e = "Unexpected http status: " + resp.Status
}
return res, e
}
func init() {
registry.Register("share", func(_ *Broker) Caller {
return &mShare{}
})
}