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 a35dfc6d06f3ce4ba22e2a3d256283ffda24075a
parent bcbed3b3492db43fc333e2f69e41f48fe8154225
Author: Joris Vink <joris@coders.se>
Date:   Tue, 10 Apr 2018 16:20:50 +0200

Teach kodev create about pyko applications.

Now you can create a pyko application skeleton using kodev:

	$ kodev create -p myapp

Not sure if this functionality will remain in kodev, but for now i'm undecided.

Diffstat:
src/cli.c | 133++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 126 insertions(+), 7 deletions(-)

diff --git a/src/cli.c b/src/cli.c @@ -200,9 +200,13 @@ static void cli_create(int, char **); static void cli_reload(int, char **); static void cli_flavor(int, char **); +static void cli_create_help(void); + static void file_create_src(void); static void file_create_config(void); static void file_create_gitignore(void); +static void file_create_pyko_src(void); +static void file_create_pyko_config(void); static struct cmd cmds[] = { { "help", "this help text", cli_help }, @@ -231,6 +235,18 @@ static const char *gen_dirs[] = { NULL }; +static const char *pyko_gen_dirs[] = { + "cert", + NULL +}; + +static struct filegen pyko_gen_files[] = { + { file_create_pyko_src }, + { file_create_pyko_config }, + { file_create_gitignore }, + { NULL } +}; + static const char *http_serveable_function = "int\n" "asset_serve_%s_%s(struct http_request *req)\n" @@ -304,6 +320,39 @@ static const char *build_data = "# included if you build with the \"prod\" flavor.\n" "#}\n"; +static const char *pyko_config_data = + "# %s configuration\n" + "\n" + "tls_dhparam\tdh2048.pem\n" + "\n" + "domain * {\n" + "\tcertfile\tcert/server.pem\n" + "\tcertkey\t\tcert/key.pem\n" + "\n" + "\tstatic\t/\tpage\n" + "}\n"; + +static const char *pyko_init_data = + "import os\n" + "import kore\n" + "from handlers import *\n" + "\n" + "ip = os.getenv('PYKO_IP')\n" + "if ip is None:\n" + "\tip = '127.0.0.1'\n" + "\n" + "port = os.getenv('PYKO_PORT')\n" + "if port is None:\n" + "\tport = '8888'\n" + "\n" + "kore.listen(ip, port, '')\n"; + +static const char *pyko_handlers_data = + "import kore\n" + "\n" + "def page(req):\n" + "\treq.response(200, b'')\n"; + static const char *dh2048_data = "-----BEGIN DH PARAMETERS-----\n" "MIIBCAKCAQEAn4f4Qn5SudFjEYPWTbUaOTLUH85YWmmPFW1+b5bRa9ygr+1wfamv\n" @@ -389,25 +438,68 @@ cli_help(int argc, char **argv) } static void +cli_create_help(void) +{ + printf("Usage: kodev create [-p] [name]\n"); + printf("Synopsis:\n"); + printf(" Create a new application skeleton directory structure.\n"); + printf("\n"); + printf(" Optional flags:\n"); + printf("\t-p = generate a application for use with pyko\n"); + + exit(1); +} + +static void cli_create(int argc, char **argv) { - int i; - char *fpath; + char *fpath; + const char **dirs; + struct filegen *files; + int i, ch, pyko; + + pyko = 0; + optind = 0; + + while ((ch = getopt(argc, argv, "hp")) != -1) { + switch (ch) { + case 'h': + cli_create_help(); + break; + case 'p': + pyko = 1; + break; + default: + cli_create_help(); + break; + } + } + + argc -= optind; + argv += optind; if (argc != 1) - fatal("missing application name"); + cli_create_help(); appl = argv[0]; cli_mkdir(appl, 0755); - for (i = 0; gen_dirs[i] != NULL; i++) { - (void)cli_vasprintf(&fpath, "%s/%s", appl, gen_dirs[i]); + if (pyko) { + dirs = pyko_gen_dirs; + files = pyko_gen_files; + } else { + dirs = gen_dirs; + files = gen_files; + } + + for (i = 0; dirs[i] != NULL; i++) { + (void)cli_vasprintf(&fpath, "%s/%s", appl, dirs[i]); cli_mkdir(fpath, 0755); free(fpath); } - for (i = 0; gen_files[i].cb != NULL; i++) - gen_files[i].cb(); + for (i = 0; files[i].cb != NULL; i++) + files[i].cb(); if (chdir(appl) == -1) fatal("chdir(%s): %s", appl, errno_s); @@ -701,6 +793,33 @@ cli_info(int argc, char **argv) } static void +file_create_pyko_src(void) +{ + char *name; + + (void)cli_vasprintf(&name, "%s/__init__.py", appl); + cli_file_create(name, pyko_init_data, strlen(pyko_init_data)); + free(name); + + (void)cli_vasprintf(&name, "%s/handlers.py", appl); + cli_file_create(name, pyko_handlers_data, strlen(pyko_handlers_data)); + free(name); +} + +static void +file_create_pyko_config(void) +{ + int l; + char *name, *data; + + (void)cli_vasprintf(&name, "%s/kore.conf", appl); + l = cli_vasprintf(&data, pyko_config_data, appl); + cli_file_create(name, data, l); + free(name); + free(data); +} + +static void file_create_src(void) { char *name;