smollerGraph 0.20.2
A small graph database, from scratch
Loading...
Searching...
No Matches
server.cc
Go to the documentation of this file.
2
3#include <httplib.h>
4
5#include <iostream>
6#include <string>
7
8#include "absl/status/status.h"
9#include "absl/status/statusor.h"
10#include "absl/strings/str_cat.h"
11#include "smollergraph/api.h"
12
13namespace smollergraph {
14namespace {
15
16// Sends JSON, or an error as JSON, with the HTTP status that goes with the
17// error's code: 404 for something that isn't there, and 400 for a request
18// that's wrong.
19void Send(const absl::StatusOr<std::string>& json,
20 httplib::Response& response) {
21 if (!json.ok()) {
22 response.status = absl::IsNotFound(json.status()) ? 404 : 400;
23 response.set_content(ErrorJson(json.status().message()),
24 "application/json");
25 return;
26 }
27 response.set_content(*json, "application/json");
28}
29
30} // namespace
31
32absl::Status Serve(const Graph& graph, const std::string& address, int port) {
33 httplib::Server server;
34 // cpp-httplib lets several servers share a port by default, so a second sg
35 // serve on a busy port would start without a word. SO_REUSEADDR alone lets
36 // a restarted server have its port straight back, and refuses a busy one.
37 server.set_socket_options([](socket_t fd) {
38 httplib::set_socket_opt(fd, SOL_SOCKET, SO_REUSEADDR, 1);
39 });
40 // The UI's files are served from the source tree, so changing one needs a
41 // reload in the browser, not a new build. No cache tells the browser to ask
42 // whether a file has changed before it uses its copy.
43 if (!server.set_mount_point("/", SMOLLERGRAPH_UI,
44 {{"Cache-Control", "no-cache"}})) {
45 return absl::NotFoundError(
46 absl::StrCat("there's no folder ", SMOLLERGRAPH_UI));
47 }
48 // The server answers requests on several threads at once. Nothing writes to
49 // the graph while it's served, and the buffer pools' latches keep their
50 // reads from getting in each other's way.
51 server.Get("/api/names", [&graph](const httplib::Request& request,
52 httplib::Response& response) {
53 response.set_content(NamesJson(graph, request.get_param_value("prefix")),
54 "application/json");
55 });
56 server.Get("/api/query", [&graph](const httplib::Request& request,
57 httplib::Response& response) {
58 Send(QueryJson(graph, request.get_param_value("q")), response);
59 });
60 server.Get(R"(/api/node/(\d+))", [&graph](const httplib::Request& request,
61 httplib::Response& response) {
62 Send(NodeJson(graph, request.matches[1].str()), response);
63 });
64 server.Get(R"(/api/similar/(\d+))", [&graph](const httplib::Request& request,
65 httplib::Response& response) {
66 Send(SimilarJson(graph, request.matches[1].str()), response);
67 });
68 if (!server.bind_to_port(address, port)) {
69 return absl::UnavailableError(
70 absl::StrCat("can't listen on ", address, " port ", port));
71 }
72 // std::endl flushes, so the line shows straight away in Docker's logs too.
73 std::cout << "Serving the graph on http://localhost:" << port << std::endl;
74 if (!server.listen_after_bind()) {
75 return absl::InternalError("stopped listening");
76 }
77 return absl::OkStatus();
78}
79
80} // namespace smollergraph
A graph kept in three files of records, one for nodes, one for relationships and one for their proper...
Definition graph.h:113
absl::StatusOr< std::string > SimilarJson(const Graph &graph, std::string_view id)
Returns the kSimilar nodes whose embeddings are most like node id's, the most alike first,...
Definition api.cc:234
absl::StatusOr< std::string > QueryJson(const Graph &graph, std::string_view text)
Definition api.cc:158
absl::StatusOr< std::string > NodeJson(const Graph &graph, std::string_view id)
Returns node id's record as it's kept on disk: its label, its page, its offset in the page and its by...
Definition api.cc:130
std::string ErrorJson(std::string_view message)
Returns an error's message as JSON, for the UI to show.
Definition api.cc:126
std::string NamesJson(const Graph &graph, std::string_view prefix)
Returns a few of the graph's names that start with prefix, in order, for the UI to suggest.
Definition api.cc:116
absl::Status Serve(const Graph &graph, const std::string &address, int port)
Answers HTTP requests for the graph on address and port, until the process is stopped.
Definition server.cc:32