kore

An easy to use, scalable and secure web application framework for writing web APIs in C.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit 21d1e5156b6e6ef4cd64c2bce27e0e4bdb7abffb
parent 4e384167f056559ffb7b44b16737e7f5ca94feeb
Author: Joris Vink <joris@coders.se>
Date:   Wed,  9 Sep 2020 22:14:29 +0200

Add tools directory.

Includes the kore-serve utility that spins up a Kore instance
on the local directory and serves the contents via a filemap
on localhost port 8888.

Used by myself when hacking on the kore website.

Diffstat:
Makefile | 23+++++++++++++++++++++++
tools/kore-serve/.gitignore | 6++++++
tools/kore-serve/conf/build.conf | 11+++++++++++
tools/kore-serve/conf/kore-serve.conf | 2++
tools/kore-serve/src/kore-serve.c | 94+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 136 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -6,12 +6,15 @@ PREFIX?=/usr/local OBJDIR?=obj KORE=kore KODEV=kodev/kodev +KOREPATH?=$(shell pwd) KORE_CRYPTO?=crypto INSTALL_DIR=$(PREFIX)/bin MAN_DIR?=$(PREFIX)/share/man SHARE_DIR=$(PREFIX)/share/kore INCLUDE_DIR=$(PREFIX)/include/kore +TOOLS= kore-serve + GENERATED= PLATFORM=platform.h VERSION=$(OBJDIR)/version.c @@ -223,6 +226,26 @@ uninstall: rm -rf $(SHARE_DIR) $(MAKE) -C kodev uninstall +tools-build: $(KODEV) + for t in $(TOOLS); do \ + pushd tools/$$t; \ + env KODEV_OUTPUT=$(KOREPATH) $(KOREPATH)/$(KODEV) build; \ + popd; \ + done + +tools-clean: $(KODEV) + for t in $(TOOLS); do \ + pushd tools/$$t; \ + $(KOREPATH)/$(KODEV) clean; \ + popd; \ + done + +tools-install: + mkdir -p $(DESTDIR)$(INSTALL_DIR) + for t in $(TOOLS); do \ + install -m 555 $$t $(DESTDIR)$(INSTALL_DIR)/$$t; \ + done + $(OBJDIR)/%.o: src/%.c $(CC) $(CFLAGS) -c $< -o $@ diff --git a/tools/kore-serve/.gitignore b/tools/kore-serve/.gitignore @@ -0,0 +1,6 @@ +*.o +.flavor +.objs +kore-serve.so +assets.h +cert diff --git a/tools/kore-serve/conf/build.conf b/tools/kore-serve/conf/build.conf @@ -0,0 +1,11 @@ +single_binary=yes +kore_source=../../ +kore_flavor=NOTLS=1 + +cflags=-std=c99 -Werror +cflags=-Wall -Wmissing-declarations -Wshadow +cflags=-Wstrict-prototypes -Wmissing-prototypes +cflags=-Wpointer-arith -Wcast-qual -Wsign-compare + +dev { +} diff --git a/tools/kore-serve/conf/kore-serve.conf b/tools/kore-serve/conf/kore-serve.conf @@ -0,0 +1,2 @@ +# kore-serve configuration +# empty diff --git a/tools/kore-serve/src/kore-serve.c b/tools/kore-serve/src/kore-serve.c @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2020 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. + */ + +/* + * Simple static file serving over non TLS. Heavily used by myself + * when working on kore-site. + */ + +#include <sys/types.h> +#include <kore/kore.h> + +#include <stdlib.h> + +static void +usage(void) +{ + fprintf(stderr, + "Usage: kore-serve [-i ip] [-p port] [-r root]\n"); + + exit(1); +} + +void +kore_parent_configure(int argc, char *argv[]) +{ + int ch; + struct kore_domain *dom; + struct kore_server *srv; + char *rpath; + const char *ip, *port, *root; + + root = "."; + port = "8888"; + ip = "127.0.0.1"; + + foreground = 1; + kore_quiet = 1; + + skip_runas = 1; + skip_chroot = 1; + + kore_filemap_ext = kore_strdup(".html"); + + while ((ch = getopt(argc, argv, "hi:p:r:")) != -1) { + switch (ch) { + case 'i': + ip = optarg; + break; + case 'h': + usage(); + break; + case 'p': + port = optarg; + break; + case 'r': + root = optarg; + break; + default: + usage(); + } + } + + if ((rpath = realpath(root, NULL)) == NULL) + fatal("realpath(%s): %s", root, errno_s); + + kore_log(LOG_INFO, "%s -> http://%s:%s", rpath, ip, port); + + srv = kore_server_create("kore-serve"); + srv->tls = 0; + + if (!kore_server_bind(srv, ip, port, NULL)) + fatal("Failed to bind to %s:%s (%s)", ip, port, errno_s); + + kore_server_finalize(srv); + + dom = kore_domain_new("*"); + kore_domain_attach(dom, srv); + + if (!kore_filemap_create(dom, rpath, "/")) + fatal("failed to create filemap for %s", rpath); +}