-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
systemd: add socket activation example
- Loading branch information
Showing
11 changed files
with
296 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
minimal-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
23 changes: 23 additions & 0 deletions
23
...l-examples-lowlevel/http-server/minimal-http-server-systemd-socketact/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | ||
|
||
|
5 changes: 5 additions & 0 deletions
5
...r/minimal-http-server-systemd-socketact/lws-minimal-http-server-systemd-socketact.service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
6 changes: 6 additions & 0 deletions
6
...er/minimal-http-server-systemd-socketact/lws-minimal-http-server-systemd-socketact.socket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[Socket] | ||
ListenStream=7681 | ||
|
||
[Install] | ||
WantedBy=sockets.target | ||
|
74 changes: 74 additions & 0 deletions
74
...http-server/minimal-http-server-systemd-socketact/minimal-http-server-systemd-socketact.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
9 changes: 9 additions & 0 deletions
9
...examples-lowlevel/http-server/minimal-http-server-systemd-socketact/mount-origin/404.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 added
BIN
+1.37 KB
...mples-lowlevel/http-server/minimal-http-server-systemd-socketact/mount-origin/favicon.ico
Binary file not shown.
15 changes: 15 additions & 0 deletions
15
...amples-lowlevel/http-server/minimal-http-server-systemd-socketact/mount-origin/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
66 changes: 66 additions & 0 deletions
66
...r/minimal-http-server-systemd-socketact/mount-origin/libwebsockets.org-logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.