aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/clog.c116
1 files changed, 97 insertions, 19 deletions
diff --git a/src/clog.c b/src/clog.c
index 3265602..c4f44e3 100644
--- a/src/clog.c
+++ b/src/clog.c
@@ -33,31 +33,95 @@ static const char *database = "db";
int
init(int state)
{
- int err;
+ int err = 0;
+ const char *env = NULL;
+ struct kore_buf *pg_config_buf = kore_buf_alloc(0);
+
+ // Parse env vars and build PostgreSQL config string
+ env = getenv("POSTGRES_HOST");
+ if (env)
+ {
+ kore_buf_appendf(pg_config_buf, "host=%s ", env);
+ }
+ else
+ {
+ const char *host = "host=localhost ";
+ kore_buf_append(pg_config_buf, host, strlen(host));
+ }
+
+ env = getenv("POSTGRES_PORT");
+ if (env)
+ {
+ kore_buf_appendf(pg_config_buf, "port=%s ", env);
+ }
+ else
+ {
+ const char *port = "port=5432 ";
+ kore_buf_append(pg_config_buf, port, strlen(port));
+ }
+
+ env = getenv("POSTGRES_USER");
+ if (env)
+ {
+ kore_buf_appendf(pg_config_buf, "user=%s ", env);
+ }
+ else
+ {
+ const char *user = "user=clog ";
+ kore_buf_append(pg_config_buf, user, strlen(user));
+ }
+
+ env = getenv("POSTGRES_PASSWORD");
+ if (env)
+ {
+ kore_buf_appendf(pg_config_buf, "password=%s ", env);
+ }
+
+ env = getenv("POSTGRES_DBNAME");
+ if (env)
+ {
+ kore_buf_appendf(pg_config_buf, "user=%s ", env);
+ }
+ else
+ {
+ const char *dbname = "dbname=clog ";
+ kore_buf_append(pg_config_buf, dbname, strlen(dbname));
+ }
+
+ // FIXME: Should be configurable.
+ const char *sslmode = "sslmode=disable ";
+ kore_buf_append(pg_config_buf, sslmode, strlen(sslmode));
// Register the database
+ const char *pg_config = kore_buf_stringify(pg_config_buf, NULL);
+ kore_log(LOG_INFO, "%s", pg_config);
err = kore_pgsql_register(
database,
- "host=postgres port=5432 user=postgres password=postgres dbname=clog sslmode=disable"
+ pg_config
);
+ kore_buf_free(pg_config_buf);
+
return err;
}
int
post(struct http_request *req)
{
- int err;
+ int err = 0;
- const char *accept, *resource;
+ const char *accept = NULL;
+ const char *resource = NULL;
resource = req->path + strlen("/posts/");
kore_log(LOG_DEBUG, "Resource /posts/%s", resource);
err = http_request_header(req, "accept", &accept);
- if (err == KORE_RESULT_OK) {
+ if (err == KORE_RESULT_OK)
+ {
kore_log(LOG_DEBUG, "Accept: %s", accept);
- if (strcmp(accept, "application/json") == 0) {
+ if (strcmp(accept, "application/json") == 0)
+ {
return render_posts_json(req, resource);
}
}
@@ -68,14 +132,16 @@ post(struct http_request *req)
int
posts(struct http_request *req)
{
- int err;
+ int err = 0;
- const char *accept;
+ const char *accept = NULL;
err = http_request_header(req, "accept", &accept);
- if (err == KORE_RESULT_OK) {
+ if (err == KORE_RESULT_OK)
+ {
kore_log(LOG_DEBUG, "Accept: %s", accept);
- if (strcmp(accept, "application/json") == 0) {
+ if (strcmp(accept, "application/json") == 0)
+ {
return render_posts_json(req, NULL);
}
}
@@ -122,7 +188,8 @@ render_posts_html(struct http_request *req, const char *resource)
}
// Query for posts, check for error.
- if (resource) {
+ if (resource)
+ {
// Query a post.
err = kore_pgsql_query_params(
&sql,
@@ -133,7 +200,9 @@ render_posts_html(struct http_request *req, const char *resource)
1, // param count
KORE_PGSQL_PARAM_TEXT(resource)
);
- } else {
+ }
+ else
+ {
// Query all posts.
err = kore_pgsql_query(
&sql,
@@ -142,7 +211,9 @@ render_posts_html(struct http_request *req, const char *resource)
"ORDER BY updated_at DESC;"
);
}
- if (err == KORE_RESULT_ERROR) {
+
+ if (err == KORE_RESULT_ERROR)
+ {
kore_buf_appendf(
resp_buf,
(const char *) asset_error_html,
@@ -154,7 +225,8 @@ render_posts_html(struct http_request *req, const char *resource)
// Iterate over posts and render them.
rows = kore_pgsql_ntuples(&sql);
- for (row = 0; row < rows; row++) {
+ for (row = 0; row < rows; row++)
+ {
id = kore_pgsql_getvalue(&sql, row, 0);
title = kore_pgsql_getvalue(&sql, row, 1);
created_at = kore_pgsql_getvalue(&sql, row, 2);
@@ -165,7 +237,8 @@ render_posts_html(struct http_request *req, const char *resource)
struct kore_buf *html_buf = kore_buf_alloc(0);
// Render MD.
err = render_md(body, html_buf);
- if (err == KORE_RESULT_ERROR) {
+ if (err == KORE_RESULT_ERROR)
+ {
kore_log(LOG_ERR, "Error rendering markdown for entry %s.", id);
kore_buf_free(html_buf);
continue;
@@ -198,7 +271,8 @@ out: ;
}
int
-render_posts_json(struct http_request *req, const char *resource) {
+render_posts_json(struct http_request *req, const char *resource)
+{
// Errors
int err;
@@ -231,7 +305,8 @@ render_posts_json(struct http_request *req, const char *resource) {
goto out;
}
- if (resource) {
+ if (resource)
+ {
err = kore_pgsql_query_params(
&sql,
"SELECT JSON_AGG(ROW_TO_JSON(row)) FROM ("
@@ -243,7 +318,9 @@ render_posts_json(struct http_request *req, const char *resource) {
1, // param count
KORE_PGSQL_PARAM_TEXT(resource)
);
- } else {
+ }
+ else
+ {
err = kore_pgsql_query(
&sql,
"SELECT JSON_AGG(ROW_TO_JSON(row)) FROM ("
@@ -253,7 +330,8 @@ render_posts_json(struct http_request *req, const char *resource) {
") row;"
);
}
- if (err == KORE_RESULT_ERROR) {
+ if (err == KORE_RESULT_ERROR)
+ {
kore_buf_appendf(
resp_buf,
(const char *) asset_error_html,