Last active
April 7, 2021 16:47
-
-
Save hokamoto/725d7d893bdbc42381664e0181d4e843 to your computer and use it in GitHub Desktop.
URL Shortener in Nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jester, tables, md5 | |
var urls {.threadvar.}: Table[string, string] | |
routes: | |
get "/": | |
if @"url" != "": | |
let id = ($(@"url".toMD5)).substr(0, 5) | |
urls[id] = @"url" | |
resp id | |
else: | |
resp Http204, "" | |
get "/@id": | |
if urls.hasKey(@"id") == true: | |
resp Http301, [(key: "location", value: urls[@"id"])], "" | |
else: | |
resp Http404, "No key" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment