@@ -27,14 +27,27 @@ Options:
27
27
-h --host HOST Host to bind HTTP server to [default: 0.0.0.0]
28
28
-p --port PORT Port to bind HTTP server to [default: 1080]
29
29
-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/]
31
31
--unix-sock PATH If set will listen to a UNIX socket at this location instead of a TCP socket
32
32
--max-size SIZE Maximum size of a single upload in bytes [default: 0]
33
33
--store-size BYTE Size of space allowed for storage [default: 0]
34
34
--timeout TIMEOUT Read timeout for connections in milliseconds. A zero value means that reads will not timeout [default: 30*1000]
35
35
--behind-proxy Respect X-Forwarded-* and similar headers which may be set by proxies [default: false]
36
36
`
37
37
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
+
38
51
var stdout = log .New (os .Stdout , "[tusd] " , log .Ldate | log .Ltime )
39
52
var stderr = log .New (os .Stderr , "[tusd] " , log .Ldate | log .Ltime )
40
53
@@ -43,62 +56,63 @@ func logEv(logOutput *log.Logger, eventName string, details ...string) {
43
56
}
44
57
45
58
func Server () {
59
+ var conf ServerConf
46
60
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" )
57
71
58
72
storeCompoesr := tusd .NewStoreComposer ()
59
73
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 {
62
76
stderr .Fatalf ("Unable to ensure directory exists: %s" , err )
63
77
}
64
- store := filestore .New (uploadDir )
78
+ store := filestore .New (conf . uploadDir )
65
79
store .UseIn (storeCompoesr )
66
80
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 )
70
84
71
85
// 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
74
88
}
75
89
}
76
90
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 )
78
92
79
93
// Serve
80
94
81
95
// Address
82
96
address := ""
83
- if httpSock != "" {
84
- address = httpSock
97
+ if conf . httpSock != "" {
98
+ address = conf . httpSock
85
99
stdout .Printf ("Using %s as socket to listen.\n " , address )
86
100
} else {
87
- address = httpHost + ":" + httpPort
101
+ address = conf . httpHost + ":" + conf . httpPort
88
102
stdout .Printf ("Using %s as address to listen.\n " , address )
89
103
}
90
104
91
105
// 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 )
93
107
94
108
// show capabilities
95
109
stdout .Printf (storeCompoesr .Capabilities ())
96
110
97
111
// tus handler
98
112
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 ,
102
116
StoreComposer : storeCompoesr ,
103
117
NotifyCompleteUploads : false ,
104
118
NotifyTerminatedUploads : false ,
@@ -109,19 +123,19 @@ func Server() {
109
123
stderr .Fatalf ("Unable to create handler: %s" , err )
110
124
}
111
125
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 )))
115
129
}
116
130
117
131
var listener net.Listener
118
- timeoutDuration := time .Duration (timeout ) * time .Millisecond
132
+ timeoutDuration := time .Duration (conf . timeout ) * time .Millisecond
119
133
120
- if httpSock != "" {
134
+ if conf . httpSock != "" {
121
135
if listener , err = util .NewUnixListener (address , timeoutDuration , timeoutDuration ); err != nil {
122
136
stderr .Fatalf ("Unable to create listener: %s" , err )
123
137
}
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 )
125
139
} else {
126
140
if listener , err = util .NewListener (address , timeoutDuration , timeoutDuration ); err != nil {
127
141
stderr .Fatalf ("Unable to create listener: %s" , err )
0 commit comments