add /completion.js file to make it easy to use the server from js

This commit is contained in:
Tobias Lütke 2023-07-02 15:56:10 -04:00
parent 8e1b04d319
commit dd1df3f31c
No known key found for this signature in database
GPG key ID: 1FC0DBB14164709A
6 changed files with 793 additions and 694 deletions

View file

@ -13,6 +13,7 @@
// auto generated files (update with ./deps.sh)
#include "index.html.hpp"
#include "index.js.hpp"
#include "completion.js.hpp"
#ifndef SERVER_VERBOSE
#define SERVER_VERBOSE 1
@ -872,6 +873,7 @@ static void log_server_request(const Request & req, const Response & res) {
});
}
int main(int argc, char ** argv) {
// own arguments required by this example
gpt_params params;
@ -923,6 +925,12 @@ int main(int argc, char ** argv) {
return false;
});
// this is only called if no index.html is found in the public --path
svr.Get("/completion.js", [](const Request &, Response & res) {
res.set_content(reinterpret_cast<const char*>(&completion_js), completion_js_len, "application/javascript");
return false;
});
svr.Post("/completion", [&llama](const Request & req, Response & res) {
auto lock = llama.lock();
@ -1078,6 +1086,12 @@ int main(int argc, char ** argv) {
res.status = 500;
});
svr.set_error_handler([](const Request &, Response & res) {
res.set_content("File Not Found", "text/plain");
res.status = 404;
});
// set timeouts and change hostname and port
svr.set_read_timeout(sparams.read_timeout);
svr.set_write_timeout(sparams.write_timeout);