diff --git a/.gitignore b/.gitignore index 47f7a9078..e0417760d 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,7 @@ CMakeCache.txt CMakeFiles build cmake_install.cmake -lws-minimal* +build/lws-minimal* Makefile .cproject .project diff --git a/minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/CMakeLists.txt b/minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/CMakeLists.txt new file mode 100644 index 000000000..c4f731557 --- /dev/null +++ b/minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/CMakeLists.txt @@ -0,0 +1,44 @@ +project(lws-minimal-http-server-systemd-socketact C) +cmake_minimum_required(VERSION 3.5) +find_package(libwebsockets CONFIG REQUIRED) +list(APPEND CMAKE_MODULE_PATH ${LWS_CMAKE_DIR}) +include(CheckCSourceCompiles) +include(LwsCheckRequirements) + +set(SRCS minimal-http-server-systemd-socketact.c) + +add_compile_options(-Wall -Wextra -Werror -pedantic -g -Ofast + -DINSTALL_SHARE=\"${CMAKE_INSTALL_PREFIX}/share/${PROJECT_NAME}\") + +set(requirements 1) +require_lws_config(LWS_ROLE_H1 1 requirements) +require_lws_config(LWS_WITH_SERVER 1 requirements) +require_lws_config(LWS_HAVE_SYSTEMD_H 1 requirements) + +if (requirements) + add_executable(${PROJECT_NAME} ${SRCS}) + + if (websockets_shared) + target_link_libraries(${PROJECT_NAME} websockets_shared + ${LIBWEBSOCKETS_DEP_LIBS}) + add_dependencies(${PROJECT_NAME} websockets_shared) + else() + target_link_libraries(${PROJECT_NAME} websockets + ${LIBWEBSOCKETS_DEP_LIBS}) + endif() + + install(TARGETS ${PROJECT_NAME} + DESTINATION bin) + + install(FILES mount-origin/404.html + mount-origin/favicon.ico + mount-origin/index.html + mount-origin/libwebsockets.org-logo.svg + mount-origin/strict-csp.svg + DESTINATION share/${PROJECT_NAME}) + + install(FILES lws-minimal-http-server-systemd-socketact.service + lws-minimal-http-server-systemd-socketact.socket + DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/systemd/system) + +endif() \ No newline at end of file diff --git a/minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/README.md b/minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/README.md new file mode 100644 index 000000000..62d0c1ce9 --- /dev/null +++ b/minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/README.md @@ -0,0 +1,23 @@ +# lws minimal http server systemd socketact + +## build + +``` + $ cmake . && make && sudo make install +``` + +This will by default install to `/usr/local` and the systemd pieces to `/usr/local/lib/systemd/system` + +Assets will go to `/usr/local/share/lws-minimal-http-server-systemd-socketact/` and +the test app will know to fetch things from there. + +## configure + +``` + $ systemctl --user link /usr/local/lib/systemd/system/lws-minimal-http-server-systemd-socketact.service /usr/local/lib/systemd/system/lws-minimal-http-server-systemd-socketact.socket + $ systemctl --user start lws-minimal-http-server-systemd-socketact.socket +``` + +Then the test server should be autoexecuted by systemd if you try to browse to `http://127.0.0.1:7681` + + diff --git a/minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/lws-minimal-http-server-systemd-socketact.service b/minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/lws-minimal-http-server-systemd-socketact.service new file mode 100644 index 000000000..983478c1e --- /dev/null +++ b/minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/lws-minimal-http-server-systemd-socketact.service @@ -0,0 +1,5 @@ +[Unit] +Description=LWS minimal example for socket activation + +[Service] +ExecStart=/usr/local/bin/lws-minimal-http-server-systemd-socketact diff --git a/minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/lws-minimal-http-server-systemd-socketact.socket b/minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/lws-minimal-http-server-systemd-socketact.socket new file mode 100644 index 000000000..f15d2ad85 --- /dev/null +++ b/minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/lws-minimal-http-server-systemd-socketact.socket @@ -0,0 +1,6 @@ +[Socket] +ListenStream=7681 + +[Install] +WantedBy=sockets.target + diff --git a/minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/minimal-http-server-systemd-socketact.c b/minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/minimal-http-server-systemd-socketact.c new file mode 100644 index 000000000..4db17a3bf --- /dev/null +++ b/minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/minimal-http-server-systemd-socketact.c @@ -0,0 +1,74 @@ +/* + * lws-minimal-http-server-systemd-socketact + * + * Written in 2010-2024 by 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 int interrupted; + +static const struct lws_http_mount mount = { + .mountpoint = "/", /* mountpoint URL */ + .origin = INSTALL_SHARE, /* serve from dir */ + .def = "index.html", /* default filename */ + .origin_protocol = LWSMPRO_FILE, /* files in a dir */ + .mountpoint_len = 1, /* char count */ +}; + +void sigint_handler(int sig) +{ + interrupted = 1; +} + +int main(int argc, const char **argv) +{ + struct lws_context_creation_info info; + struct lws_context *cx; + int n = 0; + + lws_context_info_defaults(&info, NULL); + info.default_loglevel = LLL_USER | LLL_ERR | LLL_WARN; + info.fd_limit_per_thread = 128; + if (lws_systemd_inherited_fd(0, &info)) { + lwsl_err("This example needs to run from systemd " + "socket activation (see README.md)\n"); + return 1; + } + lws_cmdline_option_handle_builtin(argc, argv, &info); + + signal(SIGINT, sigint_handler); + + lwsl_user("LWS minimal http server via socket activation | " + "visit http://localhost:%u\n", info.port); + + info.mounts = &mount; + info.error_document_404 = "/404.html"; + info.options = + LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE; + + cx = lws_create_context(&info); + if (!cx) { + lwsl_err("lws init failed\n"); + return 1; + } + + n = 0; + while (n >= 0 && !interrupted) + n = lws_service(cx, 0); + + lws_context_destroy(cx); + + return 0; +} diff --git a/minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/mount-origin/404.html b/minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/mount-origin/404.html new file mode 100644 index 000000000..3e5a14b9f --- /dev/null +++ b/minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/mount-origin/404.html @@ -0,0 +1,9 @@ + + + +
+

404

+ Sorry, that file doesn't exist. + + + diff --git a/minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/mount-origin/favicon.ico b/minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/mount-origin/favicon.ico new file mode 100644 index 000000000..c0cc2e3df Binary files /dev/null and b/minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/mount-origin/favicon.ico differ diff --git a/minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/mount-origin/index.html b/minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/mount-origin/index.html new file mode 100644 index 000000000..bc9ffa442 --- /dev/null +++ b/minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/mount-origin/index.html @@ -0,0 +1,15 @@ + + + + + + +
+ + Hello from the minimal http server example. +
+ You can confirm the 404 page handler by going to this + nonexistant page. + + + diff --git a/minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/mount-origin/libwebsockets.org-logo.svg b/minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/mount-origin/libwebsockets.org-logo.svg new file mode 100644 index 000000000..ef241b37c --- /dev/null +++ b/minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/mount-origin/libwebsockets.org-logo.svg @@ -0,0 +1,66 @@ + + + + + +image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/mount-origin/strict-csp.svg b/minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/mount-origin/strict-csp.svg new file mode 100644 index 000000000..cd128f1d2 --- /dev/null +++ b/minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/mount-origin/strict-csp.svg @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +