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

kore-serve.c (2199B)



      1 /*
      2  * Copyright (c) 2020 Joris Vink <joris@coders.se>
      3  *
      4  * Permission to use, copy, modify, and distribute this software for any
      5  * purpose with or without fee is hereby granted, provided that the above
      6  * copyright notice and this permission notice appear in all copies.
      7  *
      8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
      9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     15  */
     16 
     17 /*
     18  * Simple static file serving over non TLS. Heavily used by myself
     19  * when working on kore-site.
     20  */
     21 
     22 #include <sys/types.h>
     23 #include <kore/kore.h>
     24 #include <kore/hooks.h>
     25 
     26 #include <stdlib.h>
     27 
     28 static void
     29 usage(void)
     30 {
     31 	fprintf(stderr,
     32 	    "Usage: kore-serve [-i ip] [-p port] [-r root]\n");
     33 
     34 	exit(1);
     35 }
     36 
     37 void
     38 kore_parent_configure(int argc, char *argv[])
     39 {
     40 	int			ch;
     41 	struct kore_domain	*dom;
     42 	struct kore_server	*srv;
     43 	char			*rpath;
     44 	const char		*ip, *port, *root;
     45 
     46 	root = ".";
     47 	port = "8888";
     48 	ip = "127.0.0.1";
     49 
     50 	kore_quiet = 1;
     51 	kore_foreground = 1;
     52 
     53 	skip_runas = 1;
     54 	skip_chroot = 1;
     55 
     56 	kore_filemap_ext = kore_strdup(".html");
     57 
     58 	while ((ch = getopt(argc, argv, "hi:p:r:")) != -1) {
     59 		switch (ch) {
     60 		case 'i':
     61 			ip = optarg;
     62 			break;
     63 		case 'h':
     64 			usage();
     65 			break;
     66 		case 'p':
     67 			port = optarg;
     68 			break;
     69 		case 'r':
     70 			root = optarg;
     71 			break;
     72 		default:
     73 			usage();
     74 		}
     75 	}
     76 
     77 	if ((rpath = realpath(root, NULL)) == NULL)
     78 		fatal("realpath(%s): %s", root, errno_s);
     79 
     80 	kore_log(LOG_INFO, "%s -> http://%s:%s", rpath, ip, port);
     81 
     82 	srv = kore_server_create("kore-serve");
     83 	srv->tls = 0;
     84 
     85 	if (!kore_server_bind(srv, ip, port, NULL))
     86 		fatal("Failed to bind to %s:%s (%s)", ip, port, errno_s);
     87 
     88 	kore_server_finalize(srv);
     89 
     90 	dom = kore_domain_new("*");
     91 	kore_domain_attach(dom, srv);
     92 
     93 	if (kore_filemap_create(dom, rpath, "/", NULL) == NULL)
     94 		fatal("failed to create filemap for %s", rpath);
     95 }