Skip to content

Instantly share code, notes, and snippets.

@hokamoto
Last active April 7, 2021 16:47
Show Gist options
  • Save hokamoto/725d7d893bdbc42381664e0181d4e843 to your computer and use it in GitHub Desktop.
Save hokamoto/725d7d893bdbc42381664e0181d4e843 to your computer and use it in GitHub Desktop.
URL Shortener in Nim
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