-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
192 lines (159 loc) · 3.95 KB
/
main.cpp
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "crow_all.h"
#include <fstream>
#include <cctype>
#include <cstddef>
#include <sstream>
#include <vector>
#include <string>
#include <filesystem>
#include <iostream>
#include <fstream>
#include <stdio.h>
typedef struct
{
int index;
std::string url;
std::string name;
std::string email;
}Page;
class Ring
{
private:
int pageCounter = 0;
std::vector<Page*>* pages = new std::vector<Page*>();
int getIndexOfName(std::string name)
{
int requestIndex = -1;
for (int i = 0; i < pages->size(); i++)
{
if ((*pages)[i]->name == name)
{
requestIndex = i;
break;
}
}
return requestIndex;
}
public:
~Ring()
{
for (Page* page : *pages)
{
delete page;
}
delete pages;
}
void collectPages(std::filesystem::path path)
{
std::ifstream* file = new std::ifstream(path);
std::string line;
while (std::getline(*file, line))
{
std::stringstream lineStream(line);
std::string url;
std::string name;
std::getline(lineStream, url, ',');
std::getline(lineStream, name, ',');
printf("index %d: url: %s, name: %s\n", pageCounter, url.c_str(), name.c_str());
Page* page = new Page{pageCounter++, url, name};
pages->push_back(page);
}
}
bool nameIsInRing(std::string name)
{
return getIndexOfName(name) != -1;
}
std::string getRandomUrl()
{
int index = rand() % pages->size();
Page* selectedPage = (*pages)[index];
return selectedPage->url;
}
std::string left(std::string startingName)
{
int leftIndex = getIndexOfName(startingName);
if (leftIndex == -1)
return NULL;
leftIndex--;
if (leftIndex < 0)
leftIndex = pages->size()-1;
return (*pages)[leftIndex]->url;
}
std::string right(std::string startingName)
{
int rightIndex = getIndexOfName(startingName);
if (rightIndex == -1)
return NULL;
rightIndex++;
if (rightIndex > pages->size())
rightIndex = 0;
return (*pages)[rightIndex]->url;
}
};
std::string str_tolower(std::string s)
{
std::transform(s.begin(), s.end(), s.begin(),
[](unsigned char c){ return std::tolower(c); }
);
return s;
}
int main(int argc, char** argv)
{
if (argc < 2)
{
fprintf(stderr, "No file given to collect pages from, check readme.");
return 1;
}
std::filesystem::path path = argv[1];
Ring* ring = new Ring();
ring->collectPages(path);
crow::SimpleApp app;
CROW_ROUTE(app, "/")([ring](){
auto page = crow::mustache::load_text("index.html");
return page;
});
CROW_ROUTE(app, "/random/")([ring](){
crow::response response;
response.redirect(ring->getRandomUrl());
return response;
});
CROW_ROUTE(app, "/<string>/")([](std::string name){
crow::response response;
response.write("You didnt list a direction o_o?");
response.code = 400;
return response;
});
CROW_ROUTE(app, "/<string>/<string>")([ring](std::string name, std::string direction)
{
if (!ring->nameIsInRing(name))
{
crow::response response;
response.write("The name you started your request from isnt in the ring :( \nFeel free to contact aber comp-soc and ask to be added");
response.code = 400;
return response;
}
if (str_tolower(direction) == "left")
{
std::string resultingUrl = ring->left(name);
crow::response response;
response.redirect(resultingUrl);
return response;
}
else if (str_tolower(direction) == "right")
{
std::string resultingUrl = ring->right(name);
crow::response response;
response.redirect(resultingUrl);
return response;
}
else
{
crow::response response;
response.write("You requested an impossible direction around the ring :/");
response.code = 400;
return response;
}
});
app.port(18080).run();
delete ring;
}