forked from 1920853199/go-bbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·197 lines (168 loc) · 5.53 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"flag"
_ "go-bbs/routers"
"go-bbs/utils"
"go-bbs/utils/syncdb"
"os"
// "go-bbs/utils/sitemap"
"go-bbs/utils/hotnews"
"html/template"
"net/http"
_ "net/http/pprof"
"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
"github.com/robfig/cron"
"github.com/ulule/limiter/v3"
"github.com/ulule/limiter/v3/drivers/store/memory"
// "github.com/garyburd/redigo/redis"
// "fmt"
// "time"
)
func init() {
var t bool
var install bool
flag.BoolVar(&t, "t", false, "爬虫定时任务是否开启")
flag.BoolVar(&install, "install", false, "是否安装")
flag.Parse()
args := os.Args
for _, v := range args {
if v == "--install" {
syncdb.Syncdb()
os.Exit(0)
}
}
syncdb.Connect()
beego.AddFuncMap("IndexForOne", utils.IndexForOne)
beego.AddFuncMap("IndexAddOne", utils.IndexAddOne)
beego.AddFuncMap("IndexDecrOne", utils.IndexDecrOne)
beego.AddFuncMap("StringReplace", utils.StringReplace)
beego.AddFuncMap("TimeStampToTime", utils.TimeStampToTime)
beego.AddFuncMap("TemlpateTime", utils.TemlpateTime)
beego.AddFuncMap("IsOdd", func(num int) string {
if num%2 == 0 {
return "odd"
}
return "even"
})
if t == true {
//每天0点定时更新站点地图
go func() {
c := cron.New()
//*/1 0 * * *
// 0 0 * * *
// c.AddFunc("@daily", func() {
// sitemap.Sitemap("./", conf.String("url"))
// })
c.AddFunc("0 */25 * * *", func() {
hp := hotnews.Hupu{}
hp.Get("https://www.hupu.com/")
})
c.AddFunc("0 */15 * * *", func() {
w := hotnews.Weibo{}
w.Get("https://s.weibo.com/top/summary")
})
c.AddFunc("0 */30 * * *", func() {
b := hotnews.Baidu{}
b.Get("https://top.baidu.com/board?tab=realtime")
})
c.AddFunc("0 */60 * * *", func() {
k := hotnews.Kaolamedia{}
k.Get("https://www.kaolamedia.com/hot")
})
c.Start()
}()
}
// debug sql
// orm.Debug = true
}
func page_not_found(rw http.ResponseWriter, r *http.Request) {
t, _ := template.New("404.html").ParseFiles("views/home/nihongdengxia/404.html")
data := make(map[string]interface{})
data["content"] = template.HTML("404 Page Not Found")
data["url"] = "/"
t.Execute(rw, data)
}
func main() {
// mount /dev/vdb /work
// docker build -t chromegin . && docker run -p 6666:6666 -v /work/chromegin/chrome_screen_shot:/data --name chromegin chromegin
r := &utils.RateLimiter{}
rate, err := limiter.NewRateFromFormatted("5000-D")
utils.PanicOnError(err)
r.GeneralLimiter = limiter.New(memory.NewStore(), rate)
loginRate, err := limiter.NewRateFromFormatted("10-M")
utils.PanicOnError(err)
r.LoginLimiter = limiter.New(memory.NewStore(), loginRate)
//More on Beego filters here https://beego.me/docs/mvc/controller/filter.md
beego.InsertFilter("/*", beego.BeforeRouter, func(c *context.Context) {
utils.RateLimit(r, c)
}, true)
//refer to https://beego.me/docs/mvc/controller/errors.md for error handling
beego.ErrorHandler("429", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusTooManyRequests)
w.Write([]byte("Too Many Requests"))
return
})
// 黑名单
beego.ErrorHandler("403", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusTooManyRequests)
w.Write([]byte("你的IP存在可疑行为已被拉入黑名单,如有误报请联系站长([email protected])"))
return
})
beego.ErrorHandler("404", page_not_found)
//bee generate appcode -tables="cron" -driver=mysql -conn="root:root@tcp(127.0.0.1:3306)/blog" -level=3
beego.Run()
// s := utils.MusicGet(2175288)
// fmt.Printf("%v", s)
// path, err := utils.DownImage("https://img9.doubanio.com/view/subject/s/public/s3745215.jpg")
// if err != nil {
// fmt.Println(err.Error())
// }
// fmt.Println(path)
// fmt.Println(utils.TemlpateTime(time.Now()))
// fmt.Println(utils.TemlpateTime(time.Now().Add(-time.Second * 40)))
// fmt.Println(utils.TemlpateTime(time.Now().Add(-time.Second * 10)))
// fmt.Println(utils.TemlpateTime(time.Now().Add(-time.Second * 60 * 2)))
// fmt.Println(utils.TemlpateTime(time.Now().Add(-time.Hour * 2)))
// fmt.Println(utils.TemlpateTime(time.Now().Add(-time.Hour * 26)))
// fmt.Println(utils.TemlpateTime(time.Now().Add(-time.Hour * 79)))
// fmt.Println(utils.TemlpateTime(time.Now().Add(-time.Hour * 24 * 31 * 3)))
// fmt.Println(utils.TemlpateTime(time.Now().Add(-time.Hour * 24 * 365 * 2)))
// fmt.Println(utils.TemlpateTime(time.Now().Add(-time.Hour * 24 * 365 * 200)))
// bytes, err := utils.GenDefaultQRCode("Hello World")
// if err != nil {
// fmt.Println(err.Error())
// }
// err = ioutil.WriteFile("1.jpg", bytes, 0666)
// bytes, err = utils.GenDefaultQRCode("http://www.baidu.com")
// if err != nil {
// fmt.Println(err.Error())
// }
// err = ioutil.WriteFile("2.jpg", bytes, 0666)
//worker, _ := utils.NewIdWorker(1)
//ID, _ := worker.GetNextId()
// username := utils.CreateUsername()
//fmt.Println(ID)
// ctx := metadata.NewIncomingContext(context.Background(), metadata.Pairs("p-id", "P66-"))
// id := utils.NextID(ctx)
// fmt.Println(id)
// fmt.Println(utils.NextID(context.Background()))
// fmt.Println(utils.NextNumID())
// fmt.Println(utils.UUID())
//fmt.Println(utils.UUIDShort())
// utils.SendEmail(utils.Email{
// From: "[email protected]",
// To: "[email protected]",
// Header: map[string]string{
// "Subject": "霓虹灯下注册验证码",
// },
// Template: "views/home/nihongdengxia/reg-email.html",
// Data: utils.Reg{
// Tag: "注册",
// Code: "123",
// Date: time.Now().Format("2006-01-02 15:04:05"),
// },
// })
}