-
Notifications
You must be signed in to change notification settings - Fork 58
/
main.go
166 lines (139 loc) · 4.15 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
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
163
164
165
166
package main
import (
"context"
"net/http"
"os"
"os/signal"
"time"
log "github.com/Ptt-Alertor/logrus"
"github.com/google/gops/agent"
"github.com/julienschmidt/httprouter"
"github.com/robfig/cron"
"github.com/Ptt-Alertor/ptt-alertor/channels/line"
"github.com/Ptt-Alertor/ptt-alertor/channels/messenger"
"github.com/Ptt-Alertor/ptt-alertor/channels/telegram"
ctrlr "github.com/Ptt-Alertor/ptt-alertor/controllers"
"github.com/Ptt-Alertor/ptt-alertor/jobs"
)
var (
telegramToken = os.Getenv("TELEGRAM_TOKEN")
authUser = os.Getenv("AUTH_USER")
authPassword = os.Getenv("AUTH_PW")
)
type myRouter struct {
httprouter.Router
}
func (mr myRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.WithFields(log.Fields{
"method": r.Method,
"IP": r.RemoteAddr,
"URI": r.URL.Path,
}).Info("visit")
mr.Router.ServeHTTP(w, r)
}
func newRouter() *myRouter {
r := &myRouter{
Router: *httprouter.New(),
}
r.NotFound = http.FileServer(http.Dir("public"))
return r
}
func basicAuth(handle httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
user, password, hasAuth := r.BasicAuth()
if hasAuth && user == authUser && password == authPassword {
handle(w, r, params)
} else {
w.Header().Set("WWW-Authenticate", "Basic realm=Restricted")
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
}
}
}
func main() {
log.Info("Start Jobs")
startJobs()
router := newRouter()
m := messenger.New()
router.GET("/", ctrlr.Index)
router.GET("/messenger", ctrlr.MessengerIndex)
router.GET("/line", ctrlr.LineIndex)
router.GET("/telegram", ctrlr.TelegramIndex)
router.GET("/redirect/:checksum", ctrlr.Redirect)
router.GET("/top", ctrlr.Top)
router.GET("/docs", ctrlr.Docs)
// websocket
router.GET("/ws", ctrlr.WebSocket)
router.POST("/broadcast", basicAuth(ctrlr.Broadcast))
// boards apis
router.GET("/boards/:boardName/articles/:code", ctrlr.BoardArticle)
router.GET("/boards/:boardName/articles", ctrlr.BoardArticleIndex)
router.GET("/boards", ctrlr.BoardIndex)
// keyword apis
router.GET("/keyword/boards", ctrlr.KeywordBoards)
// author apis
router.GET("/author/boards", ctrlr.AuthorBoards)
// pushsum apis
router.GET("/pushsum/boards", ctrlr.PushSumBoards)
// articles apis
router.GET("/articles", ctrlr.ArticleIndex)
// users apis
router.GET("/users/:account", basicAuth(ctrlr.UserFind))
router.GET("/users", basicAuth(ctrlr.UserAll))
router.POST("/users", basicAuth(ctrlr.UserCreate))
router.PUT("/users/:account", basicAuth(ctrlr.UserModify))
// line
router.POST("/line/callback", line.HandleRequest)
router.POST("/line/notify/callback", line.CatchCallback)
// facebook messenger
router.GET("/messenger/webhook", m.Verify)
router.POST("/messenger/webhook", m.Received)
// telegram
router.POST("/telegram/"+telegramToken, telegram.HandleRequest)
// gops agent
if err := agent.Listen(agent.Options{Addr: ":6060", ShutdownCleanup: true}); err != nil {
log.Fatal(err)
}
// Web Server
log.Info("Web Server Start on Port 9090")
srv := http.Server{
Addr: ":9090",
Handler: router,
}
go func() {
if err := srv.ListenAndServe(); err != nil {
log.Fatal("ListenAndServer", err)
}
}()
// graceful shutdown
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
<-quit
log.Info("Shutdown Web Server...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.WithError(err).Fatal("Web Server Showdown Failed")
}
log.Info("Web Server Was Been Shutdown")
}
func startJobs() {
go jobs.NewChecker().Run()
go jobs.NewPushSumChecker().Run()
go jobs.NewCommentChecker().Run()
go jobs.NewPttMonitor().Run()
c := cron.New()
c.AddJob("@hourly", jobs.NewTop())
c.AddJob("@every 48h", jobs.NewPushSumKeyReplacer())
c.Start()
}
func init() {
// for initial app
// jobs.NewPushSumKeyReplacer().Run()
// jobs.NewMigrateBoard(map[string]string{}).Run()
// jobs.NewTop().Run()
// jobs.NewCacheCleaner().Run()
// jobs.NewGenerator().Run()
// jobs.NewFetcher().Run()
// jobs.NewMigrateDB().Run()
// jobs.NewCategoryCleaner().Run()
}