From 538a4792470b832212869ddaf7ccd92bc9028f14 Mon Sep 17 00:00:00 2001 From: mnito Date: Sun, 28 Nov 2021 12:04:35 -0500 Subject: [PATCH] Implement basic shortened URL generation --- Makefile | 5 ++++ src/redirectservice.cc | 8 ++++-- src/urlgen.cc | 60 ++++++++++++++++++++++++++++++++++++++++++ src/urlgen.hh | 5 ++++ 4 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 src/urlgen.cc create mode 100644 src/urlgen.hh diff --git a/Makefile b/Makefile index 4ab9e17..662edc3 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,9 @@ redirectservice_dev: -ldl \ gen/redirectservice.grpc.pb.cc \ gen/redirectservice.pb.cc \ + src/log.cc \ + src/store.cc \ + src/urlgen.cc \ src/redirectservice.cc \ -o build/circlet_redirectservice.dev @@ -39,6 +42,7 @@ redirectservice_release: gen/redirectservice.pb.cc \ src/log.cc \ src/store.cc \ + src/urlgen.cc \ src/redirectservice.cc \ -o build/circlet_redirectservice.dev @@ -54,5 +58,6 @@ redirectservice_dev_mike: gen/redirectservice.pb.cc \ src/log.cc \ src/store.cc \ + src/urlgen.cc \ src/redirectservice.cc \ -o ~/circlet_redirectservice.dev diff --git a/src/redirectservice.cc b/src/redirectservice.cc index 8fc6f8b..279da7c 100644 --- a/src/redirectservice.cc +++ b/src/redirectservice.cc @@ -26,6 +26,7 @@ #include "log.hh" #include "store.hh" +#include "urlgen.hh" using grpc::Server; using grpc::ServerBuilder; @@ -71,10 +72,13 @@ class RedirectServiceImpl final : public RedirectService::Service { ); } - this->get_store()->create_redirect(from_location, "/test"); + // TODO Make URL generation configurable + auto to_location = "/" + encode_url_id(generate_url_id()); + + this->get_store()->create_redirect(from_location, to_location); response->set_from_location(from_location); - response->set_to_location("/test"); + response->set_to_location(to_location); return Status::OK; } diff --git a/src/urlgen.cc b/src/urlgen.cc new file mode 100644 index 0000000..3214c34 --- /dev/null +++ b/src/urlgen.cc @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#include + +uint64_t generate_url_id() { + uint64_t url_id = std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch() + ).count(); + + return url_id; +} + +std::string a = "0123456789befhjmnrstvwyz"; +size_t a_size = 24; + +std::string encode_url_id(uint64_t url_id) { + std::stringstream s; + while (url_id) { + s << a[url_id % a_size]; + url_id /= a_size; + } + auto str = s.str(); + std::reverse(str.begin(), str.end()); + return str; +} + +static uint64_t pow24_cache[14] = { + 1, + 24, + 576, + 13824, + 331776, + 7962624, + 191102976, + 4586471424, + 110075314176, + 2641807540224, + 63403380965376, + 1521681143169024, + 36520347436056576, + 876488338465357824, +}; + +uint64_t pow24(int n) { + if (n > 13) return -1; + return pow24_cache[n]; +} + +uint64_t decode_url_id(std::string url_id_str) { + uint64_t id = 0; + + size_t i = 0; + for (auto &l : url_id_str) { + id += a.find(l) * pow24(url_id_str.size() - (i + 1)); + i++; + } + return id; +} diff --git a/src/urlgen.hh b/src/urlgen.hh new file mode 100644 index 0000000..90f2341 --- /dev/null +++ b/src/urlgen.hh @@ -0,0 +1,5 @@ +#include + +uint64_t generate_url_id(); +std::string encode_url_id(uint64_t url_id); +uint64_t decode_url_id(std::string url_id_str);