Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 4bd035b

Browse files
committed
feat: nitro add debug build
1 parent b1e1e59 commit 4bd035b

File tree

4 files changed

+86
-39
lines changed

4 files changed

+86
-39
lines changed

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ if(LLAMA_CUBLAS)
3232
add_compile_definitions(GGML_USE_CUBLAS)
3333
endif()
3434
endif()
35+
36+
if(DEBUG)
37+
message(STATUS "NITRO DEBUG IS ON")
38+
add_compile_definitions(ALLOW_ALL_CORS)
39+
endif()
40+
3541
add_subdirectory(llama.cpp)
3642
add_executable(${PROJECT_NAME} main.cc)
3743

controllers/health.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#include "health.h"
2+
#include "utils/nitro_utils.h"
23

34
void health::asyncHandleHttpRequest(
45
const HttpRequestPtr &req,
56
std::function<void(const HttpResponsePtr &)> &&callback) {
6-
auto resp = HttpResponse::newHttpResponse();
7+
auto resp = nitro_utils::nitroHttpResponse();
78
resp->setStatusCode(k200OK);
89
resp->setContentTypeCode(CT_TEXT_HTML);
910
resp->setBody("Nitro is alive!!!");

controllers/llamaCPP.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void llamaCPP::chatCompletion(
4545
if (!model_loaded) {
4646
Json::Value jsonResp;
4747
jsonResp["message"] = "Model is not loaded yet";
48-
auto resp = drogon::HttpResponse::newHttpJsonResponse(jsonResp);
48+
auto resp = nitro_utils::nitroHttpJsonResponse(jsonResp);
4949
resp->setStatusCode(drogon::k500InternalServerError);
5050
callback(resp);
5151
return;
@@ -205,8 +205,8 @@ void llamaCPP::chatCompletion(
205205
return 0;
206206
};
207207

208-
auto resp = drogon::HttpResponse::newStreamResponse(chunked_content_provider,
209-
"chat_completions.txt");
208+
auto resp = nitro_utils::nitroStreamResponse(chunked_content_provider,
209+
"chat_completions.txt");
210210
callback(resp);
211211
}
212212

@@ -216,7 +216,7 @@ void llamaCPP::embedding(
216216
if (!model_loaded) {
217217
Json::Value jsonResp;
218218
jsonResp["message"] = "Model is not loaded yet";
219-
auto resp = drogon::HttpResponse::newHttpJsonResponse(jsonResp);
219+
auto resp = nitro_utils::nitroHttpJsonResponse(jsonResp);
220220
resp->setStatusCode(drogon::k500InternalServerError);
221221
callback(resp);
222222
return;
@@ -282,13 +282,13 @@ void llamaCPP::loadModel(
282282
LOG_ERROR << "Error loading the model will exit the program";
283283
Json::Value jsonResp;
284284
jsonResp["message"] = "Model loaded failed";
285-
auto resp = drogon::HttpResponse::newHttpJsonResponse(jsonResp);
285+
auto resp = nitro_utils::nitroHttpJsonResponse(jsonResp);
286286
resp->setStatusCode(drogon::k500InternalServerError);
287287
callback(resp);
288288
}
289289
Json::Value jsonResp;
290290
jsonResp["message"] = "Model loaded successfully";
291291
model_loaded = true;
292-
auto resp = drogon::HttpResponse::newHttpJsonResponse(jsonResp);
292+
auto resp = nitro_utils::nitroHttpJsonResponse(jsonResp);
293293
callback(resp);
294294
}

utils/nitro_utils.h

Lines changed: 72 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
#include "cstdio"
33
#include "random"
44
#include "string"
5+
#include <algorithm>
6+
#include <drogon/HttpResponse.h>
57
#include <iostream>
68
#include <ostream>
7-
#include <algorithm>
89

910
namespace nitro_utils {
1011
inline std::string generate_random_string(std::size_t length) {
@@ -23,39 +24,78 @@ inline std::string generate_random_string(std::size_t length) {
2324
return random_string;
2425
}
2526

26-
inline void nitro_logo(){
27-
std::string rainbowColors[] = {
28-
"\033[93m", // Yellow
29-
"\033[94m", // Blue
30-
};
31-
32-
std::string resetColor = "\033[0m";
33-
std::string asciiArt =
34-
" ___ ___ ___ \n"
35-
" /__/\ ___ ___ / /\\ / /\\ \n"
36-
" \\ \\:\\ / /\\ / /\\ / /::\\ / /::\\ \n"
37-
" \\ \\:\\ / /:/ / /:/ / /:/\\:\\ / /:/\\:\\ \n"
38-
" _____\\__\\:\\ /__/::\\ / /:/ / /:/ \\:\\ / /:/ \\:\\ \n"
39-
" /__/::::::::\\ \\__\\/\\:\\__ / /::\\ /__/:/ /:/___ /__/:/ \\__\\:\\\n"
40-
" \\ \\:\\~~\\~~\\/ \\ \\:\\/\\ /__/:/\\:\\ \\ \\:\\/:::::/ \\ \\:\\ / /:/\n"
41-
" \\ \\:\\ ~~~ \\__\\::/ \\__\\/ \\:\\ \\ \\::/~~~~ \\ \\:\\ /:/ \n"
42-
" \\ \\:\\ /__/:/ \\ \\:\\ \\ \\:\\ \\ \\:\\/:/ \n"
43-
" \\ \\:\\ \\__\\/ \\__\\/ \\ \\:\\ \\ \\::/ \n"
44-
" \\__\\/ \\__\\/ \\__\\/ \n";
45-
46-
int colorIndex = 0;
47-
48-
for (char c : asciiArt) {
49-
if (c == '\n') {
50-
std::cout << resetColor << c;
51-
colorIndex = 0;
52-
} else {
53-
std::cout << rainbowColors[colorIndex % 2] << c;
54-
colorIndex++;
55-
}
27+
inline void nitro_logo() {
28+
std::string rainbowColors[] = {
29+
"\033[93m", // Yellow
30+
"\033[94m", // Blue
31+
};
32+
33+
std::string resetColor = "\033[0m";
34+
std::string asciiArt =
35+
" ___ ___ ___ \n"
36+
" /__/\ ___ ___ / /\\ / /\\ \n"
37+
" \\ \\:\\ / /\\ / /\\ / /::\\ / /::\\ "
38+
" \n"
39+
" \\ \\:\\ / /:/ / /:/ / /:/\\:\\ / /:/\\:\\ "
40+
" \n"
41+
" _____\\__\\:\\ /__/::\\ / /:/ / /:/ \\:\\ / /:/ "
42+
"\\:\\ \n"
43+
" /__/::::::::\\ \\__\\/\\:\\__ / /::\\ /__/:/ /:/___ /__/:/ "
44+
"\\__\\:\\\n"
45+
" \\ \\:\\~~\\~~\\/ \\ \\:\\/\\ /__/:/\\:\\ \\ \\:\\/:::::/ \\ "
46+
"\\:\\ / /:/\n"
47+
" \\ \\:\\ ~~~ \\__\\::/ \\__\\/ \\:\\ \\ \\::/~~~~ \\ "
48+
"\\:\\ /:/ \n"
49+
" \\ \\:\\ /__/:/ \\ \\:\\ \\ \\:\\ \\ "
50+
"\\:\\/:/ \n"
51+
" \\ \\:\\ \\__\\/ \\__\\/ \\ \\:\\ \\ "
52+
"\\::/ \n"
53+
" \\__\\/ \\__\\/ \\__\\/ "
54+
"\n";
55+
56+
int colorIndex = 0;
57+
58+
for (char c : asciiArt) {
59+
if (c == '\n') {
60+
std::cout << resetColor << c;
61+
colorIndex = 0;
62+
} else {
63+
std::cout << rainbowColors[colorIndex % 2] << c;
64+
colorIndex++;
5665
}
66+
}
67+
68+
std::cout << resetColor; // Reset color at the endreturn;
69+
}
70+
71+
inline drogon::HttpResponsePtr nitroHttpResponse() {
72+
auto resp = drogon::HttpResponse::newHttpResponse();
73+
#ifdef ALLOW_ALL_CORS
74+
LOG_INFO << "Respond for all cors!";
75+
resp->addHeader("Access-Control-Allow-Origin", "*");
76+
#endif
77+
return resp;
78+
}
79+
80+
inline drogon::HttpResponsePtr nitroHttpJsonResponse(const Json::Value &data) {
81+
auto resp = drogon::HttpResponse::newHttpJsonResponse(data);
82+
#ifdef ALLOW_ALL_CORS
83+
LOG_INFO << "Respond for all cors!";
84+
resp->addHeader("Access-Control-Allow-Origin", "*");
85+
#endif
86+
return resp;
87+
};
5788

58-
std::cout << resetColor; // Reset color at the endreturn;
89+
inline drogon::HttpResponsePtr nitroStreamResponse(
90+
const std::function<std::size_t(char *, std::size_t)> &callback,
91+
const std::string &attachmentFileName = "") {
92+
auto resp =
93+
drogon::HttpResponse::newStreamResponse(callback, attachmentFileName);
94+
#ifdef ALLOW_ALL_CORS
95+
LOG_INFO << "Respond for all cors!";
96+
resp->addHeader("Access-Control-Allow-Origin", "*");
97+
#endif
98+
return resp;
5999
}
60100

61101
} // namespace nitro_utils

0 commit comments

Comments
 (0)