From afc61f07e0cb9b2dea24f7602823657bbb454fe4 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sun, 31 Oct 2021 00:31:45 +0200 Subject: Replaced uuid with the shorter nanoids --- src/database.nim | 10 +++++----- src/short.nim | 22 +++++++++++++--------- 2 files changed, 18 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/database.nim b/src/database.nim index 37ef8b4..a696d12 100644 --- a/src/database.nim +++ b/src/database.nim @@ -1,7 +1,7 @@ import std / [options, times] import tiny_sqlite -import uuids +import nanoid import dbUtils @@ -49,7 +49,7 @@ proc Migrate*(db: DbConn): bool {.raises: [].} = type ShortUrl* = object ID*: int - Token*: UUID + Token*: string Title*: string Url*: string Created*: DateTime @@ -60,11 +60,11 @@ proc AddUrl*(db: DbConn, url: ShortUrl) {.raises: [SqliteError].} = INSERT INTO url(token, title, url, created, expires) VALUES (?, ?, ?, ?, ?); """) - stmt.exec($url.Token, url.Title, url.Url, url.Created, $url.Expires) + stmt.exec(url.Token, url.Title, url.Url, url.Created, $url.Expires) -proc GetUrl*(db: DbConn, token: UUID): ref ShortUrl {.raises: [SqliteError].} = +proc GetUrl*(db: DbConn, token: string): ref ShortUrl {.raises: [SqliteError].} = let stmt = db.stmt("SELECT id, title, url, created, expires FROM url WHERE token = ?") - for row in stmt.iterate($token): + for row in stmt.iterate(token): new(result) result.ID = row[0].fromDbValue(int) result.Token = token diff --git a/src/short.nim b/src/short.nim index 1822e20..9ab5f97 100644 --- a/src/short.nim +++ b/src/short.nim @@ -4,10 +4,13 @@ import std/[hashes, re, sequtils, times, uri] import tiny_sqlite import jester import nimja/parser -import uuids +import nanoid import database +const nanoidAlphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" +const nanoidSize = 10 + const allCss = staticRead("../static/all.css") const cssRoute = "/static/all.css." & $hash(allCss) const favicon = staticRead("../static/favicon.ico") @@ -55,12 +58,13 @@ func renderNoShort(req: ShortUrl): string {.raises: [].} = func renderError(code: int, msg: string): string {.raises: [].} = compileTemplateFile(getScriptDir() / "templates/error.html") -proc handleToken(tokenStr: string): (HttpCode, string) {.raises: [].} = - var token: UUID +proc handleToken(token: string): (HttpCode, string) {.raises: [].} = try: - token = parseUUID(tokenStr) - except ValueError: - return (Http400, renderError(400, "Bad Request")) + let tokenRegexp = re"^[\w]{10}$" + if not match(token, tokenRegexp): + return (Http400, renderError(400, "Bad Request")) + except RegexError: + return (Http500, renderError(500, "RegexError")) db.CleanExpired() try: let req = db.GetUrl(token) @@ -102,11 +106,11 @@ proc handleIndexPost(params: Table[string, string]): (HttpCode, string) {.raises if input.Title == "" or input.Url == "" or exp == 0: return (Http400, renderError(400, "Bad Request")) try: - input.Token = genUUID() + input.Token = generate(alphabet=nanoidAlphabet, size=nanoidSize) except IOError: - return (Http500, renderError(500, "IOError on genUUID")) + return (Http500, renderError(500, "IOError on UUID generation")) except OSError: - return (Http500, renderError(500, "OSError on genUUID")) + return (Http500, renderError(500, "OSError on UUID generation")) input.Created = times.now() input.Expires = input.Created + initDuration(minutes = exp) try: -- cgit v1.2.3