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 /src/short.nim | |
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 'src/short.nim')
-rw-r--r-- | src/short.nim | 23 |
1 files changed, 14 insertions, 9 deletions
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 "/": |