Skip to content

Commit 47db2ee

Browse files
committed
add options to autoclean backups
1 parent 36c9247 commit 47db2ee

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

tw5server.nim

+26-11
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import json
2222

2323
const
2424
name = "TW5 server"
25-
version = "1.3.0"
25+
version = "1.5.0"
2626
style = staticRead("style.css")
2727
temp = staticRead("template.html")
2828
js = staticRead("main.js")
@@ -41,6 +41,7 @@ tw5server -a:localhost -p:8000 -d:dir -b:backup
4141
-b backup directory, default `backup` in serve dir. `backup/` or `backup\\` for a backup path.
4242
-l show log message
4343
-m max size of uploaded file (MB), default 100
44+
--autoclean if auto clean backups.
4445
4546
Backups auto-clean strategy:
4647
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 =
153154
writeFile(path, content)
154155
logmsg("Update: " & path, log)
155156

156-
let (dir, name, _) = splitFile(path)
157+
let (_, name, _) = splitFile(path)
157158
let backup_name = backup / name & "-" & time_now() & ".html.gz"
158159

159160
let compressed = compress(content, BestCompression)
@@ -288,6 +289,7 @@ var
288289
maxbody = 100 # max body length (MB)
289290
configFile = "tw5server.json"
290291
configStr = "{}"
292+
autoclean = false
291293

292294
for kind, key, val in parseopt.getopt():
293295
case kind
@@ -308,6 +310,8 @@ for kind, key, val in parseopt.getopt():
308310
dir = val
309311
of "b", "backup":
310312
backup = val
313+
of "autoclean":
314+
autoclean = true
311315
of "l", "log":
312316
log = true
313317
of "m", "max":
@@ -328,6 +332,7 @@ address = config{"address"}.getStr(address)
328332
port = config{"port"}.getInt(port)
329333
title = config{"title"}.getStr(title)
330334
backup = config{"backup"}.getStr(backup)
335+
autoclean = config{"autoclean"}.getBool(autoclean)
331336

332337
var settings: NimHttpSettings
333338
settings.directory = dir
@@ -348,22 +353,32 @@ echo(" Backup dir: ", backup)
348353

349354
createDir(backup)
350355

351-
proc handleCtrlC() {.noconv.} =
352-
write(stdout, "\rClean backups (y to clean): ")
353-
let clean = readLine(stdin)
354-
356+
proc auto_clean_backup(folder: string) =
355357
var cleaned = 0
356-
if "y" == clean:
357-
cleaned = clean_backup(backup)
358+
cleaned = clean_backup(folder)
358359

359360
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)
361368
else:
362-
echo("No backups were cleaned. Bye ~")
369+
write(stdout, "\rClean backups (y to clean): ")
370+
let clean = readLine(stdin)
371+
if "y" == clean:
372+
auto_clean_backup(backup)
363373

374+
echo("Bye ~")
364375
quit()
365376

366-
setControlCHook(handleCtrlC)
377+
if autoclean:
378+
auto_clean_backup(backup)
379+
setControlCHook(handleCtrlC)
380+
else:
381+
setControlCHook(handleCtrlC)
367382

368383
maxbody = config{"max_body"}.getInt(maxbody)
369384
log = config{"log"}.getBool(log)

0 commit comments

Comments
 (0)