Skip to content

Commit 1493e00

Browse files
author
Jack Tang
committed
ADD: ClientConfig and ServerConfig
1 parent a496246 commit 1493e00

File tree

2 files changed

+64
-43
lines changed

2 files changed

+64
-43
lines changed

internal/client.go

+17-10
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,42 @@ Options:
2323
--override-patch-method Sending a POST request instead of PATCH [default: false]
2424
`
2525

26+
type ClientConf struct {
27+
file string
28+
url string
29+
resume bool
30+
}
31+
2632
func Client() {
2733
var err error
28-
arguments, _ := docopt.ParseDoc(clientUsage)
2934

30-
file, _ := arguments.String("<file>")
31-
url, _ := arguments.String("<url>")
32-
resume := util.GetBool(arguments, "--resumable")
35+
var conf ClientConf
36+
arguments, _ := docopt.ParseDoc(clientUsage)
37+
conf.file, _ = arguments.String("<file>")
38+
conf.url, _ = arguments.String("<url>")
39+
conf.resume = util.GetBool(arguments, "--resumable")
3340

3441
// open file
35-
f, err := os.Open(file)
42+
f, err := os.Open(conf.file)
3643
if err != nil {
37-
util.ExitWithMessages("Cannot open file: " + file)
44+
util.ExitWithMessages("Cannot open file: " + conf.file)
3845
}
3946
defer f.Close()
4047

4148
// create the tus client
4249
var store tus.Store
43-
if resume {
50+
if conf.resume {
4451
path := util.GetString(arguments, "--store")
4552
store, err = leveldbstore.NewLeveldbStore(path)
4653
if err != nil {
4754
util.ExitWithMessages("Cannot Open "+path, clientUsage)
4855
}
4956
}
5057

51-
client, _ := tus.NewClient(url, &tus.Config{
58+
client, _ := tus.NewClient(conf.url, &tus.Config{
5259
ChunkSize: util.GetInt64(arguments, "--chuck-size"),
5360
OverridePatchMethod: util.GetBool(arguments, "--override-patch-method"),
54-
Resume: resume,
61+
Resume: conf.resume,
5562
Store: store,
5663
Header: make(http.Header),
5764
HttpClient: nil,
@@ -65,7 +72,7 @@ func Client() {
6572

6673
// create the uploader.
6774
var uploader *tus.Uploader
68-
if resume {
75+
if conf.resume {
6976
uploader, err = client.CreateOrResumeUpload(upload)
7077
} else {
7178
uploader, err = client.CreateUpload(upload)

internal/server.go

+47-33
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,27 @@ Options:
2727
-h --host HOST Host to bind HTTP server to [default: 0.0.0.0]
2828
-p --port PORT Port to bind HTTP server to [default: 1080]
2929
-d --dir PATH Directory to store uploads in [default: ./data]
30-
-b --base-path PATH Basepath of the HTTP server [default: /files/]
30+
--base-path PATH Basepath of the HTTP server [default: /files/]
3131
--unix-sock PATH If set will listen to a UNIX socket at this location instead of a TCP socket
3232
--max-size SIZE Maximum size of a single upload in bytes [default: 0]
3333
--store-size BYTE Size of space allowed for storage [default: 0]
3434
--timeout TIMEOUT Read timeout for connections in milliseconds. A zero value means that reads will not timeout [default: 30*1000]
3535
--behind-proxy Respect X-Forwarded-* and similar headers which may be set by proxies [default: false]
3636
`
3737

38+
type ServerConf struct {
39+
httpHost string
40+
httpPort string
41+
httpSock string
42+
maxSize int64
43+
uploadDir string
44+
storeSize int64
45+
listingEndpoint string
46+
uploadEndpoint string
47+
timeout int64
48+
isBehindProxy bool
49+
}
50+
3851
var stdout = log.New(os.Stdout, "[tusd] ", log.Ldate|log.Ltime)
3952
var stderr = log.New(os.Stderr, "[tusd] ", log.Ldate|log.Ltime)
4053

@@ -43,62 +56,63 @@ func logEv(logOutput *log.Logger, eventName string, details ...string) {
4356
}
4457

4558
func Server() {
59+
var conf ServerConf
4660
arguments, _ := docopt.ParseDoc(serverUsage)
47-
var httpHost, _ = arguments.String("--host")
48-
var httpPort, _ = arguments.String("--port")
49-
var httpSock, _ = arguments.String("--unix-sock")
50-
var maxSize = util.GetInt64(arguments, "--max-size")
51-
var uploadDir, _ = arguments.String("--dir")
52-
var storeSize = util.GetInt64(arguments, "--store-size")
53-
var listingEndpoint = "/"
54-
var uploadEndpoint, _ = arguments.String("--base-path")
55-
var timeout = util.GetInt64(arguments, "--timeout")
56-
var isBehindProxy, _ = arguments.Bool("--behind-proxy")
61+
conf.httpHost, _ = arguments.String("--host")
62+
conf.httpPort, _ = arguments.String("--port")
63+
conf.httpSock, _ = arguments.String("--unix-sock")
64+
conf.maxSize = util.GetInt64(arguments, "--max-size")
65+
conf.uploadDir, _ = arguments.String("--dir")
66+
conf.storeSize = util.GetInt64(arguments, "--store-size")
67+
conf.listingEndpoint = "/"
68+
conf.uploadEndpoint, _ = arguments.String("--base-path")
69+
conf.timeout = util.GetInt64(arguments, "--timeout")
70+
conf.isBehindProxy, _ = arguments.Bool("--behind-proxy")
5771

5872
storeCompoesr := tusd.NewStoreComposer()
5973

60-
stdout.Printf("Using '%s' as directory storage.\n", uploadDir)
61-
if err := os.MkdirAll(uploadDir, os.FileMode(0774)); err != nil {
74+
stdout.Printf("Using '%s' as directory storage.\n", conf.uploadDir)
75+
if err := os.MkdirAll(conf.uploadDir, os.FileMode(0774)); err != nil {
6276
stderr.Fatalf("Unable to ensure directory exists: %s", err)
6377
}
64-
store := filestore.New(uploadDir)
78+
store := filestore.New(conf.uploadDir)
6579
store.UseIn(storeCompoesr)
6680

67-
if storeSize > 0 {
68-
limitedstore.New(storeSize, storeCompoesr.Core, storeCompoesr.Terminater).UseIn(storeCompoesr)
69-
stdout.Printf("Using %.2fMB as storage size.\n", float64(storeSize)/1024/1024)
81+
if conf.storeSize > 0 {
82+
limitedstore.New(conf.storeSize, storeCompoesr.Core, storeCompoesr.Terminater).UseIn(storeCompoesr)
83+
stdout.Printf("Using %.2fMB as storage size.\n", float64(conf.storeSize)/1024/1024)
7084

7185
// We need to ensure that a single upload can fit into the storage size
72-
if maxSize > storeSize || maxSize == 0 {
73-
maxSize = storeSize
86+
if conf.maxSize > conf.storeSize || conf.maxSize == 0 {
87+
conf.maxSize = conf.storeSize
7488
}
7589
}
7690

77-
stdout.Printf("Using %.2fMB as maximum size.\n", float64(maxSize)/1024/1024)
91+
stdout.Printf("Using %.2fMB as maximum size.\n", float64(conf.maxSize)/1024/1024)
7892

7993
// Serve
8094

8195
// Address
8296
address := ""
83-
if httpSock != "" {
84-
address = httpSock
97+
if conf.httpSock != "" {
98+
address = conf.httpSock
8599
stdout.Printf("Using %s as socket to listen.\n", address)
86100
} else {
87-
address = httpHost + ":" + httpPort
101+
address = conf.httpHost + ":" + conf.httpPort
88102
stdout.Printf("Using %s as address to listen.\n", address)
89103
}
90104

91105
// Base path
92-
stdout.Printf("Using %s as the base path.\n", uploadEndpoint)
106+
stdout.Printf("Using %s as the base path.\n", conf.uploadEndpoint)
93107

94108
// show capabilities
95109
stdout.Printf(storeCompoesr.Capabilities())
96110

97111
// tus handler
98112
handler, err := tusd.NewHandler(tusd.Config{
99-
MaxSize: maxSize,
100-
BasePath: uploadEndpoint,
101-
RespectForwardedHeaders: isBehindProxy,
113+
MaxSize: conf.maxSize,
114+
BasePath: conf.uploadEndpoint,
115+
RespectForwardedHeaders: conf.isBehindProxy,
102116
StoreComposer: storeCompoesr,
103117
NotifyCompleteUploads: false,
104118
NotifyTerminatedUploads: false,
@@ -109,19 +123,19 @@ func Server() {
109123
stderr.Fatalf("Unable to create handler: %s", err)
110124
}
111125

112-
http.Handle(uploadEndpoint, http.StripPrefix(uploadEndpoint, handler))
113-
if listingEndpoint != uploadEndpoint {
114-
http.Handle(listingEndpoint, http.StripPrefix(listingEndpoint, homepage(store)))
126+
http.Handle(conf.uploadEndpoint, http.StripPrefix(conf.uploadEndpoint, handler))
127+
if conf.listingEndpoint != conf.uploadEndpoint {
128+
http.Handle(conf.listingEndpoint, http.StripPrefix(conf.listingEndpoint, homepage(store)))
115129
}
116130

117131
var listener net.Listener
118-
timeoutDuration := time.Duration(timeout) * time.Millisecond
132+
timeoutDuration := time.Duration(conf.timeout) * time.Millisecond
119133

120-
if httpSock != "" {
134+
if conf.httpSock != "" {
121135
if listener, err = util.NewUnixListener(address, timeoutDuration, timeoutDuration); err != nil {
122136
stderr.Fatalf("Unable to create listener: %s", err)
123137
}
124-
stdout.Printf("You can now upload files to: http://%s%s", address, uploadEndpoint)
138+
stdout.Printf("You can now upload files to: http://%s%s", address, conf.uploadEndpoint)
125139
} else {
126140
if listener, err = util.NewListener(address, timeoutDuration, timeoutDuration); err != nil {
127141
stderr.Fatalf("Unable to create listener: %s", err)

0 commit comments

Comments
 (0)