Commit ·
7237cad
1
Parent(s): 76cdce6
Direct access to s3 bucket
Browse files- server.cpp +42 -22
server.cpp
CHANGED
|
@@ -14,6 +14,7 @@
|
|
| 14 |
#include "threadpool.h"
|
| 15 |
#include <sstream>
|
| 16 |
#include <chrono>
|
|
|
|
| 17 |
#define CROW_MAIN
|
| 18 |
|
| 19 |
using std::string;
|
|
@@ -26,8 +27,15 @@ namespace fs = std::filesystem;
|
|
| 26 |
namespace rv = std::ranges::views;
|
| 27 |
|
| 28 |
constexpr string to_st(const auto& i){ std::stringstream ss; ss << i; return ss.str(); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
static inline
|
| 31 |
std::stringstream ss;
|
| 32 |
std::hash<string> h;
|
| 33 |
const auto t0 = std::chrono::system_clock::now();
|
|
@@ -48,7 +56,7 @@ static inline string exec(const char* cmd) {
|
|
| 48 |
return result;
|
| 49 |
}
|
| 50 |
|
| 51 |
-
constexpr auto reify(
|
| 52 |
|
| 53 |
constexpr string strip(const string& s){ return s; }
|
| 54 |
|
|
@@ -118,29 +126,41 @@ int main(){
|
|
| 118 |
CROW_LOG_INFO << name << " queued with prompt: " << prompt;
|
| 119 |
};
|
| 120 |
|
| 121 |
-
CROW_ROUTE(app, "/create")
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
| rv::transform([](const guy& i){ return i[0] + ": '" + i[1] + "'"; }));
|
| 140 |
crow::json::wvalue ret;
|
| 141 |
-
|
|
|
|
|
|
|
| 142 |
return ret;
|
| 143 |
});
|
| 144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
app.port(80).run();
|
| 146 |
}
|
|
|
|
| 14 |
#include "threadpool.h"
|
| 15 |
#include <sstream>
|
| 16 |
#include <chrono>
|
| 17 |
+
#include <unordered_map>
|
| 18 |
#define CROW_MAIN
|
| 19 |
|
| 20 |
using std::string;
|
|
|
|
| 27 |
namespace rv = std::ranges::views;
|
| 28 |
|
| 29 |
constexpr string to_st(const auto& i){ std::stringstream ss; ss << i; return ss.str(); }
|
| 30 |
+
template<typename T>
|
| 31 |
+
constexpr T st_to(const string& s){
|
| 32 |
+
T t(0);
|
| 33 |
+
std::stringstream ss(s);
|
| 34 |
+
ss >> t;
|
| 35 |
+
return t;
|
| 36 |
+
}
|
| 37 |
|
| 38 |
+
static inline string uid(const std::string& s){
|
| 39 |
std::stringstream ss;
|
| 40 |
std::hash<string> h;
|
| 41 |
const auto t0 = std::chrono::system_clock::now();
|
|
|
|
| 56 |
return result;
|
| 57 |
}
|
| 58 |
|
| 59 |
+
constexpr auto reify(std::ranges::range auto&& r){ return vector(r.begin(), r.end()); }
|
| 60 |
|
| 61 |
constexpr string strip(const string& s){ return s; }
|
| 62 |
|
|
|
|
| 126 |
CROW_LOG_INFO << name << " queued with prompt: " << prompt;
|
| 127 |
};
|
| 128 |
|
| 129 |
+
CROW_ROUTE(app, "/create")([=](const crow::request& req){
|
| 130 |
+
crow::json::wvalue ret;
|
| 131 |
+
if(auto prompt = req.url_params.get("prompt"); prompt == nullptr){
|
| 132 |
+
CROW_LOG_INFO << "No prompt specified";
|
| 133 |
+
ret["error"] = "No prompt given";
|
| 134 |
+
} else {
|
| 135 |
+
CROW_LOG_INFO << prompt << " commissioned";
|
| 136 |
+
auto id = uid(prompt);
|
| 137 |
+
enqueue({to_st(id), strip(prompt, '\'', '\"')});
|
| 138 |
+
pool->enqueue(training_loop);
|
| 139 |
+
CROW_LOG_INFO << "Launched training loop";
|
| 140 |
+
ret["id"] = id;
|
| 141 |
+
}
|
| 142 |
+
return ret;
|
| 143 |
+
});
|
| 144 |
+
|
| 145 |
+
CROW_ROUTE(app, "/list")([=](){
|
| 146 |
+
auto l = splitOn(run("sh list.sh"), "\n");
|
|
|
|
| 147 |
crow::json::wvalue ret;
|
| 148 |
+
for(int k = 0; auto& [i, p] : q_to_v(*commissions))
|
| 149 |
+
ret["pending"][k++] = {{ "id", i }, { "prompt", p}};
|
| 150 |
+
ret["finished"] = l;
|
| 151 |
return ret;
|
| 152 |
});
|
| 153 |
|
| 154 |
+
CROW_ROUTE(app, "/get/<string>")([=](const crow::request& req, crow::response& res, const string n){
|
| 155 |
+
if(auto l = reify( splitOn(run("sh list.sh"), "\n") | rv::filter([=](const string& s){ return s == n; })); !l.empty())
|
| 156 |
+
res.redirect(string("https://s3.us-west-2.amazonaws.com/models.webaverse.com/") + n + ".glb");
|
| 157 |
+
else if(auto q = reify( q_to_v(*commissions)
|
| 158 |
+
| rv::filter([=](const guy& i){ return i[0] == n; })); !q.empty())
|
| 159 |
+
res.code = 209;
|
| 160 |
+
else
|
| 161 |
+
res.code = 404;
|
| 162 |
+
res.end();
|
| 163 |
+
});
|
| 164 |
+
|
| 165 |
app.port(80).run();
|
| 166 |
}
|