-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
serve.go
130 lines (108 loc) · 2.7 KB
/
serve.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
package main
import (
"net"
"net/http"
"time"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
cs "github.com/webtor-io/common-services"
s "github.com/webtor-io/torrent-store/services"
p "github.com/webtor-io/torrent-store/services/providers"
)
func makeServeCMD() cli.Command {
serveCmd := cli.Command{
Name: "serve",
Aliases: []string{"s"},
Usage: "Serves web server",
Action: serve,
}
configureServe(&serveCmd)
return serveCmd
}
func configureServe(c *cli.Command) {
c.Flags = cs.RegisterProbeFlags(c.Flags)
c.Flags = cs.RegisterS3ClientFlags(c.Flags)
c.Flags = cs.RegisterRedisClientFlags(c.Flags)
c.Flags = cs.RegisterPprofFlags(c.Flags)
c.Flags = s.RegisterGRPCFlags(c.Flags)
c.Flags = p.RegisterBadgerFlags(c.Flags)
c.Flags = p.RegisterRedisFlags(c.Flags)
c.Flags = p.RegisterS3Flags(c.Flags)
c.Flags = s.RegisterAbuseClientFlags(c.Flags)
c.Flags = s.RegisterAbuseFlags(c.Flags)
c.Flags = s.RegisterStoplistFlags(c.Flags)
}
func serve(c *cli.Context) (err error) {
stoplist, err := s.NewStoplist(c)
if err != nil {
return
}
var servers []cs.Servable
// Setting Probe
probe := cs.NewProbe(c)
if probe != nil {
servers = append(servers, probe)
defer probe.Close()
}
// Setting Pprof
pprof := cs.NewPprof(c)
if pprof != nil {
servers = append(servers, pprof)
defer pprof.Close()
}
var providers []s.StoreProvider
// Setting Badger Provider
badger := p.NewBadger(c)
defer badger.Close()
providers = append(providers, badger)
// Setting Redis Client
redisCl := cs.NewRedisClient(c)
defer redisCl.Close()
// Setting Redis Provider
redis := p.NewRedis(c, redisCl)
if redis != nil {
providers = append(providers, redis)
}
// Setting HTTP Client
myTransport := &http.Transport{
MaxIdleConns: 500,
MaxIdleConnsPerHost: 500,
MaxConnsPerHost: 500,
IdleConnTimeout: 90 * time.Second,
Dial: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 15 * time.Minute,
}).Dial,
}
cl := &http.Client{
Timeout: 10 * time.Second,
Transport: myTransport,
}
// Setting S3 Client
s3Cl := cs.NewS3Client(c, cl)
// Setting S3 Provider
s3 := p.NewS3(c, s3Cl)
if s3 != nil {
providers = append(providers, s3)
}
// Setting Store
store := s.NewStore(providers)
// Setting Abuse Client
aCl := s.NewAbuseClient(c)
// Setting Abuse
abuse := s.NewAbuse(c, aCl)
// Setting Server
server := s.NewServer(store, abuse, stoplist)
// Setting GRPC Server
grpcServer := s.NewGRPCServer(c, server)
servers = append(servers, grpcServer)
defer grpcServer.Close()
// Setting ServeService
serve := cs.NewServe(servers...)
// And SERVE!
err = serve.Serve()
if err != nil {
log.WithError(err).Error("got server error")
}
return
}