aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--short.nimble2
-rw-r--r--src/database.nim10
-rw-r--r--src/short.nim22
-rw-r--r--tests/database.nim2
4 files changed, 20 insertions, 16 deletions
diff --git a/short.nimble b/short.nimble
index d2ffc84..2834286 100644
--- a/short.nimble
+++ b/short.nimble
@@ -14,7 +14,7 @@ requires "nim >= 1.4.8",
"https://github.com/dom96/jester#HEAD",
"nimja >= 0.4.1",
"https://github.com/GULPF/tiny_sqlite#HEAD",
- "uuids >= 0.1.11"
+ "nanoid >= 0.2.0"
import os, strformat
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:
diff --git a/tests/database.nim b/tests/database.nim
index 3ebbcca..c763e6f 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: parseUUID("ca69c4b5-75a7-4acc-8384-316d41ef59c7"),
+ Token: "Az0f8uSeK9",
Title: "title",
Url: "url",
Created: testingNow,