-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
101 lines (82 loc) · 3.23 KB
/
Copy pathmain.cpp
File metadata and controls
101 lines (82 loc) · 3.23 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
95
96
97
98
99
100
101
#include <iostream>
#include "lib/server.h"
#include "lib/environment.h"
#include "lib/nlohmann/json.hpp" // https://github.com/nlohmann/json
// #include "lib/http.h"
using json = nlohmann::json;
std::string exec(const char *cmd)
{
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
if (!pipe)
{
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
{
result += buffer.data();
}
return result;
}
int main(){
Server::Server server;
Server::Router router;
Envir::Environment env("./.env");
server.set_logs(true);
server.set_worker_counts(3);
server.set_ip(env.get_env("HOST").second);
server.set_port(stoi(env.get_env("PORT").second, nullptr, 10));
server.bind_server();
server.set_router(&router);
router.GET("/", [](Request request, Response response){
// response.set_status_code(payload::StatusCode::OK);
// response.set_response_content_type("html");
// response.set_response_body("<html><head><script src='https://cdn.tailwindcss.com'></script></head><body class='h-screen w-screen bg-red-500'> hello world </body></html>");
response.set_html_content("./src/index.html");
return response;
});
router.GET("/test", [](Request request, Response response){
// json json_body;
// json_body["message"] = "hello world";
// json_body["status"] = "ok";
// cout << json_body.dump() << endl;
// response.set_response_json(json_body);
// HTTPResponse res = http_fetch("127.0.0.1", "/", "GET", "", 8080);
return response;
});
router.POST("/api/gemini/prompt", [](Request request, Response response)
{
/**
* Input: prompt, language
* Output: response text
*/
json data = request.json_body;
if(data["prompt"].is_null()){
response.set_response_status(payload::StatusCode::NO_CONTENT);
response.set_response_body("Prompt is required");
response.set_response_content_type("text");
return response;
}else{
string language;
if(!data["language"].is_null()){
language = data["language"];
}
Envir::Environment env("./.env");
string prompt = data["prompt"];
string url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=" + env.get_env("GEMINI_API_KEY").second;
string parts = "{ \"contents\" : [{ \"parts\": [{ \"text\" : \"" + prompt + "\"}] }]}";
string command = "curl \"" + url + "\" -X POST -H 'Content-Type: application/json' -d '" + parts + "'";
std::string output = exec(command.c_str());
std::regex text_regex(R"(\"text\":\s*\"([^"]+)\")");
std::smatch match;
regex_search(output, match, text_regex);
json response_json;
response_json["response"] = match[1];
response.set_response_json(response_json);
return response;
}
});
server.listen_server();
return 0;
}