forked from kentaro/takosan
/
httpd.go
63 lines (56 loc) · 1.63 KB
/
httpd.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
package main
import (
"fmt"
"github.com/go-martini/martini"
"github.com/martini-contrib/binding"
"log"
)
type Httpd struct {
Host string
Port int
}
type Param struct {
Channel string `form:"channel" binding:"required"`
Message string `form:"message"`
Name string `form:"name"`
Icon string `form:"icon"`
Fallback string `form:"fallback"`
Color string `form:"color"`
Pretext string `form:"pretext"`
AuthorName string `form:"author_name"`
AuthorLink string `form:"author_link"`
AuthorIcon string `form:"author_icon"`
Title string `form:"title"`
TitleLink string `form:"title_link"`
Text string `form:"text"`
FieldTitle []string `form:"field_title[]"`
FieldValue []string `form:"field_value[]"`
FieldShort []bool `form:"field_short[]"`
ImageURL string `form:"image_url"`
Manual bool `form:"manual"`
}
func NewHttpd(host string, port int) *Httpd {
return &Httpd{
Host: host,
Port: port,
}
}
func (h *Httpd) Run() {
m := martini.Classic()
m.Get("/", func() string { return "Hello, I'm Takosan!!1" })
m.Post("/notice", binding.Bind(Param{}), messageHandler)
m.Post("/privmsg", binding.Bind(Param{}), messageHandler)
m.RunOnAddr(fmt.Sprintf("%s:%d", h.Host, h.Port))
}
func messageHandler(p Param) (int, string) {
ch := make(chan error, 1)
go MessageBus.Publish(NewMessage(p, ch))
err := <-ch
if err != nil {
message := fmt.Sprintf("Failed to send message to %s: %s\n", p.Channel, err)
log.Printf(fmt.Sprintf("[error] %s", message))
return 400, message
} else {
return 200, fmt.Sprintf("Message sent successfully to %s", p.Channel)
}
}