diff --git a/minimal-examples/server/README.md b/minimal-examples/server/README.md index 119de336..628eb053 100644 --- a/minimal-examples/server/README.md +++ b/minimal-examples/server/README.md @@ -1,6 +1,7 @@ |Example|Demonstrates| ---|--- minimal-http-server|Serves a directory over http/1 or http/2, custom 404 handler +minimal-http-server-libuv|Same as minimal-http-server but libuv event loop minimal-http-server-smp|Multiple service threads minimal-ws-server|Serves an index.html over http that opens a ws shared chat client in a browser minimal-ws-server-pmd|Simple ws server with permessage-deflate support diff --git a/minimal-examples/server/minimal-http-server-libuv/CMakeLists.txt b/minimal-examples/server/minimal-http-server-libuv/CMakeLists.txt new file mode 100644 index 00000000..8168b245 --- /dev/null +++ b/minimal-examples/server/minimal-http-server-libuv/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 2.8) +include(CheckSymbolExists) + +set(SAMP lws-minimal-http-server-libuv) +set(SRCS minimal-http-server.c) + +if (UNIX) + set(CMAKE_C_FLAGS "-Wall -Wsign-compare -Wignored-qualifiers -Wtype-limits -Wuninitialized -Werror -Wundef ${CMAKE_C_FLAGS}" ) +endif() + +CHECK_SYMBOL_EXISTS(LWS_WITH_LIBUV libwebsockets.h HAS_LIBUV) +if (HAS_LIBUV) +else() + message(FATAL_ERROR "lws must have been built with LWS_WITH_LIBUV") +endif() + +add_executable(${SAMP} ${SRCS}) +target_link_libraries(${SAMP} -lwebsockets) + diff --git a/minimal-examples/server/minimal-http-server-libuv/README.md b/minimal-examples/server/minimal-http-server-libuv/README.md new file mode 100644 index 00000000..873e2502 --- /dev/null +++ b/minimal-examples/server/minimal-http-server-libuv/README.md @@ -0,0 +1,18 @@ +# lws minimal http server libuv + +## build + +``` + $ cmake . && make +``` + +## usage + +``` + $ ./lws-minimal-http-server-libuv +[2018/03/04 09:30:02:7986] USER: LWS minimal http server-libuv | visit http://localhost:7681 +[2018/03/04 09:30:02:7986] NOTICE: Creating Vhost 'default' port 7681, 1 protocols, IPv6 on +``` + +Visit http://localhost:7681 + diff --git a/minimal-examples/server/minimal-http-server-libuv/minimal-http-server.c b/minimal-examples/server/minimal-http-server-libuv/minimal-http-server.c new file mode 100644 index 00000000..bb666f99 --- /dev/null +++ b/minimal-examples/server/minimal-http-server-libuv/minimal-http-server.c @@ -0,0 +1,94 @@ +/* + * lws-minimal-http-server-libuv + * + * Copyright (C) 2018 Andy Green + * + * This file is made available under the Creative Commons CC0 1.0 + * Universal Public Domain Dedication. + * + * This demonstrates the most minimal http server you can make with lws. + * + * To keep it simple, it serves stuff from the subdirectory + * "./mount-origin" of the directory it was started in. + * You can change that by changing mount.origin below. + */ + +#include +#include +#include + +static struct lws_context *context; + +static const struct lws_http_mount mount = { + /* .mount_next */ NULL, /* linked-list "next" */ + /* .mountpoint */ "/", /* mountpoint URL */ + /* .origin */ "./mount-origin", /* serve from dir */ + /* .def */ "index.html", /* default filename */ + /* .protocol */ NULL, + /* .cgienv */ NULL, + /* .extra_mimetypes */ NULL, + /* .interpret */ NULL, + /* .cgi_timeout */ 0, + /* .cache_max_age */ 0, + /* .auth_mask */ 0, + /* .cache_reusable */ 0, + /* .cache_revalidate */ 0, + /* .cache_intermediaries */ 0, + /* .origin_protocol */ LWSMPRO_FILE, /* files in a dir */ + /* .mountpoint_len */ 1, /* char count */ + /* .basic_auth_login_file */ NULL, +}; + +void signal_cb(uv_signal_t *watcher, int signum) +{ + lwsl_notice("Signal %d caught, exiting...\n", watcher->signum); + + switch (watcher->signum) { + case SIGTERM: + case SIGINT: + break; + default: + signal(SIGABRT, SIG_DFL); + abort(); + break; + } + lws_libuv_stop(context); +} + +int main(int argc, char **argv) +{ + struct lws_context_creation_info info; + + memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */ + info.port = 7681; + info.mounts = &mount; + info.error_document_404 = "/404.html"; + info.options = LWS_SERVER_OPTION_LIBUV; + + lws_set_log_level(LLL_ERR | LLL_WARN | LLL_NOTICE | LLL_USER + /* | LLL_INFO */ /* | LLL_DEBUG */, NULL); + + lwsl_user("LWS minimal http server libuv | visit http://localhost:7681\n"); + + context = lws_create_context(&info); + if (!context) { + lwsl_err("lws init failed\n"); + return 1; + } + + lws_uv_sigint_cfg(context, 1, signal_cb); + + if (lws_uv_initloop(context, NULL, 0)) { + lwsl_err("lws_uv_initloop failed\n"); + + goto bail; + } + + lws_libuv_run(context, 0); + +bail: + lws_context_destroy(context); + lws_context_destroy2(context); + + return 0; +} diff --git a/minimal-examples/server/minimal-http-server-libuv/mount-origin/404.html b/minimal-examples/server/minimal-http-server-libuv/mount-origin/404.html new file mode 100644 index 00000000..1f7ae66e --- /dev/null +++ b/minimal-examples/server/minimal-http-server-libuv/mount-origin/404.html @@ -0,0 +1,9 @@ + + + +
+

404

+ Sorry, that file doesn't exist. + + + diff --git a/minimal-examples/server/minimal-http-server-libuv/mount-origin/favicon.ico b/minimal-examples/server/minimal-http-server-libuv/mount-origin/favicon.ico new file mode 100644 index 00000000..c0cc2e3d Binary files /dev/null and b/minimal-examples/server/minimal-http-server-libuv/mount-origin/favicon.ico differ diff --git a/minimal-examples/server/minimal-http-server-libuv/mount-origin/index.html b/minimal-examples/server/minimal-http-server-libuv/mount-origin/index.html new file mode 100644 index 00000000..d9e27c29 --- /dev/null +++ b/minimal-examples/server/minimal-http-server-libuv/mount-origin/index.html @@ -0,0 +1,12 @@ + + + +
+ + Hello from the minimal http server libuv example. +
+ You can confirm the 404 page handler by going to this + nonexistant page. + + + diff --git a/minimal-examples/server/minimal-http-server-libuv/mount-origin/libwebsockets.org-logo.png b/minimal-examples/server/minimal-http-server-libuv/mount-origin/libwebsockets.org-logo.png new file mode 100644 index 00000000..2060a10c Binary files /dev/null and b/minimal-examples/server/minimal-http-server-libuv/mount-origin/libwebsockets.org-logo.png differ