-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshortenurl.cpp
More file actions
94 lines (73 loc) · 2.34 KB
/
Copy pathshortenurl.cpp
File metadata and controls
94 lines (73 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <winsock2.h>
#include <windows.h>
#include "cpp-httplib-master/httplib.h"
#include "nlohmann/json.hpp"
using namespace std;
using namespace httplib;
using json = nlohmann::json;
unordered_map<string ,string> shortToLong;
unsigned int generateHash(string longUrl)
{
unsigned int hashValue =0;
for(int i=0;i<longUrl.size();i++)
{
hashValue = (hashValue<<5)+(hashValue^longUrl[i]);
}
return hashValue;
}
string getShortUrlFromHash(unsigned int hashValue){
char base62Arr[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const int base =62;
string shortUrl = "";
do {
unsigned int index = (hashValue%base);
char c = base62Arr[index];
shortUrl.push_back(c);
hashValue /= base;
}while(hashValue >0);
reverse(shortUrl.begin(),shortUrl.end());
return shortUrl;
}
string shortenUrl(string longUrl)
{
unsigned int hashValue = generateHash(longUrl);
cout<<"the hashvalue is"<<hashValue<<endl;
string shortUrl =getShortUrlFromHash(hashValue);
cout<<"the short url is"<<shortUrl<<endl;
shortToLong[shortUrl] = longUrl;
return shortUrl;
}
int main(int argc,char const *argv[])
{
Server server;
server.Post("/shorten",[](const Request& req, Response& res)
{
string longUrl = req.get_param_value("longUrl");
string shortUrl = shortenUrl(longUrl);
shortToLong[shortUrl] = longUrl;
res.set_content("{\"shortUrl\":\"" + shortUrl + "\"}", "application/json");
});
server.Get(R"(/(\w+))", [&](const Request& req, Response& res)
{
string shortUrl = req.matches[1];
string response;
json jsonResponse;
if(shortToLong.find(shortUrl) != shortToLong.end() ){
jsonResponse["url"] = shortToLong[shortUrl];
jsonResponse["statusCode"] = 302;
}
else
{
jsonResponse["url"] = "";
jsonResponse["statusCode"] = 404;
}
res.set_content(jsonResponse.dump(),"application/json");
});
cout<<"Server listening on port 8080"<<endl;
server.listen("localhost",8080);
return 0;
}