-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
224 lines (174 loc) · 6.37 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
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
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"reflect"
"runtime"
"strings"
"time"
"github.com/awolverp/kickcore/internal/kickcore"
"github.com/awolverp/kickcore/logging"
)
var (
logConfig logging.Config
coreConfig kickcore.ConfigCore
ListenAddr string
showVersion bool
showUrls bool
)
var core kickcore.Core
func main() {
sigchannel := make(chan os.Signal, 1)
donechannel := make(chan struct{})
go kickcore_server(donechannel)
signal.Notify(sigchannel, os.Interrupt)
select {
case <-donechannel:
signal.Stop(sigchannel)
close(donechannel)
os.Exit(0)
case <-sigchannel:
fmt.Printf("Please wait (for 5 seconds), don't try again (open connections %d) ...\n", core.OpenConnections())
core.Shutdown()
signal.Stop(sigchannel)
close(sigchannel)
close(donechannel)
os.Exit(0)
}
}
func kickcore_server(done chan<- struct{}) {
flag.Usage = func() { fmt.Printf(helpUsage, kickcore.Version(), os.Args[0]) }
// server
flag.StringVar(&ListenAddr, "l", "127.0.0.1:9090", "")
flag.DurationVar(&coreConfig.ServerReadTimeout, "server-timeout:read", time.Second*30, "")
flag.DurationVar(&coreConfig.ServerWriteTimeout, "server-timeout:write", time.Second*30, "")
flag.BoolVar(&coreConfig.ReduceServerMemoryUsage, "reduce-memory-usage", false, "")
flag.BoolVar(&coreConfig.ServerGetOnly, "get-only", false, "")
// cache
flag.BoolVar(&coreConfig.DisableCaching, "disable-cache", false, "")
flag.DurationVar(&coreConfig.CacheExpirationMachineInterval, "expire:interval", time.Minute, "")
flag.StringVar(&coreConfig.CacheExtraTTLFilename, "expire:ttl", "extra_ttl.json", "")
flag.StringVar(&coreConfig.CacheSQLiteDSN, "sqlite:dsn", "db.sqlite3", "")
flag.DurationVar(&coreConfig.CacheSQLiteTimeout, "sqlite:timeout", time.Minute, "")
// api client timeouts
flag.DurationVar(&coreConfig.APIClientReadTimeout, "client-timeout:read", time.Second*20, "")
flag.DurationVar(&coreConfig.APIClientWriteTimeout, "client-timeout:write", time.Second*20, "")
// logging options
flag.IntVar(&coreConfig.LoggingLevel, "v", kickcore.LOGGING_WARNING, "")
flag.StringVar(&logConfig.Filename, "log:file", "", "")
flag.BoolVar(&logConfig.Append, "log:append", false, "")
flag.BoolVar(&coreConfig.ServerLogSpeed, "log:speed", false, "")
// other
flag.BoolVar(&showUrls, "urls", false, "")
flag.BoolVar(&showVersion, "version", false, "")
flag.Parse()
if showVersion {
fmt.Printf(
"kickcore %s [%s] - build on %s/%s\n",
kickcore.Version(), runtime.Version(),
runtime.GOOS, runtime.GOARCH,
)
done <- struct{}{}
return
}
if showUrls {
for _, value := range core.Urls() {
var fname string
fobj := runtime.FuncForPC(reflect.ValueOf(value[1]).Pointer())
if fobj != nil {
fname = strings.Split(fobj.Name(), ".")[1]
}
fmt.Printf(
"%s - func %s\n", value[0].(string), fname,
)
}
done <- struct{}{}
return
}
fmt.Printf(
"KickIt Core Server %s (C) / by aWolverP - [%s] on %s/%s\n\n",
kickcore.Version(), runtime.Version(), runtime.GOOS, runtime.GOARCH,
)
coreConfig.LoggingConfig = &logConfig
if err := core.Init(&coreConfig); err != nil {
fmt.Println("ERROR", err)
done <- struct{}{}
return
}
if err := core.Serve(ListenAddr); err != nil {
fmt.Println("ERROR", err)
}
done <- struct{}{}
}
var helpUsage = `NAME
kickcore %s - KickCore Server (C)
USAGE
%s [OPTIONS]
DESCRIPTION
kickcore (C) is a Football API server written in golang language.
OPTIONS
*Server
-l=address (default "127.0.0.1:9090")
Server listening address. If the port in the address
parameter is empty or "0", as in "127.0.0.1:" or
"[::1]:0", a port number is automatically chosen. The
Addr method of Listener can be used to discover the
chosen port.
-server-timeout:read=duration (default 30s)
is the amount of time allowed to read the full request
including body. The connection's read deadline is
reset when the connection opens, or for keep-alive
connections after the first byte has been read.
-server-timeout:write=duration (default 30s)
is the maximum duration before timing out writes of the
response. It is reset after the request handler has
returned.
-reduce-memory-usage
reduces memory usage at the cost of higher CPU usage.
Try enabling this option only if the server consumes too
much memory serving mostly idle keep-alive connections.
This may reduce memory usage by more than 50%%.
-get-only
Rejects all non-GET requests. This option is useful as
anti-DoS protection for servers accepting only GET
requests.
*Cache
-disable-cache
Disable cache. It slows down this server and maybe banned
from original football API.
-expire:interval=duration (default 1m)
The Cache expiration machine checks the cache for expired
objects after any interval time.
-expire:ttl=filename (default "extra_ttl.json")
Configuration file of Time-To-Live of cached objects.
file format must be JSON, like 'extra_ttl.json'.
if set empty, all objects are deleted after every -expire-interval.
-sqlite:dsn=dsn (default "db.sqlite3")
SQLite path address.
-sqlite:timeout=duration (default 1m)
SQLite connecting timeout.
*API Client
-client-timeout:read=duration (default 20s)
Maximum duration for full response reading (including body)
-client-timeout:write=duration (default 20s)
Maximum duration for full request writing (including body).
*Logging
-v=[0-4] (default 1)
Logging verbose level.
0 Critical level.
1 Error level.
2 Warning level.
3 Information level.
4 Debugging level.
-log:file=filename (default "")
Logging filename. if set, the logs are written in the file.
-log:append
If set, not truncate file and append new logs to file.
-log:speed
Show server handlers ping speed. (needs -v 3 or 4)
*Other
-version Print version and exit.
-urls Print URLs and exit.
`