Skip to content

Commit

Permalink
save all backups in one dir
Browse files Browse the repository at this point in the history
  • Loading branch information
hffqyd committed Aug 16, 2024
1 parent cf8dea3 commit cd76e7e
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions tw5server.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,30 @@ import
zippy,
mimetypes

import strformat
import tables, strtabs
from httpcore import HttpMethod, HttpHeaders

from parseBody import parseMPFD

const
name = "TW5 server"
version = "1.2.3"
version = "1.2.4"
style = staticRead("style.css")
temp = staticRead("template.html")
js = staticRead("main.js")

const usage = """
const usage = fmt"""
{name} {version}
Usage:
tw5server -a:localhost -p:8000 -d:dir -b:backup
-h usage help
-h this help
-a address, defautl "127.0.0.1"
-p port, default 8000
-d directory to servering, default `current dir`
-b backup directory name, default `backup`
-d directory to serve, default `current dir`
-b backup directory, default `backup` in serve dir. `backup/` or `backup\\` for a backup path.
-l show log message
-m max size of uploaded file (MB), default 100
Expand Down Expand Up @@ -147,7 +151,7 @@ proc getPut(req: Request, path, backup: string, log: bool): NimHttpResponse =
logmsg("Update: " & path, log)

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

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

proc clean_backup(dir, backup: string): int =
proc clean_backup(backup: string): int =
var names: HashSet[string]
let all_backups = toSeq(walkPattern(dir / backup / "*.html.gz"))
let all_backups = toSeq(walkPattern(backup / "*.html.gz"))

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

var count = 0
for i in names:
for old in old_backups(dir / backup, i):
for old in old_backups(backup, i):
removeFile(old)
count += 1

Expand Down Expand Up @@ -302,6 +306,9 @@ for kind, key, val in parseopt.getopt():
log = true
of "m", "max":
maxbody = parseInt(val)
of "v", "version":
echo version
quit()
else:
assert(false)

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

echo(" Serving url: ", address, ":", port)
echo("Serving path: ", dir)

if not ("/" in backup or "\\" in backup):
backup = dir / backup
echo(" Backup dir: ", backup)

createDir(dir / backup)
createDir(backup)

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

var cleaned = 0
if "y" == clean:
cleaned = clean_backup(dir, backup)
cleaned = clean_backup(backup)

if cleaned > 0:
echo(cleaned, " backup(s) cleaned. Bye ~")
Expand Down

0 comments on commit cd76e7e

Please sign in to comment.