-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexchange.go
213 lines (197 loc) · 5.53 KB
/
exchange.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Package main runs web service to handle incoming requests and
// return Russian Central Bank rate exchange.
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"runtime"
"strings"
"syscall"
"time"
"github.com/z0rr0/exchange/rates"
)
const (
// Name is a programm name
Name = "Exchange"
// Config is default configuration file name
Config = "config.json"
// interruptPrefix is constant prefix of interrupt signal
interruptPrefix = "interrupt signal"
// shutdownTimeout is connections' graceful shutdown timeout
shutdownTimeout = time.Second * 2
)
var (
// Version is a version from GIT tags
Version = "0.0.0"
// Revision - GIT revision number
Revision = "git:000000"
// Date - build date
Date = "2016-01-01_01:01:01UTC"
// GoVersion is runtime Go language version
GoVersion = runtime.Version()
// requiredCodes are default required codes
requiredCodes = map[string][]string{
"USD": {"$", "dollar", "доллар"},
"EUR": {"€", "euro", "евро"},
"RUB": {"₽", "rub", "руб"},
}
// internal loggers
loggerError = log.New(os.Stderr, fmt.Sprintf("ERROR [%v]: ", Name), log.Ldate|log.Ltime|log.Lshortfile)
loggerInfo = log.New(os.Stdout, fmt.Sprintf("INFO [%v]: ", Name), log.Ldate|log.Ltime|log.Lshortfile)
)
// helpParameters is info about HTTP parameters
type helpParameters struct {
D string `json:"d"`
Q string `json:"q"`
}
// help is help data structure
type help struct {
P helpParameters `json:"parameters"`
V string `json:"version"`
Comment string `json:"comment"`
}
// interrupt catches custom signals.
func interrupt(errc chan error) {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
errc <- fmt.Errorf("%v %v", interruptPrefix, <-c)
}
// helpFunc writes help info to ResponseWriter and returns HTTP status code.
func helpFunc(w http.ResponseWriter, r *http.Request, h *help) int {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
encoder := json.NewEncoder(w)
if err := encoder.Encode(h); err != nil {
code := http.StatusInternalServerError
http.Error(w, http.StatusText(code), code)
loggerError.Println(err.Error())
return code
}
return http.StatusOK
}
func main() {
defer func() {
if r := recover(); r != nil {
fmt.Printf("abnormal termination [%v]: \n\t%v\n", Version, r)
}
}()
debug := flag.Bool("debug", false, "debug mode")
version := flag.Bool("version", false, "show version")
config := flag.String("config", Config, "configuration file")
flag.Parse()
if *version {
fmt.Printf("\tVersion: %v\n\tRevision: %v\n\tBuild date: %v\n\tGo version: %v\n",
Version, Revision, Date, GoVersion)
return
}
logger := log.New(ioutil.Discard, fmt.Sprintf("DEBUG [%v]: ", Name),
log.Ldate|log.Lmicroseconds|log.Lshortfile)
if *debug {
logger.SetOutput(os.Stdout)
}
cfg, err := rates.New(*config, logger, fmt.Sprintf("%v/%v", Name, Version))
if err != nil {
loggerError.Fatalf("configuration error: %v", err)
}
err = cfg.SetRequiredCodes(requiredCodes)
if err != nil {
loggerError.Fatal(err)
}
h := &help{
P: helpParameters{
Q: "query (default '1 rub')",
D: "date, format YYYY-MM-DD (default today) [optional]",
},
V: Version,
Comment: "https://github.com/z0rr0/exchange",
}
server := &http.Server{
Addr: cfg.Addr(),
Handler: http.DefaultServeMux,
ReadTimeout: cfg.HandleTimeout(),
WriteTimeout: cfg.HandleTimeout(),
MaxHeaderBytes: 1 << 20, // 1MB
ErrorLog: loggerError,
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
var date time.Time
start, code := time.Now(), http.StatusOK
defer func() {
loggerInfo.Printf("%-5v %v\t%-12v\t%v",
r.Method,
code,
time.Since(start),
r.URL.String(),
)
}()
switch path := strings.TrimRight(r.URL.Path, "/"); {
case path == "/help":
code = helpFunc(w, r, h)
return
case path != "":
code = http.StatusNotFound
http.NotFound(w, r)
return
}
query := r.FormValue("q")
if query == "" {
query = "1 rub"
}
if d := r.FormValue("d"); d != "" {
date, err = time.Parse("2006-01-02", d)
if err != nil {
code = http.StatusBadRequest
http.Error(w, "bad date format", code)
return
}
if date.After(time.Now().UTC()) {
code = http.StatusBadRequest
http.Error(w, "bad date", code)
return
}
} else {
date = time.Now().UTC()
}
info, err := cfg.GetRates(date, query)
if err != nil {
rateError := err.(*rates.RateError)
code = rateError.HTTPCode
http.Error(w, err.Error(), code)
loggerError.Println(err.Error())
return
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
encoder := json.NewEncoder(w)
err = encoder.Encode(info)
if err != nil {
code = http.StatusInternalServerError
http.Error(w, http.StatusText(code), code)
loggerError.Println(err.Error())
return
}
// ok
})
errc := make(chan error)
go interrupt(errc)
go func() {
errc <- server.ListenAndServe()
}()
loggerInfo.Printf("running: version=%v [%v %v debug=%v]\nListen: %v\n\n",
Version, GoVersion, Revision, *debug || cfg.Debug, server.Addr)
err = <-errc
loggerInfo.Printf("termination: %v [%v] reason: %+v\n", Version, Revision, err)
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer cancel()
if msg := err.Error(); strings.HasPrefix(msg, interruptPrefix) {
loggerInfo.Println("graceful shutdown")
if err := server.Shutdown(ctx); err != nil {
loggerError.Printf("graceful shutdown error: %v\n", err)
}
}
}