lpereira / lwan

Experimental, scalable, high performance HTTP server
https://lwan.ws
GNU General Public License v2.0
5.92k stars 549 forks source link

How to Pass data in lwan_set_url_map #39

Closed zenwong closed 10 years ago

zenwong commented 10 years ago

ConnectionPool_T pool = ConnectionPool_new(url); ConnectionPool_start(pool);

const lwan_url_map_t routes[] = { { .prefix = "/tags", .callback = tags, .data=pool } };

lwan_set_url_map(&l, routes);

how to pass data? When I try with lwan_set_url_map, it seems the prefix is ignored. If I remove the .data in routes the prefix works.

lpereira commented 10 years ago

This seems to be working properly here when using the included freegeoip server.

Could you please give more information about your setup, like CPU architecture, glibc version, and distribution? On Mar 3, 2014 3:47 AM, "zenwong" notifications@github.com wrote:

ConnectionPool_T pool = ConnectionPool_new(url); ConnectionPool_start(pool);

const lwan_url_map_t routes[] = { { .prefix = "/tags", .callback = tags, .data=pool } };

lwan_set_url_map(&l, routes);

how to pass data? When I try with lwan_set_url_map, it seems the prefix is ignored. If I remove the .data in routes the prefix works.

— Reply to this email directly or view it on GitHubhttps://github.com/lpereira/lwan/issues/39 .

lpereira commented 10 years ago

Also, if you could share the prototype of tags and how ConnectionPool_T is defined, it would help understand why this isn't working as it should.

zenwong commented 10 years ago
#include "lwan.h"
#include "lwan-serve-files.h"
#include <zdb2/all.h>

lwan_http_status_t vids(lwan_request_t *request, lwan_response_t *response, void *data) {
  ConnectionPool_T pool = data;
  Connection_T con = ConnectionPool_getConnection(pool);
  ResultSet_T result = Connection_executeQuery(con, "select * from vids");
  while (ResultSet_next(result)) {
    int id = ResultSet_getInt(result, 1);
    const char *title = ResultSet_getString(result, 2);
    strbuf_append_printf(response->buffer, "<h1>%d) %s</h1><br>\n", id, title);
  }
  return HTTP_OK;
}

lwan_http_status_t tags(lwan_request_t *request, lwan_response_t *response, void *data) {
  strbuf_append_printf(response->buffer, "<h1>%s</h1>\n", "tags");
  return HTTP_OK;
}

lwan_http_status_t acts(lwan_request_t *request, lwan_response_t *response, void *data) {
  strbuf_append_printf(response->buffer, "<h1>%s</h1>\n", "acts");
  return HTTP_OK;
}

int main(void) {
  URL_T url = URL_new("mysql:///jav?user=root&password=password&unix-socket=/var/run/mysqld/mysqld.sock");
  ConnectionPool_T pool = ConnectionPool_new(url);
  ConnectionPool_start(pool);

  lwan_t l = {
    .config = {
      .port = 80,
      .keep_alive_timeout = 15
    }
  };
  lwan_init(&l);

  const lwan_url_map_t routes[] = {
    { .prefix = "/vids", .callback = vids, .data = pool },
    { .prefix = "/tags", .callback = tags, .data = pool },
    { .prefix = "/acts", .callback = acts, .data = pool },
    { .prefix = "/", SERVE_FILES("./www") },
    { .prefix = NULL }
  };

  lwan_set_url_map(&l, routes);

  lwan_main_loop(&l);
  lwan_shutdown(&l);
  ConnectionPool_free(&pool);
  URL_free(&url);
  return 0;
}

I'm running arch linux 64bit, glibc 2.19-3. If I try to access localhost:8080/vids, I get this on the console.

3340 lwan-response.c:113 log_request() 127.0.0.1 "GET /vids HTTP/1.1" 200 (null).

I'm also not sure why the port is listening on 8080, since I specified port 80 in the lwan_t config.

lpereira commented 10 years ago

You need to set the content type or else lwan will assume an error occurred. Just set response->mime_type. See main.c for examples.

Try using chunked encoding while looping through your database result set, as this will scale better.

If lwan finds a lwan.conf file in the current directory it might use settings from it instead, so double check that.

I am curious as to what you're building. If that's public, please send me a link. :) On Mar 3, 2014 4:33 PM, "zenwong" notifications@github.com wrote:

include "lwan.h"

include "lwan-serve-files.h"

include <zdb2/all.h>

lwan_http_status_t vids(lwan_request_t request, lwan_response_t response, void data) { ConnectionPool_T pool = data; Connection_T con = ConnectionPool_getConnection(pool); ResultSet_T result = Connection_executeQuery(con, "select * from vids"); while (ResultSet_next(result)) { int id = ResultSet_getInt(result, 1); const char title = ResultSet_getString(result, 2); strbuf_append_printf(response->buffer, "

%d) %s


\n", id, title); } return HTTP_OK; }

lwan_http_status_t tags(lwan_request_t request, lwan_response_t response, void *data) { strbuf_append_printf(response->buffer, "

%s

\n", "tags"); return HTTP_OK; }

lwan_http_status_t acts(lwan_request_t request, lwan_response_t response, void *data) { strbuf_append_printf(response->buffer, "

%s

\n", "acts"); return HTTP_OK; }

int main(void) { URL_T url = URL_new("mysql:///jav?user=root&password=password&unix-socket=/var/run/mysqld/mysqld.sock"); ConnectionPool_T pool = ConnectionPool_new(url); ConnectionPool_start(pool);

lwan_t l = { .config = { .port = 80, .keep_alive_timeout = 15 } }; lwan_init(&l);

const lwan_url_map_t routes[] = { { .prefix = "/vids", .callback = vids, .data = pool }, { .prefix = "/tags", .callback = tags, .data = pool }, { .prefix = "/acts", .callback = acts, .data = pool }, { .prefix = "/", SERVE_FILES("./www") }, { .prefix = NULL } };

lwan_set_url_map(&l, routes);

lwan_main_loop(&l); lwan_shutdown(&l); ConnectionPool_free(&pool); URL_free(&url); return 0; }

I'm running arch linux 64bit, glibc 2.19-3. If I try to access localhost:8080/vids, I get this on the console.

3340 lwan-response.c:113 log_request() 127.0.0.1 "GET /vids HTTP/1.1" 200 (null).

I'm also not sure why the port is listening on 8080, since I specified port 80 in the lwan_t config.

— Reply to this email directly or view it on GitHubhttps://github.com/lpereira/lwan/issues/39#issuecomment-36549329 .

zenwong commented 10 years ago

It works after putting response->mime_type, thanks!!

I'm building a porn organizer, something like XBMC for porn / jav / hentai. I'm working on an API that my Qt Client can call to get tags and actress metadata. I'm currently trying out a few server backends to see how the performance and memory usage is, node.js, lwan, gwan, nginx with lua.

http://tagu.in

lpereira commented 10 years ago

Porn organizer? Although unexpected, I'm not really surprised such thing exists.

Since it is working as intended, I'm closing this issue. Feel free to open another one should you encounter any performance/memory usage issue.