1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2011 Stewart Smith
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
#include <drizzled/module/module.h>
24
#include <drizzled/module/context.h>
25
#include <drizzled/plugin/plugin.h>
26
#include <drizzled/plugin.h>
27
#include <drizzled/sys_var.h>
28
#include <drizzled/gettext.h>
29
#include <drizzled/error.h>
30
#include <drizzled/query_id.h>
31
#include <drizzled/session.h>
32
#include <drizzled/internal/my_sys.h>
33
#include <drizzled/internal/m_string.h>
36
#include <boost/program_options.hpp>
37
#include <drizzled/module/option_map.h>
38
#include <drizzled/constrained_value.h>
42
namespace po= boost::program_options;
43
using namespace drizzled;
46
namespace drizzle_plugin
51
static port_constraint port;
52
struct event_base *base= NULL;
55
static in_port_t getPort(void)
60
extern "C" void process_request(struct evhttp_request *req, void* );
61
extern "C" void process_root_request(struct evhttp_request *req, void* );
63
extern "C" void process_request(struct evhttp_request *req, void* )
65
struct evbuffer *buf = evbuffer_new();
66
if (buf == NULL) return;
67
evbuffer_add_printf(buf, "Requested: %s\n", evhttp_request_uri(req));
68
evhttp_send_reply(req, HTTP_OK, "OK", buf);
71
extern "C" void process_root_request(struct evhttp_request *req, void* )
73
struct evbuffer *buf = evbuffer_new();
74
if (buf == NULL) return;
75
evbuffer_add_printf(buf, "Handling root request for URI %s\n", evhttp_request_uri(req));
76
evhttp_send_reply(req, HTTP_OK, "OK", buf);
79
extern "C" void *libevent_thread(void*);
81
extern "C" void *libevent_thread(void*)
83
event_base_dispatch(base);
88
static int json_server_init(drizzled::module::Context &context)
90
context.registerVariable(new sys_var_constrained_value_readonly<in_port_t>("port", port));
93
httpd= evhttp_new(base);
97
int r= evhttp_bind_socket(httpd, "0.0.0.0", getPort());
102
evhttp_set_cb(httpd, "/", process_root_request, NULL);
103
evhttp_set_gencb(httpd, process_request, NULL);
105
pthread_t libevent_loop_thread;
107
pthread_create(&libevent_loop_thread, NULL, libevent_thread, NULL);
112
static void init_options(drizzled::module::option_context &context)
115
po::value<port_constraint>(&port)->default_value(80),
116
_("Port number to use for connection or 0 for default (port 80) "));
119
} /* namespace json_server */
120
} /* namespace drizzle_plugin */
122
DRIZZLE_DECLARE_PLUGIN
128
"JSON HTTP interface",
130
drizzle_plugin::json_server::json_server_init, /* Plugin Init */
132
drizzle_plugin::json_server::init_options /* config options */
134
DRIZZLE_DECLARE_PLUGIN_END;