-
Notifications
You must be signed in to change notification settings - Fork 47
/
main.go
103 lines (94 loc) · 2.38 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
package main
import (
"encoding/json"
"flag"
"fmt"
"net/http"
"net/url"
"os"
"strconv"
"sync"
)
const (
cacheDB = 2 // 存储数据的redis db
cachePrefix = "gonews" // 缓存前缀
)
var dir string
var act string
var port int
var pullInterval int64 // 自动拉取间隔
var wg sync.WaitGroup
func init() {
flag.StringVar(&dir, "d", "", "the path of news to parse")
flag.StringVar(&act, "a", "cache", "the action to run service, values 'api' or 'cache'")
flag.IntVar(&port, "p", 8017, "the port to listen for api service")
flag.Int64Var(&pullInterval, "pull", 60, "the time interval (minutes) to pull data source, ")
}
func main() {
flag.Parse()
if act == "api" { // web服务
http.HandleFunc("/", showApis)
http.HandleFunc("/news", getNewsApi) // 获取新闻数据api
err := http.ListenAndServe(":"+strconv.Itoa(port), nil)
if err != nil {
fmt.Fprintf(os.Stderr, "ListenAndServe error:%s", err)
os.Exit(1)
}
fmt.Fprintf(os.Stdout, "%s", "Success to run api service")
} else { // 缓存数据操作
initDataPuller(dir, pullInterval)
select {}
}
}
// 并发缓存数据
func cacheNews(path string) {
defer wg.Done()
news := getNews(path)
for _, item := range news {
cache := map[string]interface{}{
"id": strconv.Itoa(int(item.ID)),
"title": item.Title,
"link": item.Link.String(),
"ctime": item.Ctime.Format("20060102"),
}
setNewsCache(cache) // 缓存数据
}
}
// 首页展示api列表
func showApis(w http.ResponseWriter, req *http.Request) {
w.Write([]byte(`{"apis":["/news"]}`))
}
// 获取新闻的api
func getNewsApi(w http.ResponseWriter, req *http.Request) {
page := int64(1)
per := int64(50)
keyword := ""
query, err := url.ParseQuery(req.URL.RawQuery)
if err == nil {
if len(query["page"]) > 0 && query["page"][0] != "" {
iPage, _ := strconv.Atoi(query["page"][0])
page = int64(iPage)
}
if len(query["per"]) > 0 && query["per"][0] != "" {
iPer, _ := strconv.Atoi(query["per"][0])
per = int64(iPer)
}
if len(query["keyword"]) > 0 && query["keyword"][0] != "" {
keyword = query["keyword"][0]
}
}
var news []map[string]string
var count int64 = 0
if keyword != "" {
news, count = searchNews(keyword, page, per)
} else {
news, count = getAllNews(page, per)
}
data := map[string]interface{}{
"total": count,
"per": per,
"items": news,
}
jData, _ := json.Marshal(data)
w.Write(jData)
}