Skip to content

Commit cd76e7e

Browse files
committed
save all backups in one dir
1 parent cf8dea3 commit cd76e7e

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

tw5server.nim

+21-11
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,30 @@ import
1212
zippy,
1313
mimetypes
1414

15+
import strformat
1516
import tables, strtabs
1617
from httpcore import HttpMethod, HttpHeaders
1718

1819
from parseBody import parseMPFD
1920

2021
const
2122
name = "TW5 server"
22-
version = "1.2.3"
23+
version = "1.2.4"
2324
style = staticRead("style.css")
2425
temp = staticRead("template.html")
2526
js = staticRead("main.js")
2627

27-
const usage = """
28+
const usage = fmt"""
29+
{name} {version}
30+
31+
Usage:
2832
tw5server -a:localhost -p:8000 -d:dir -b:backup
2933
30-
-h usage help
34+
-h this help
3135
-a address, defautl "127.0.0.1"
3236
-p port, default 8000
33-
-d directory to servering, default `current dir`
34-
-b backup directory name, default `backup`
37+
-d directory to serve, default `current dir`
38+
-b backup directory, default `backup` in serve dir. `backup/` or `backup\\` for a backup path.
3539
-l show log message
3640
-m max size of uploaded file (MB), default 100
3741
@@ -147,7 +151,7 @@ proc getPut(req: Request, path, backup: string, log: bool): NimHttpResponse =
147151
logmsg("Update: " & path, log)
148152

149153
let (dir, name, _) = splitFile(path)
150-
let backup_name = dir / backup / name & "-" & time_now() & ".html.gz"
154+
let backup_name = backup / name & "-" & time_now() & ".html.gz"
151155

152156
let compressed = compress(content, BestCompression)
153157
writeFile(backup_name, compressed)
@@ -255,17 +259,17 @@ proc backupFileName(name: string): string =
255259
# backup name: name-timestamp.html.gz, e.g, test-20230227142037.html.gz
256260
return name[0..^21]
257261

258-
proc clean_backup(dir, backup: string): int =
262+
proc clean_backup(backup: string): int =
259263
var names: HashSet[string]
260-
let all_backups = toSeq(walkPattern(dir / backup / "*.html.gz"))
264+
let all_backups = toSeq(walkPattern(backup / "*.html.gz"))
261265

262266
for i in all_backups:
263267
let (_, name, _) = splitFile(i)
264268
names.incl(backupFileName(name))
265269

266270
var count = 0
267271
for i in names:
268-
for old in old_backups(dir / backup, i):
272+
for old in old_backups(backup, i):
269273
removeFile(old)
270274
count += 1
271275

@@ -302,6 +306,9 @@ for kind, key, val in parseopt.getopt():
302306
log = true
303307
of "m", "max":
304308
maxbody = parseInt(val)
309+
of "v", "version":
310+
echo version
311+
quit()
305312
else:
306313
assert(false)
307314

@@ -317,17 +324,20 @@ settings.port = Port(port)
317324

318325
echo(" Serving url: ", address, ":", port)
319326
echo("Serving path: ", dir)
327+
328+
if not ("/" in backup or "\\" in backup):
329+
backup = dir / backup
320330
echo(" Backup dir: ", backup)
321331

322-
createDir(dir / backup)
332+
createDir(backup)
323333

324334
proc handleCtrlC() {.noconv.} =
325335
write(stdout, "\rClean backups (y to clean): ")
326336
let clean = readLine(stdin)
327337

328338
var cleaned = 0
329339
if "y" == clean:
330-
cleaned = clean_backup(dir, backup)
340+
cleaned = clean_backup(backup)
331341

332342
if cleaned > 0:
333343
echo(cleaned, " backup(s) cleaned. Bye ~")

0 commit comments

Comments
 (0)