Skip to content

Commit

Permalink
Implement basic shortened URL generation
Browse files Browse the repository at this point in the history
  • Loading branch information
mnito committed Nov 28, 2021
1 parent 2292a04 commit 538a479
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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
8 changes: 6 additions & 2 deletions src/redirectservice.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include "log.hh"
#include "store.hh"
#include "urlgen.hh"

using grpc::Server;
using grpc::ServerBuilder;
Expand Down Expand Up @@ -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;
}
Expand Down
60 changes: 60 additions & 0 deletions src/urlgen.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <algorithm>
#include <chrono>
#include <iostream>
#include <sstream>
#include <tuple>

uint64_t generate_url_id() {
uint64_t url_id = std::chrono::duration_cast<std::chrono::microseconds>(
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;
}
5 changes: 5 additions & 0 deletions src/urlgen.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <string>

uint64_t generate_url_id();
std::string encode_url_id(uint64_t url_id);
uint64_t decode_url_id(std::string url_id_str);

0 comments on commit 538a479

Please sign in to comment.