/
server.go
98 lines (82 loc) · 2.87 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
89
90
91
92
93
94
95
96
97
98
package main
import (
"xujian.pub/common/version"
"xujian.pub/server"
"fmt"
"strings"
"flag"
"os"
"os/signal"
"syscall"
"time"
)
func serverFlagSet() *flag.FlagSet {
flagSet := flag.NewFlagSet("server", flag.ExitOnError)
flagSet.Bool("version", false, "print version string")
flagSet.Bool("verbose", false, "enable verbose logging")
flagSet.String("tcp-address", "0.0.0.0:6789", "<addr>:<port> to listen")
flagSet.String("data-path", "/data/download", "path where the data store")
flagSet.Duration("client-timeout", 60 * time.Second, "client timeout")
flagSet.Int64("max-clients", 100000, "how many clients cloud connect to this server")
flagSet.Bool("cached", false, "cache some hot data")
flagSet.Int64("cache-size", 1024*1024, "cache data per slice")
flagSet.String("log-file", "", "the file to save the log, if null, output to the stderr")
return flagSet
}
var (
ver = flag.Bool("version", false, "print version string")
verbose = flag.Bool("verbose", false, "enable verbose logging")
tcpaddr = flag.String("tcp-address", "0.0.0.0:6789", "<addr>:<port> to listen")
lookupTcpAddr = flag.String("lookup-tcp-address", "", "<addr>:<port> lookup tcp address, to connect")
datapath = flag.String("data-path", "/data/download", "path where the data store")
client_timeout = flag.Duration("client-timeout", 60 * time.Second, "client timeout")
max_client = flag.Int64("max-clients", 100000, "how many clients cloud connect to this server")
cached = flag.Bool("cached", false, "cache some hot data")
cache_size = flag.Int64("cache-size", 1024*1024, "cache data per slice")
cache_timeout = flag.Duration("cache-timeout", 60 * time.Minute, "cache timeout")
log_file = flag.String("log-file", "", "the file to save the log, if null, output to the stderr")
)
func main() {
flag.Parse()
opt := server.NewServerOption()
if *ver {
fmt.Printf("download server v%s\n", version.ServerVersion)
return
}
if *verbose {
opt.Verbose = *verbose
}
if tcpaddr != nil {
opt.TCPAddress = *tcpaddr
}
if datapath != nil {
opt.DataPath = *datapath
}
if client_timeout != nil {
opt.ClientTimeout = *client_timeout
}
if max_client != nil {
opt.MaxClients = *max_client
}
if lookupTcpAddr != nil {
opt.LookupSrvAddrs = strings.Split(*lookupTcpAddr, ",")
}
if *cached {
opt.Cached = *cached
}
if cache_size != nil {
opt.CachedSize = *cache_size
}
if log_file != nil {
opt.LogFile = *log_file
}
if cache_timeout != nil {
opt.CacheTimeout = *cache_timeout
}
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
s := server.NewServer(opt)
s.Main()
<- signalChan
s.Exit()
}