-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
88 lines (75 loc) · 2.21 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
package controllers
import (
"encoding/json"
"fmt"
"html/template"
"log"
"main/app/models"
"main/config"
"net/http"
"regexp"
"strconv"
)
var templates = template.Must(template.ParseFiles("app/views/google.html"))
func viewChartHandler(w http.ResponseWriter, r *http.Request) {
limit := 100
duration := "1s"
durationTime := config.Config.Durations[duration]
df, _ := models.GetAllCandle(config.Config.ProductCode, durationTime, limit)
err := templates.ExecuteTemplate(w, "google.html", df.Candles)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
type JSONError struct {
Error string `json:"error"`
Code int `json:"code"`
}
func APIError(w http.ResponseWriter, errMessage string, code int) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
jsonError, err := json.Marshal(JSONError{Error: errMessage, Code: code})
if err != nil {
log.Fatal(err)
}
w.Write(jsonError)
}
var apiValidPath = regexp.MustCompile("^/api/candle/$")
func apiMakeHandler(fn func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
m := apiValidPath.FindStringSubmatch(r.URL.Path)
if len(m) == 0 {
APIError(w, "Not found", http.StatusNotFound)
}
fn(w, r)
}
}
func apiCandleHandler(w http.ResponseWriter, r *http.Request) {
productCode := r.URL.Query().Get("product_code")
if productCode == "" {
APIError(w, "No product_code param", http.StatusBadRequest)
return
}
strLimit := r.URL.Query().Get("limit")
limit, err := strconv.Atoi(strLimit)
if strLimit == "" || err != nil || limit < 0 || limit > 1000 {
limit = 1000
}
duration := r.URL.Query().Get("duration")
if duration == "" {
duration = "1m"
}
durationTime := config.Config.Durations[duration]
df, _ := models.GetAllCandle(productCode, durationTime, limit)
js, err := json.Marshal(df)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
func StartWebServer() error {
http.HandleFunc("/api/candle/", apiMakeHandler(apiCandleHandler))
http.HandleFunc("/chart/", viewChartHandler)
return http.ListenAndServe(fmt.Sprintf(":%d", config.Config.Port), nil)
}