-
Notifications
You must be signed in to change notification settings - Fork 24
/
server.go
162 lines (131 loc) · 4.93 KB
/
server.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2018 The Pouch Robot Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"io/ioutil"
"net/http"
"strings"
"github.com/pouchcontainer/pouchrobot/ci"
"github.com/pouchcontainer/pouchrobot/config"
"github.com/pouchcontainer/pouchrobot/docgenerator"
"github.com/pouchcontainer/pouchrobot/fetcher"
"github.com/pouchcontainer/pouchrobot/gh"
"github.com/pouchcontainer/pouchrobot/processor"
"github.com/pouchcontainer/pouchrobot/reporter"
"github.com/pouchcontainer/pouchrobot/utils/translators"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
)
// DefaultAddress is the default address daemon will listen to.
var DefaultAddress = ":6789"
// Server refers to a daemon server interating with github repos.
type Server struct {
// listenAddress is the address which is used to accepting requests.
listenAddress string
// processor processes webhook event from GitHub.
processor *processor.Processor
// fetcher does periodical work to check repo's status on GitHub.
fetcher *fetcher.Fetcher
// ciNotifier handles ci system webhook.
ciNotifier *ci.Notifier
// reporter reports weekly update of repository.
reporter *reporter.Reporter
// docGenerator auto generates docs for repo.
docGenerator *docgenerator.Generator
}
// NewServer constructs a brand new robot server
func NewServer(config config.Config) (*Server, error) {
ghClient := gh.NewClient(config.Owner, config.Repo, config.AccessToken)
translator := translators.NewBaiduTranslator(translators.BaiduTranslatorOptions{
Appid: config.TranslatorConfig.BaiduConfig.AppID,
Key: config.TranslatorConfig.BaiduConfig.Key,
})
docGenerator, err := docgenerator.New(ghClient,
config.Owner, config.Repo,
config.DocGenerateConfig.RootDir, config.DocGenerateConfig.SwaggerPath, config.DocGenerateConfig.APIDocPath,
config.DocGenerateConfig.GenerationHour,
config.DocGenerateConfig.CliDocGeneratorCmd,
)
if err != nil {
return nil, err
}
return &Server{
listenAddress: config.HTTPListen,
processor: processor.New(ghClient, translator, config.Owner, config.Repo),
fetcher: fetcher.New(ghClient, config.FetcherConfig.CommitsGap),
ciNotifier: ci.New(ghClient, config.Owner, config.Repo),
reporter: reporter.New(ghClient, config.WeeklyReportConfig.ReportDay, config.WeeklyReportConfig.ReportHour),
docGenerator: docGenerator,
}, nil
}
// Run runs the server.
func (s *Server) Run() error {
// start fetcher, reporter and doc generator in goroutines
go s.fetcher.Run()
go s.reporter.Run()
go s.docGenerator.Run()
// start webserver
listenAddress := s.listenAddress
if listenAddress == "" {
listenAddress = DefaultAddress
}
r := mux.NewRouter()
// register ping api
r.HandleFunc("/_ping", pingHandler).Methods("GET")
// github webhook API
r.HandleFunc("/events", s.gitHubEventHandler).Methods("POST")
// travisCI webhook API
r.HandleFunc("/ci_notifications", s.ciNotificationHandler).Methods("POST")
logrus.Infof("start http server on address %s", listenAddress)
return http.ListenAndServe(listenAddress, r)
}
// pingHandler handles ping request to return health of server.
func pingHandler(w http.ResponseWriter, r *http.Request) {
logrus.Debug("/_ping request received")
w.WriteHeader(http.StatusOK)
w.Write([]byte{'O', 'K'})
}
// gitHubEventHandler handles webhook events from github.
func (s *Server) gitHubEventHandler(w http.ResponseWriter, r *http.Request) {
logrus.Debug("/events request received")
eventType := r.Header.Get("X-Github-Event")
data, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
r.Body.Close()
if err := s.processor.HandleEvent(eventType, data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
// ciNotificationHandler handles webhook events from CI system.
func (s *Server) ciNotificationHandler(w http.ResponseWriter, r *http.Request) {
logrus.Info("/ci_notifications events reveived")
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rawStr := r.PostForm.Get("payload")
logrus.Debugf("r.PostForm[payload]: %v", rawStr)
jsonStr := strings.Replace(rawStr, `\"`, `"`, -1)
if err := s.ciNotifier.Process(jsonStr); err != nil {
logrus.Errorf("failed to process ci notification: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}