-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbind_http_server.cpp
More file actions
64 lines (56 loc) · 2.7 KB
/
Copy pathbind_http_server.cpp
File metadata and controls
64 lines (56 loc) · 2.7 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
#include "bindings.hpp"
#include <odr/file.hpp>
#include <odr/html.hpp>
#include <odr/http_server.hpp>
#include <pybind11/stl.h>
namespace py = pybind11;
void odr_python::bind_http_server(py::module_ &m) {
m.attr("has_http_server") = true;
py::class_<odr::HttpServer> server(m, "HttpServer",
"Serves translated files over HTTP.");
py::class_<odr::HttpServer::Config>(
server, "Config",
"Server-wide settings. Empty since the cache path went with "
"`serve_file`: "
"what a service was translated into belongs to whoever translated it.")
.def(py::init<>());
py::class_<odr::HttpServer::Options>(server, "Options",
"Socket options for `bind`.")
.def(py::init<>())
.def_readwrite("reuse_address", &odr::HttpServer::Options::reuse_address)
.def_readwrite("reuse_port", &odr::HttpServer::Options::reuse_port);
server
.def(py::init([](const odr::HttpServer::Config &config) {
return odr::HttpServer(config);
}),
py::arg("config") = odr::HttpServer::Config{})
.def("connect_service", &odr::HttpServer::connect_service,
py::arg("service"), py::arg("prefix"))
.def(
"bind",
[](const odr::HttpServer &self, const std::string &host,
const std::uint32_t port,
const odr::HttpServer::Options &options) {
return self.bind(host, port, options);
},
py::arg("host"), py::arg("port"),
py::arg("options") = odr::HttpServer::Options{},
"Bind the socket, `port` 0 for any free one; returns the port it "
"got. Connections are accepted from here on, before `listen` runs.")
// Release the GIL: `listen` blocks and `stop` must remain callable from
// other Python threads. `stop` waits for the listen thread, which needs
// the GIL back to return out of `listen`, so it has to let go of it too.
.def("listen", &odr::HttpServer::listen,
py::call_guard<py::gil_scoped_release>(),
"Serve what `bind` opened until `stop`. Returns right away if the "
"server has already been stopped.")
.def("is_running", &odr::HttpServer::is_running,
py::call_guard<py::gil_scoped_release>(),
"Whether a `listen` is in flight. False again once it has returned, "
"which is what `stop` waits for.")
.def("clear", &odr::HttpServer::clear)
.def("stop", &odr::HttpServer::stop,
py::call_guard<py::gil_scoped_release>(),
"Stop `listen` and release the socket. Blocks until `listen` has "
"returned, so nothing is serving any more once this returns.");
}