forked from danryan/hal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (61 loc) · 1.5 KB
/
main.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
64
65
66
67
68
69
70
71
72
73
package main
import (
"github.com/jobandtalent/hal"
_ "github.com/jobandtalent/hal/adapter/shell"
"github.com/jobandtalent/hal/handler"
_ "github.com/jobandtalent/hal/store/memory"
"os"
)
// HAL is just another Go package, which means you are free to organize things
// however you deem best.
// You can define your handlers in the same file...
var pingHandler = hal.Hear(`ping`, func(res *hal.Response) error {
return res.Send("PONG")
})
func run() int {
robot, err := hal.NewRobot()
if err != nil {
hal.Logger.Error(err)
return 1
}
// Or define them inside another function...
fooHandler := hal.Respond(`foo`, func(res *hal.Response) error {
return res.Send("BAR")
})
tableFlipHandler := &hal.Handler{
Method: hal.HEAR,
Pattern: `tableflip`,
Run: func(res *hal.Response) error {
return res.Send(`(╯°□°)╯︵ ┻━┻`)
},
}
robot.Handle(
pingHandler,
fooHandler,
tableFlipHandler,
// Or stick them in an entirely different package, and reference them
// exactly in the way you would expect.
handler.Ping,
// Or use a hal.Handler structure complete with usage...
&hal.Handler{
Method: hal.RESPOND,
Pattern: `SYN`,
Usage: `hal syn - replies with "ACK"`,
Run: func(res *hal.Response) error {
return res.Reply("ACK")
},
},
// Or even inline!
hal.Hear(`yo`, func(res *hal.Response) error {
return res.Send("lo")
}),
)
if err := robot.Run(); err != nil {
hal.Logger.Error(err)
return 1
}
return 0
}
func main() {
os.Exit(run())
}