/
main.go
137 lines (107 loc) · 2.97 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
// 提供簡單的日期查詢 API Server.
//
/*
主要支援動態更新 CSV 檔案讀取,解決非預定開休市狀況(如:颱風假)
Install:
go install github.com/toomore/gogrs/cmd/tradingdays_server
Usage:
tradingdays_server [flags]
The flags are:
-http
HTTP service address (default ':59123')
-csvcachetime
CSV cache time.(default: 21600)
URL Path:
/open?q={timestamp}
回傳 JSON 格式
{
"date": "2015-04-24T15:14:52Z",
"open": true
}
範例:
http://gogrs-trd.toomore.net/open?q=1429888492
*/
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"strconv"
"time"
"github.com/toomore/gogrs/tradingdays"
)
// Log is show viwer log.
func Log(req *http.Request) {
var userIP string
if userIP = req.Header.Get("X-FORWARDED-FOR"); userIP == "" {
userIP = req.RemoteAddr
}
log.Println(req.URL, userIP, req.UserAgent(), req.Form, req.Referer())
}
// Home is home page.
func Home(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte("查詢台灣股市是否開市<br>"))
w.Write([]byte(fmt.Sprintf("<a href=\"/open?q=%d\">範例</a><br>", time.Now().Unix())))
w.Write([]byte("[<a href=\"https://godoc.org/github.com/toomore/gogrs/tradingdays\">Docs</a>] [<a href=\"https://github.com/toomore/gogrs/blob/master/cmd/tradingdays_server/main.go\">github</a>]<br>"))
Log(req)
}
type tradeJSON struct {
Date time.Time `json:"date"`
Open bool `json:"open"`
}
type errorJSON struct {
Error string `json:"error"`
}
// TradeOpen is "./open" page.
func TradeOpen(w http.ResponseWriter, req *http.Request) {
var jsonStr []byte
if data, err := strconv.ParseInt(req.FormValue("q"), 10, 64); err != nil {
jsonStr, _ = json.Marshal(&errorJSON{Error: "Wrong date format"})
} else {
if csvcachetime.InCache(defaultcachetime) != true {
tradingdays.DownloadCSV(true)
csvcachetime.Set()
log.Println("DownloadCSV.")
}
date := time.Unix(data, 0)
jsonStr, _ = json.Marshal(&tradeJSON{
Date: date.UTC(),
Open: tradingdays.IsOpen(date.Year(), date.Month(), date.Day())})
}
w.Header().Set("Content-Type", "application/json")
w.Write(jsonStr)
Log(req)
}
type cachetime struct {
timestamp int64
}
func (c *cachetime) Set() {
c.timestamp = time.Now().Unix()
}
func (c *cachetime) InCache(seconds int64) bool {
result := (time.Now().Unix() - c.timestamp) <= seconds
if result != true {
c.timestamp = 0
}
return result
}
var csvcachetime cachetime
var httpport string
var defaultcachetime int64
func init() {
tradingdays.DownloadCSV(true)
log.Println("Init DownloadCSV.")
csvcachetime.Set()
flag.StringVar(&httpport, "http", ":59123", "HTTP service address (e.g., ':59123')")
flag.Int64Var(&defaultcachetime, "csvcachetime", 21600, "CSV cache time.")
}
func main() {
flag.Parse()
log.Println("http:", httpport, "csvcachetime:", defaultcachetime)
http.HandleFunc("/", Home)
http.HandleFunc("/open", TradeOpen)
log.Fatal(http.ListenAndServe(httpport, nil))
}