Skip to content

Instantly share code, notes, and snippets.

@joncham
Last active December 20, 2015 16:19
Show Gist options
  • Save joncham/6161020 to your computer and use it in GitHub Desktop.
Save joncham/6161020 to your computer and use it in GitHub Desktop.
#include "../../lib/haywire/lib/libuv/include/uv.h"
#include "haywire.h"
#include "dazzle.h"
#include "http_server.h"
namespace dazzle {
HttpServer::HttpServer(Database* database)
{
this->database = database;
}
HttpServer::~HttpServer()
{
}
void response_complete(void* user_data)
{
}
void HttpServer::handle_get(http_request* request, hw_http_response* response)
{
hw_string status_code;
hw_string content_type_name;
hw_string content_type_value;
hw_string body;
hw_string keep_alive_name;
hw_string keep_alive_value;
SETSTRING(status_code, HTTP_STATUS_200);
hw_set_response_status_code(response, &status_code);
SETSTRING(content_type_name, "Content-Type");
SETSTRING(content_type_value, "text/html");
hw_set_response_header(response, &content_type_name, &content_type_value);
dazzle_transaction* tx;
this->database->begin_transaction(&tx);
daz_keypair get_pair;
DAZ_SETSTRING(get_pair.key, "hello");
tx->get(&get_pair);
//SETSTRING(body, "hello world");
//hw_set_body(response, &body);
if (request->keep_alive)
{
SETSTRING(keep_alive_name, "Connection");
SETSTRING(keep_alive_value, "Keep-Alive");
hw_set_response_header(response, &keep_alive_name, &keep_alive_value);
}
else
{
hw_set_http_version(response, 1, 0);
}
// Reference to non-static member function must be called
hw_http_response_send(response, NULL, response_complete);
}
void HttpServer::handle_put(http_request* request, hw_http_response* response)
{
}
void HttpServer::handle_request(http_request* request, hw_http_response* response)
{
switch (request->method)
{
case HW_HTTP_GET:
handle_get(request, response);
break;
case HW_HTTP_PUT:
handle_put(request, response);
break;
}
}
void HttpServer::open_http(void* arg)
{
// now I have my instance, my 'this'
HttpServer* server = (HttpServer*)arg;
configuration config;
config.http_listen_address = "0.0.0.0";
config.http_listen_port = 8000;
hw_init_with_config(&config);
// Reference to non-static member function must be called
// is this your function in haywire, if so make it take a void*
// as the last arg and pass it into the callback function.=
hw_http_add_route("/", handle_request);
hw_http_open();
}
int HttpServer::open()
{
uv_thread_t http_thread;
// Reference to non-static member function must be called
uv_thread_create(&http_thread, open_http, this);
return DAZZLE_SUCCESS;
}
}
#pragma once
#include "database.h"
namespace dazzle {
class HttpServer
{
public:
HttpServer(Database* database);
~HttpServer();
int open();
private:
Database* database;
static void open_http(void* arg);
static void handle_request(http_request* request, hw_http_response* response);
void handle_get(http_request* request, hw_http_response* response);
void handle_put(http_request* request, hw_http_response* response);
void response_complete(void* user_data);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment