diff options
author | Julien Dessaux | 2021-10-26 21:41:39 +0200 |
---|---|---|
committer | Julien Dessaux | 2021-10-26 21:41:39 +0200 |
commit | 7f79a49bff7883db126481f8c8a795aa312e3505 (patch) | |
tree | f04efeff6440f580a9f109686e1cb30822c40ad6 | |
parent | Added 6 months expiration option (diff) | |
download | short-7f79a49bff7883db126481f8c8a795aa312e3505.tar.gz short-7f79a49bff7883db126481f8c8a795aa312e3505.tar.bz2 short-7f79a49bff7883db126481f8c8a795aa312e3505.zip |
cosmetics
-rw-r--r-- | short.nimble | 17 | ||||
-rw-r--r-- | src/database.nim | 2 | ||||
-rw-r--r-- | src/dbUtils.nim | 16 | ||||
-rw-r--r-- | src/short.nim | 6 | ||||
-rw-r--r-- | src/templates/noshort.html | 4 | ||||
-rw-r--r-- | tests/database.nim | 2 |
6 files changed, 32 insertions, 15 deletions
diff --git a/short.nimble b/short.nimble index 5da3330..ab89b75 100644 --- a/short.nimble +++ b/short.nimble @@ -14,3 +14,20 @@ requires "nim >= 1.4.8", "https://github.com/dom96/jester#HEAD", "nimja >= 0.4.1", "https://github.com/GULPF/tiny_sqlite#HEAD" + +import os, strformat + +task fmt, "Run nimpretty on all git-managed .nim files in the current repo": + ## Usage: nim fmt + for file in walkDirRec("./", {pcFile, pcDir}): + if file.splitFile().ext == ".nim": + let + # https://github.com/nim-lang/Nim/issues/6262#issuecomment-454983572 + # https://stackoverflow.com/a/2406813/1219634 + fileIsGitManaged = gorgeEx("cd $1 && git ls-files --error-unmatch $2" % [getCurrentDir(), file]).exitCode == 0 + # ^^^^^-- That "cd" is required. + if fileIsGitManaged: + let + cmd = "nimpretty --maxLineLen=220 $1" % [file] + echo "Running $1 .." % [cmd] + exec(cmd) diff --git a/src/database.nim b/src/database.nim index 8094f31..1ffc0df 100644 --- a/src/database.nim +++ b/src/database.nim @@ -22,7 +22,7 @@ const migrations = [ const latestVersion = migrations.len proc Migrate*(db: DbConn): bool {.raises: [].} = - var currentVersion : int + var currentVersion: int try: currentVersion = db.value("SELECT version FROM schema_version;").get().fromDbValue(int) except SqliteError: diff --git a/src/dbUtils.nim b/src/dbUtils.nim index bdbcf14..cfe54cf 100644 --- a/src/dbUtils.nim +++ b/src/dbUtils.nim @@ -1,13 +1,13 @@ import std/times import tiny_sqlite -proc toDbValue*(t: DateTime): DbValue {.raises: [].}= +proc toDbValue*(t: DateTime): DbValue {.raises: [].} = DbValue(kind: sqliteText, strVal: $t) -proc fromDbValue*(value: DbValue, T: typedesc[DateTime]): DateTime {.raises: [].}= - try: - case value.kind: - of sqliteText: return times.parse(value.strVal, "yyyy-MM-dd'T'HH:mm:sszzz") - else: return - except TimeParseError: - return +proc fromDbValue*(value: DbValue, T: typedesc[DateTime]): DateTime {.raises: [].} = + try: + case value.kind: + of sqliteText: return times.parse(value.strVal, "yyyy-MM-dd'T'HH:mm:sszzz") + else: return + except TimeParseError: + return diff --git a/src/short.nim b/src/short.nim index fa5486a..7057779 100644 --- a/src/short.nim +++ b/src/short.nim @@ -13,7 +13,7 @@ const favicon = staticRead("../static/favicon.ico") var db {.threadvar.}: DbConn -proc initDB() {.raises: [SqliteError].}= +proc initDB() {.raises: [SqliteError].} = if not db.isOpen(): db = openDatabase("data/short.db") if not db.Migrate(): @@ -36,7 +36,7 @@ func renderNoShort(req: ShortUrl): string {.raises: [].} = func renderError(code: int, msg: string): string {.raises: [].} = compileTemplateFile(getScriptDir() / "templates/error.html") -proc handleToken(token:string): (HttpCode, string) {.raises: [].} = +proc handleToken(token: string): (HttpCode, string) {.raises: [].} = try: let tokenRegexp = re"^[\w]{24}$" if not match(token, tokenRegexp): @@ -89,7 +89,7 @@ proc handleIndexPost(params: Table[string, string]): (HttpCode, string) {.raises return (Http400, renderError(400, "Bad Request")) input.Token = $genOid() input.Created = times.now() - input.Expires = input.Created + initDuration(minutes=exp) + input.Expires = input.Created + initDuration(minutes = exp) try: db.AddUrl(input) except SqliteError: diff --git a/src/templates/noshort.html b/src/templates/noshort.html index e0be436..976e911 100644 --- a/src/templates/noshort.html +++ b/src/templates/noshort.html @@ -2,9 +2,9 @@ {% block content %} <h1>URL not found!</h1> <p> - This url does not exist or has expired, sorry! +This url does not exist or has expired, sorry! </p> <p> - <a href="/">Go back</a> +<a href="/">Go back</a> </p> {% endblock %} diff --git a/tests/database.nim b/tests/database.nim index 9e8de52..6fa47c5 100644 --- a/tests/database.nim +++ b/tests/database.nim @@ -2,7 +2,7 @@ include ../src/database import unittest -const someTime = initDuration(seconds=1) +const someTime = initDuration(seconds = 1) let testingNow = times.now() - 60 * someTime let later = testingNow + 30 * someTime |