Skip to content

Commit

Permalink
systemd: add socket activation example
Browse files Browse the repository at this point in the history
  • Loading branch information
lws-team committed Jan 4, 2024
1 parent 3207da0 commit cc58c44
Show file tree
Hide file tree
Showing 11 changed files with 296 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CMakeCache.txt
CMakeFiles
build
cmake_install.cmake
lws-minimal*
build/lws-minimal*
Makefile
.cproject
.project
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
@@ -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`


Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[Unit]
Description=LWS minimal example for socket activation

[Service]
ExecStart=/usr/local/bin/lws-minimal-http-server-systemd-socketact
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[Socket]
ListenStream=7681

[Install]
WantedBy=sockets.target

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* lws-minimal-http-server-systemd-socketact
*
* Written in 2010-2024 by Andy Green <[email protected]>
*
* 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 <libwebsockets.h>
#include <string.h>
#include <signal.h>

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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<meta charset="UTF-8">
<html>
<body>
<img src="libwebsockets.org-logo.svg"><br>
<h1>404</h1>
Sorry, that file doesn't exist.
</body>
</html>

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<html>
<head>
<meta charset=utf-8 http-equiv="Content-Language" content="en"/>
</head>
<body>
<img src="libwebsockets.org-logo.svg">
<img src="strict-csp.svg"><br>

Hello from the <b>minimal http server example</b>.
<br>
You can confirm the 404 page handler by going to this
nonexistant <a href="notextant.html">page</a>.
</body>
</html>

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit cc58c44

Please sign in to comment.