-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (82 loc) · 2.49 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"flag"
"fmt"
"github.com/azzzak/alice"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"golang.org/x/sync/errgroup"
"log"
"net/http"
"os"
"yandex-dialogs/common"
"yandex-dialogs/coronavirus"
"yandex-dialogs/masha"
"yandex-dialogs/phrases_generator"
"yandex-dialogs/stalker"
"yandex-dialogs/voice_mail"
)
// 1. Implement your handler (dialog) complying this interface. Put implementation in separated folder/package.
type Dialog interface {
// Returns func which takes incoming Alice request and prepared response with filled `session` information. Response should be returned.
HandleRequest() func(request *alice.Request, response *alice.Response) *alice.Response
// Returns base path of your dialog REST API. For example, `/api/dialogs/sample-dialog`
GetPath() string
// Returns state of dialog (true - ok, false - something is wrong) and additional string message.
Health() (result bool, message string)
// Additional custom HTTP request handlers
ApiHandlers(router *mux.Router)
}
var (
serveHost = flag.String("serve_host", common.GetEnv("SERVER_HOST", ""),
"Host to serve requests incoming to server")
servePort = flag.String("serve_port", common.GetEnv("PORT", "8080"),
"Port to serve requests incoming to server")
g errgroup.Group
)
// 2. Just add your implementation here
func buildHandlers() []Dialog {
var dialogs []Dialog
dialogs = append(dialogs, phrases_generator.NewDialog())
dialogs = append(dialogs, voice_mail.NewVoiceMail())
dialogs = append(dialogs, masha.NewMasha(2900))
dialogs = append(dialogs, coronavirus.NewCoronavirus())
dialogs = append(dialogs, stalker.NewStalker())
return dialogs
}
func main() {
mainEndpoints := &http.Server{
Addr: fmt.Sprintf("%s:%s", *serveHost, *servePort),
Handler: handler(),
}
g.Go(func() error {
return mainEndpoints.ListenAndServe()
})
if err := g.Wait(); err != nil {
log.Fatal(err)
}
}
func handler() http.Handler {
r := mux.NewRouter()
handler := common.Handler()
dialogs := buildHandlers()
for _, v := range dialogs {
r.Handle(v.GetPath(),
handlers.LoggingHandler(
os.Stdout,
handler(handleRequest(v.HandleRequest()))),
).Methods("POST", "OPTIONS")
v.ApiHandlers(r)
}
r.Handle("/health",
handlers.LoggingHandler(
os.Stdout,
handler(handleHealthRequest(dialogs))),
).Methods("GET")
r.Handle("/statistics",
handlers.LoggingHandler(
os.Stdout,
handler(handleStatisticsRequest())),
).Methods("GET")
return JsonContentType(handlers.CompressHandler(r))
}