Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

88 lines (74 sloc) 2.479 kb
#include "ten/app.hh"
#include "ten/task.hh"
#include "ten/buffer.hh"
#include "ten/logging.hh"
#include "ten/net.hh"
#include "ten/channel.hh"
#include "ten/http/server.hh"
#include <boost/lexical_cast.hpp>
using namespace ten;
const size_t default_stacksize=256*1024;
struct my_config : app_config {
std::string http_address;
unsigned int http_threads;
};
static my_config conf;
struct state : boost::noncopyable {
application &app;
http_server http;
state(application &app_) : app(app_) {}
};
static void log_request(http_server::request &h) {
using namespace std::chrono;
auto stop = steady_clock::now();
VLOG(1) << h.agent_ip() << " " <<
h.req.method << " " <<
h.req.uri << " " <<
h.resp.status_code << " " <<
h.resp.get("Content-Length") << " " <<
duration_cast<milliseconds>(stop-h.start).count();
}
static void http_quit(std::shared_ptr<state> st, http_server::request &h) {
LOG(INFO) << "quit requested over http";
st->app.quit();
h.resp = http_response{200,
Headers{
"Connection", "close",
"Content-Length", 0
}
};
h.send_response();
}
static void http_root(std::shared_ptr<state> st, http_server::request &h) {
h.resp = http_response{200};
h.resp.set_body("Hello World!\n");
h.send_response();
}
static void start_http_server(std::shared_ptr<state> &st) {
using namespace std::placeholders;
std::string addr = conf.http_address;
uint16_t port = 0;
parse_host_port(addr, port);
if (port == 0) return;
st->http.set_log_callback(log_request);
st->http.add_route("/quit", std::bind(http_quit, st, _1));
st->http.add_route("/*", std::bind(http_root, st, _1));
st->http.serve(addr, port, conf.http_threads);
}
static void startup(application &app) {
taskname("startup");
std::shared_ptr<state> st{std::make_shared<state>(app)};
taskspawn(std::bind(start_http_server, st));
}
int main(int argc, char *argv[]) {
application app{"0.0.1", conf};
namespace po = boost::program_options;
app.opts.configuration.add_options()
("http,H", po::value<std::string>(&conf.http_address)->default_value("0.0.0.0:3080"),
"http listen address:port")
("http-threads", po::value<unsigned int>(&conf.http_threads)->default_value(1), "http threads")
;
app.parse_args(argc, argv);
taskspawn(std::bind(startup, std::ref(app)));
return app.run();
}
Jump to Line
Something went wrong with that request. Please try again.