kore

Kore is a web application platform for writing scalable, concurrent web based processes in C or Python.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit 211f82847eee1aeb112dc4bb3e7c5646ae967fc3
parent 22882261f7ca98640332b928ac256430a9652d11
Author: Joris Vink <joris@coders.se>
Date:   Mon, 31 Mar 2014 11:30:21 +0200

Add example pgsql module showing how one can use the KORE_PGSQL stuff

Diffstat:
contrib/modules/pgsql_test/build.sh | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
contrib/modules/pgsql_test/module.conf | 20++++++++++++++++++++
contrib/modules/pgsql_test/src/pgsql_test.c | 46++++++++++++++++++++++++++++++++++++++++++++++
contrib/modules/pgsql_test/tools/inject.c | 119+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 256 insertions(+), 0 deletions(-)

diff --git a/contrib/modules/pgsql_test/build.sh b/contrib/modules/pgsql_test/build.sh @@ -0,0 +1,71 @@ +#!/bin/sh +# +# Kore pgsql test module build directives. +# + +MODULE=pgsql_test.module +MEDIA_DIR=media +SOURCE_DIR=src +KORE_DIR=../../.. +PGDIR=$(pg_config --includedir) +CC=gcc +CFLAGS="-I. -I${KORE_DIR}/includes -I${PGDIR} \ + -Wall -Wstrict-prototypes -Wmissing-prototypes \ + -Wmissing-declarations -Wshadow -Wpointer-arith -Wcast-qual \ + -Wsign-compare -g" + +OSNAME=$(uname -s | sed -e 's/[-_].*//g' | tr A-Z a-z) +if [ "${OSNAME}" = "darwin" ]; then + LDFLAGS="-dynamiclib -undefined suppress -flat_namespace" +else + LDFLAGS="-shared" +fi + +MODULE_BUILD_DATE=$(date +"%Y-%m-%d %H:%M:%S") + +### Begin building #### +echo "Building module ${MODULE}..." +rm -f ${MODULE} + +${CC} ${CFLAGS} tools/inject.c -o tools/inject + +if [ ! -d ${SOURCE_DIR}/${MEDIA_DIR} ]; then + mkdir ${SOURCE_DIR}/${MEDIA_DIR}; +fi +rm -f ${SOURCE_DIR}/${MEDIA_DIR}/* + +if [ ! -d .objs ]; then + mkdir .objs; +fi +rm -f .objs/* + +rm -f static.h + +for file in `find ${MEDIA_DIR} -type f \( ! -name \*.swp \)`; do + echo "Injecting $file"; + base=`basename $file`; + ./tools/inject $file $base > ${SOURCE_DIR}/${MEDIA_DIR}/${base}.c; + if [ $? -ne 0 ]; then + echo "Injection error, check above messages for clues."; + exit 1; + fi +done + +echo "#define MODULE_BUILD_DATE \"${MODULE_BUILD_DATE}\"" >> static.h + +for src in `find ${SOURCE_DIR} -type f -name \*.c`; do + base=`basename $src`; + ${CC} ${CFLAGS} -fPIC -c $src -o .objs/${base}.o + if [ $? -ne 0 ]; then + echo "Build error, check above messages for clues."; + exit 1; + fi +done + +${CC} ${LDFLAGS} `find .objs -name \*.o -type f` -o ${MODULE} +echo "Building completed!" + +rm -rf ${SOURCE_DIR}/${MEDIA_DIR} +rm -rf .objs +rm -f tools/inject +rm -f static.h diff --git a/contrib/modules/pgsql_test/module.conf b/contrib/modules/pgsql_test/module.conf @@ -0,0 +1,20 @@ +# Kore pgsql_test configuration +# see modules/examples/module.conf for a better overview +# of what a configuration file has to offer and what different +# settings mean and do. + +bind 127.0.0.1 8080 +chroot /home/joris/src/kore +runas joris +workers 4 +pidfile kore.pid +load modules/pgsql_test/pgsql_test.module +ssl_no_compression + +domain localhost { + certfile cert/server.crt + certkey cert/server.key + accesslog kore_pgsql.log + + static / serve_pgsql_test +} diff --git a/contrib/modules/pgsql_test/src/pgsql_test.c b/contrib/modules/pgsql_test/src/pgsql_test.c @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2014 Joris Vink <joris@coders.se> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include "kore.h" +#include "http.h" +#include "contrib/postgres/kore_pgsql.h" + +#include "static.h" + +int serve_pgsql_test(struct http_request *); + +int +serve_pgsql_test(struct http_request *req) +{ + //int r; + + KORE_PGSQL(req, "SELECT * FROM test", 0, { + if (req->pgsql[0]->state == KORE_PGSQL_STATE_ERROR) { + kore_log(LOG_NOTICE, "pgsql: %s", + (req->pgsql[0]->error) ? + req->pgsql[0]->error : "unknown"); + http_response(req, 500, "fail", 4); + return (KORE_RESULT_OK); + } else { + //r = kore_pgsql_ntuples(req, 0); + } + }); + + /* Query successfully completed */ + http_response(req, 200, "ok", 2); + + return (KORE_RESULT_OK); +} diff --git a/contrib/modules/pgsql_test/tools/inject.c b/contrib/modules/pgsql_test/tools/inject.c @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2013 Joris Vink <joris@coders.se> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <sys/param.h> +#include <sys/stat.h> + +#include <ctype.h> +#include <err.h> +#include <errno.h> +#include <inttypes.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#if defined(OpenBSD) +#define PRI_TIME_T "d" +#endif + +#if defined(linux) +#if defined(__x86_64__) +#define PRI_TIME_T PRIu64 +#else +#define PRI_TIME_T "ld" +#endif +#endif + +#if defined(__MACH__) +#define PRI_TIME_T "ld" +#endif + +int +main(int argc, char *argv[]) +{ + struct stat st; + ssize_t len; + FILE *hdr; + char *ext; + unsigned char c[1]; + int fd, newline, count; + + if (argc != 3) + exit(1); + + if ((fd = open(argv[1], O_RDONLY)) == -1) + err(1, "open() %d", errno); + if ((hdr = fopen("static.h", "a+")) == NULL) + err(1, "fopen() %d", errno); + if ((ext = strchr(argv[2], '.')) != NULL) + *(ext)++ = '\0'; + else + ext = ""; + + if (stat(argv[1], &st) == -1) { + printf("stat(%s) failed: %d\n", argv[1], errno); + exit(1); + } + + printf("/**** AUTO GENERATED BY MAKEFILE - DO NOT TOUCH ****/\n"); + printf("#include <sys/param.h>\n\n"); + printf("u_int8_t static_%s_%s[] = {", ext, argv[2]); + + len = 0; + count = 0; + newline = 1; + for (;;) { + if (newline) { + printf("\n\t"); + count = 0; + newline = 0; + } + + len = read(fd, c, 1); + if (len == 0) + break; + + if (len == -1) { + printf("read(): %d\n", errno); + exit(1); + } + + if (len != 1) + exit(1); + + printf("0x%02x, ", c[0]); + if (count++ == 10) + newline = 1; + } + + close(fd); + + printf("};\n\n"); + printf("u_int32_t static_len_%s_%s = %" PRIu32 ";\n", + ext, argv[2], (u_int32_t)st.st_size); + + printf("time_t static_mtime_%s_%s = %" PRI_TIME_T ";\n", + ext, argv[2], st.st_mtime); + + fprintf(hdr, "extern u_int8_t static_%s_%s[];\n", ext, argv[2]); + fprintf(hdr, "extern u_int32_t static_len_%s_%s;\n", ext, argv[2]); + fprintf(hdr, "extern time_t static_mtime_%s_%s;\n", ext, argv[2]); + fclose(hdr); + + return (0); +}