diff options
author | Julien Dessaux | 2021-10-29 21:13:53 +0200 |
---|---|---|
committer | Julien Dessaux | 2021-10-29 21:13:53 +0200 |
commit | 785ef2a9135281e4ec67662ef60c27a320f22b9b (patch) | |
tree | ebe8917b9c192b80baff8879d951474fa209d783 | |
parent | Added gitignore (diff) | |
download | short-785ef2a9135281e4ec67662ef60c27a320f22b9b.tar.gz short-785ef2a9135281e4ec67662ef60c27a320f22b9b.tar.bz2 short-785ef2a9135281e4ec67662ef60c27a320f22b9b.zip |
Changed for a better way to generate uuids
Diffstat (limited to '')
-rw-r--r-- | short.nimble | 3 | ||||
-rw-r--r-- | src/database.nim | 12 | ||||
-rw-r--r-- | src/short.nim | 23 | ||||
-rw-r--r-- | tests/database.nim | 8 |
4 files changed, 27 insertions, 19 deletions
diff --git a/short.nimble b/short.nimble index ab89b75..7aba695 100644 --- a/short.nimble +++ b/short.nimble @@ -13,7 +13,8 @@ bin = @["short"] requires "nim >= 1.4.8", "https://github.com/dom96/jester#HEAD", "nimja >= 0.4.1", - "https://github.com/GULPF/tiny_sqlite#HEAD" + "https://github.com/GULPF/tiny_sqlite#HEAD", + "uuids >= 0.1.11" import os, strformat diff --git a/src/database.nim b/src/database.nim index 1ffc0df..37ef8b4 100644 --- a/src/database.nim +++ b/src/database.nim @@ -1,6 +1,8 @@ -import tiny_sqlite import std / [options, times] +import tiny_sqlite +import uuids + import dbUtils const migrations = [ @@ -47,7 +49,7 @@ proc Migrate*(db: DbConn): bool {.raises: [].} = type ShortUrl* = object ID*: int - Token*: string + Token*: UUID Title*: string Url*: string Created*: DateTime @@ -58,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: string): ref ShortUrl {.raises: [SqliteError].} = +proc GetUrl*(db: DbConn, token: UUID): 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 8f243c6..5005e16 100644 --- a/src/short.nim +++ b/src/short.nim @@ -1,9 +1,10 @@ import os, strutils -import std/[hashes, oids, re, times, uri] +import std/[hashes, re, times, uri] import tiny_sqlite import jester import nimja/parser +import uuids import database @@ -37,13 +38,12 @@ 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(tokenStr: string): (HttpCode, string) {.raises: [].} = + var token: UUID try: - let tokenRegexp = re"^[\w]{24}$" - if not match(token, tokenRegexp): - return (Http400, renderError(400, "Bad Request")) - except RegexError: - return (Http500, renderError(500, "RegexError")) + token = parseUUID(tokenStr) + except ValueError: + return (Http400, renderError(400, "Bad Request")) db.CleanExpired() try: let req = db.GetUrl(token) @@ -84,14 +84,19 @@ proc handleIndexPost(params: Table[string, string]): (HttpCode, string) {.raises else: return (Http400, renderError(400, "Bad Request")) if input.Title == "" or input.Url == "" or exp == 0: return (Http400, renderError(400, "Bad Request")) - input.Token = $genOid() + try: + input.Token = genUUID() + except IOError: + return (Http500, renderError(500, "IOError on genUUID")) + except OSError: + return (Http500, renderError(500, "OSError on genUUID")) input.Created = times.now() input.Expires = input.Created + initDuration(minutes = exp) try: db.AddUrl(input) except SqliteError: return (Http500, renderShort(input)) - return (Http200, input.Token) + return (Http200, $input.Token) routes: get "/": diff --git a/tests/database.nim b/tests/database.nim index 6fa47c5..3ebbcca 100644 --- a/tests/database.nim +++ b/tests/database.nim @@ -11,7 +11,7 @@ suite "database": let db = openDatabase(":memory:") check db.Migrate() == true let u = ShortUrl( - Token: "token", + Token: parseUUID("ca69c4b5-75a7-4acc-8384-316d41ef59c7"), Title: "title", Url: "url", Created: testingNow, @@ -25,17 +25,17 @@ suite "database": discard var u2 = db.GetUrl(u.Token) check u2.ID == 1 - check u2.Token == "token" + check u2.Token == u.Token check u2.Title == "title" check u2.Url == "url" check u2.Created - testingNow < someTime check u2.Expires - later < someTime db.CleanExpired() try: - discard db.GetUrl("token") + discard db.GetUrl(u.Token) except SqliteError: check false u2.Expires = testingNow + 120 * someTime db.AddUrl(u2[]) db.CleanExpired() - check db.GetUrl("token") != nil + check db.GetUrl(u.Token) != nil |