From 3595c5264297f14010eb8dc6f0a7fd31dc186e8b Mon Sep 17 00:00:00 2001 From: Michael McVady Date: Sat, 21 Aug 2021 23:17:41 -0500 Subject: Attemp to use postgres --- conf/clog.conf | 2 +- src/clog.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/conf/clog.conf b/conf/clog.conf index 08bd30d..bc54fa9 100644 --- a/conf/clog.conf +++ b/conf/clog.conf @@ -4,7 +4,7 @@ server tls { bind 127.0.0.1 8888 } -load ./clog.so +load ./clog.so init tls_dhparam dh2048.pem diff --git a/src/clog.c b/src/clog.c index a41a308..9bce41c 100644 --- a/src/clog.c +++ b/src/clog.c @@ -1,22 +1,89 @@ #include #include +#include #include "assets.h" +int init(int); int page(struct http_request *); +/* Called when our module is loaded (see config) */ +int +init(int state) +{ + /* Register our database. */ + int err = kore_pgsql_register( + "db", + "host=127.0.0.1 port=5432 user=postgres password=p0stgres dbname=clog sslmode=disable" + ); + // kore_pgsql_register("db", "host=/tmp dbname=clog"); + + return (err); +} + int page(struct http_request *req) { + struct kore_buf *buf; buf = kore_buf_alloc(2048); kore_buf_append(buf, asset_index_begin_html, asset_len_index_begin_html); + + /* sql stuff */ + struct kore_pgsql sql; + // char *name; + // int rows, i; + + /* req->status = HTTP_STATUS_INTERNAL_ERROR; */ + + kore_pgsql_init(&sql); + + /* + * Initialise our kore_pgsql data structure with the database name + * we want to connect to (note that we registered this earlier with + * kore_pgsql_register()). We also say we will perform a synchronous + * query (KORE_PGSQL_SYNC). + */ + int err = kore_pgsql_setup(&sql, "db", KORE_PGSQL_SYNC); + kore_log(1, "kore_pgsql_setup: %d", err); + kore_log(1, "sql->state: %d", sql.state); + if (!err) { + kore_log(1, "here1!"); + kore_pgsql_logerror(&sql); + goto out; + } + + kore_log(1, "here!"); + +// /* +// * Now we can fire off the query, once it returns we either have +// * a result on which we can operate or an error occurred. +// */ +// if (!kore_pgsql_query(&sql, "SELECT * FROM coders")) { +// kore_pgsql_logerror(&sql); +// goto out; +// } +// +// /* +// * Iterate over the result and dump it to somewhere. +// */ +// rows = kore_pgsql_ntuples(&sql); +// for (i = 0; i < rows; i++) { +// name = kore_pgsql_getvalue(&sql, i, 0); +// kore_log(LOG_NOTICE, "name: '%s'", name); +// } + +out: ; + kore_buf_append(buf, asset_index_end_html, asset_len_index_end_html); http_response_header(req, "content-type", "text/html; charset=utf-8"); - http_response(req, 200, buf->data, buf->offset); + http_response(req, HTTP_STATUS_OK, buf->data, buf->offset); // http_response_stream ??? kore_buf_free(buf); + /* Don't forget to cleanup the kore_pgsql data structure. */ + kore_pgsql_cleanup(&sql); + return (KORE_RESULT_OK); } -- cgit v1.2.3