-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
88 lines (66 loc) · 2.04 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
package main
import (
"github.com/docopt/docopt-go"
"github.com/kovetskiy/lorg"
"github.com/reconquest/hierr-go"
"strconv"
)
var version = "DEV"
var logger *lorg.Log
func main() {
usage := `vroxy ` + version + `
Proxy server for balancing a requests to VK API to avoid rate limit exceeded.
Requests grouping to chunks and sends every seconds using Execute
method without the habit of rate limitations.
Usage:
vroxy [options]
Options:
-l --listen <address> HTTP listen address. [default: :8080]
-r --rps <rps> Permissible VK API RPS. [default: 10]
-c --chunk-size <count> Chunk size. [default: 25]
-a --api-version <version> VK API version [default: 5.73]
-v --verbose Logging in debug mode.
`
arguments, err := docopt.Parse(usage, nil, true, version, false)
if err != nil {
panic(err)
}
verbose := arguments["--verbose"].(bool)
logger = setupLogger(verbose)
listen := arguments["--listen"].(string)
version := arguments["--api-version"].(string)
rps, err := strconv.Atoi(arguments["--rps"].(string))
if err != nil {
hierr.Fatalf(err, "unable to parse --rps")
}
chunkSize, err := strconv.Atoi(arguments["--chunk-size"].(string))
if err != nil {
hierr.Fatalf(err, "unable to parse --chunk-size")
}
logger.Info("starting VK commands queue")
queue := NewCommandsQueue(rps)
queue.ChunkSize = chunkSize
queue.Run()
logger.Info("starting VK client")
vk := NewVKClient(rps, version)
vk.Run(queue.ChunksCh)
logger.Info("starting HTTP server")
server := NewServer(queue.CommandsCh, verbose)
err = server.Run(listen)
if err != nil {
logger.Fatal(hierr.Errorf(err, "unable to start HTTP server"))
}
}
func setupLogger(verbose bool) *lorg.Log {
formatTemplate := `${level} %s [${file}:${line}]`
if verbose {
formatTemplate = `${time:15:04:05} ${level} %s [${file}:${line}]`
}
newLogger := lorg.NewLog()
newLogger.SetFormat(lorg.NewFormat(formatTemplate))
newLogger.SetLevel(lorg.LevelInfo)
if verbose {
newLogger.SetLevel(lorg.LevelDebug)
}
return newLogger
}