From ad9b9c0f7bd4d95ddc54462970d33d92bab9392c Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Fri, 22 Oct 2021 17:59:44 +0200 Subject: Added a functionning url shortening service --- src/database.nim | 79 +++++++++++++++++++++++++ src/dbUtils.nim | 13 +++++ src/short.nim | 115 +++++++++++++++++++++++++++++++++++++ src/templates/error.html | 7 +++ src/templates/index.html | 25 ++++++++ src/templates/noshort.html | 10 ++++ src/templates/partials/footer.html | 5 ++ src/templates/partials/master.html | 17 ++++++ src/templates/short.html | 9 +++ 9 files changed, 280 insertions(+) create mode 100644 src/database.nim create mode 100644 src/dbUtils.nim create mode 100644 src/short.nim create mode 100644 src/templates/error.html create mode 100644 src/templates/index.html create mode 100644 src/templates/noshort.html create mode 100644 src/templates/partials/footer.html create mode 100644 src/templates/partials/master.html create mode 100644 src/templates/short.html (limited to 'src') diff --git a/src/database.nim b/src/database.nim new file mode 100644 index 0000000..8094f31 --- /dev/null +++ b/src/database.nim @@ -0,0 +1,79 @@ +import tiny_sqlite +import std / [options, times] + +import dbUtils + +const migrations = [ + """ + CREATE TABLE schema_version ( + version INTEGER NOT NULL + ); + CREATE TABLE url ( + id INTEGER PRIMARY KEY, + token TEXT NOT NULL UNIQUE, + title TEXT NOT NULL, + url TEXT, + created DATE, + expires DATE + ); + CREATE UNIQUE INDEX idx_url_token ON url(token); + """ +] +const latestVersion = migrations.len + +proc Migrate*(db: DbConn): bool {.raises: [].} = + var currentVersion : int + try: + currentVersion = db.value("SELECT version FROM schema_version;").get().fromDbValue(int) + except SqliteError: + discard + if currentVersion != latestVersion: + try: + db.exec("BEGIN") + for v in currentVersion.. 527040: + echo "exp" + return (Http400, renderError(400, "Bad Request")) + of "shorten": discard + else: return (Http400, renderError(400, "Bad Request")) + if input.Title == "" or input.Url == "" or exp == 0: + echo "empty" + return (Http400, renderError(400, "Bad Request")) + input.Token = $genOid() + 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) + +routes: + get "/": + resp renderIndex() + post "/": + initDB() + var (code, content) = handleIndexPost(request.params) + if code != Http200: + resp code, content + else: + redirect("/" & content) + get "/static/favicon.ico": + resp Http200, {"content-type": "image/x-icon"}, favicon + get re"^/static/all\.css\.": + resp Http200, {"content-type": "text/css"}, allcss + get "/@token": + initDB() + var (code, content) = handleToken(@"token") + resp code, content + +runForever() diff --git a/src/templates/error.html b/src/templates/error.html new file mode 100644 index 0000000..5de1923 --- /dev/null +++ b/src/templates/error.html @@ -0,0 +1,7 @@ +{% extends "templates/partials/master.html" %} +{% block content %} +

{{ $code }} - {{ $msg }}

+

+ Go back +

+{% endblock %} diff --git a/src/templates/index.html b/src/templates/index.html new file mode 100644 index 0000000..b89e381 --- /dev/null +++ b/src/templates/index.html @@ -0,0 +1,25 @@ +{% extends "templates/partials/master.html" %} +{% block content %} +

URL shortener

+

+The simple, open source and privacy friendly URL shortener : anonymous usage, no tracking.
+This is a personal sharing service: Data may be deleted anytime. Don't share illegal, unethical or morally reprehensible content. +

+
+
+
+ + + +
+ +{% endblock %} diff --git a/src/templates/noshort.html b/src/templates/noshort.html new file mode 100644 index 0000000..e0be436 --- /dev/null +++ b/src/templates/noshort.html @@ -0,0 +1,10 @@ +{% extends "templates/partials/master.html" %} +{% block content %} +

URL not found!

+

+ This url does not exist or has expired, sorry! +

+

+ Go back +

+{% endblock %} diff --git a/src/templates/partials/footer.html b/src/templates/partials/footer.html new file mode 100644 index 0000000..6ae4459 --- /dev/null +++ b/src/templates/partials/footer.html @@ -0,0 +1,5 @@ + diff --git a/src/templates/partials/master.html b/src/templates/partials/master.html new file mode 100644 index 0000000..82cedac --- /dev/null +++ b/src/templates/partials/master.html @@ -0,0 +1,17 @@ + + + + + + + + + short.adyxax.org + + +
+ {% block content %}{% endblock %} +
+ {% importnwt "templates/partials/footer.html" %} + + diff --git a/src/templates/short.html b/src/templates/short.html new file mode 100644 index 0000000..1f36fdf --- /dev/null +++ b/src/templates/short.html @@ -0,0 +1,9 @@ +{% extends "templates/partials/master.html" %} +{% block content %} +

{{ $req.Title }}

+

{{ $req.Url }}

+

+Created on : {{ $req.Created }}
+Expires on : {{ $req.Expires }} +

+{% endblock %} -- cgit v1.2.3