@@ -22,7 +22,7 @@ import json
22
22
23
23
const
24
24
name = " TW5 server"
25
- version = " 1.3 .0"
25
+ version = " 1.5 .0"
26
26
style = staticRead (" style.css" )
27
27
temp = staticRead (" template.html" )
28
28
js = staticRead (" main.js" )
@@ -41,6 +41,7 @@ tw5server -a:localhost -p:8000 -d:dir -b:backup
41
41
-b backup directory, default `backup` in serve dir. `backup/` or `backup\\` for a backup path.
42
42
-l show log message
43
43
-m max size of uploaded file (MB), default 100
44
+ --autoclean if auto clean backups.
44
45
45
46
Backups auto-clean strategy:
46
47
Keep all backups in current month, keep only the newest one for previous months.
@@ -153,7 +154,7 @@ proc getPut(req: Request, path, backup: string, log: bool): NimHttpResponse =
153
154
writeFile (path, content)
154
155
logmsg (" Update: " & path, log)
155
156
156
- let (dir , name, _) = splitFile (path)
157
+ let (_ , name, _) = splitFile (path)
157
158
let backup_name = backup / name & " -" & time_now () & " .html.gz"
158
159
159
160
let compressed = compress (content, BestCompression )
288
289
maxbody = 100 # max body length (MB)
289
290
configFile = " tw5server.json"
290
291
configStr = " {}"
292
+ autoclean = false
291
293
292
294
for kind, key, val in parseopt.getopt ():
293
295
case kind
@@ -308,6 +310,8 @@ for kind, key, val in parseopt.getopt():
308
310
dir = val
309
311
of " b" , " backup" :
310
312
backup = val
313
+ of " autoclean" :
314
+ autoclean = true
311
315
of " l" , " log" :
312
316
log = true
313
317
of " m" , " max" :
@@ -328,6 +332,7 @@ address = config{"address"}.getStr(address)
328
332
port = config{" port" }.getInt (port)
329
333
title = config{" title" }.getStr (title)
330
334
backup = config{" backup" }.getStr (backup)
335
+ autoclean = config{" autoclean" }.getBool (autoclean)
331
336
332
337
var settings: NimHttpSettings
333
338
settings.directory = dir
@@ -348,22 +353,32 @@ echo(" Backup dir: ", backup)
348
353
349
354
createDir (backup)
350
355
351
- proc handleCtrlC () {.noconv .} =
352
- write (stdout, " \r Clean backups (y to clean): " )
353
- let clean = readLine (stdin)
354
-
356
+ proc auto_clean_backup (folder: string ) =
355
357
var cleaned = 0
356
- if " y" == clean:
357
- cleaned = clean_backup (backup)
358
+ cleaned = clean_backup (folder)
358
359
359
360
if cleaned > 0 :
360
- echo (cleaned, " backup(s) cleaned. Bye ~" )
361
+ echo (cleaned, " backup(s) cleaned" )
362
+ else :
363
+ echo (" No backups were cleaned" )
364
+
365
+ proc handleCtrlC () {.noconv .} =
366
+ if autoclean:
367
+ auto_clean_backup (backup)
361
368
else :
362
- echo (" No backups were cleaned. Bye ~" )
369
+ write (stdout, " \r Clean backups (y to clean): " )
370
+ let clean = readLine (stdin)
371
+ if " y" == clean:
372
+ auto_clean_backup (backup)
363
373
374
+ echo (" Bye ~" )
364
375
quit ()
365
376
366
- setControlCHook (handleCtrlC)
377
+ if autoclean:
378
+ auto_clean_backup (backup)
379
+ setControlCHook (handleCtrlC)
380
+ else :
381
+ setControlCHook (handleCtrlC)
367
382
368
383
maxbody = config{" max_body" }.getInt (maxbody)
369
384
log = config{" log" }.getBool (log)
0 commit comments