mirror of
https://github.com/warmcat/libwebsockets.git
synced 2025-03-09 00:00:04 +01:00
minimal-http-server-libuv
This commit is contained in:
parent
db8cbb3b61
commit
081c9f7678
8 changed files with 153 additions and 0 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
18
minimal-examples/server/minimal-http-server-libuv/README.md
Normal file
18
minimal-examples/server/minimal-http-server-libuv/README.md
Normal file
|
@ -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
|
||||
|
|
@ -0,0 +1,94 @@
|
|||
/*
|
||||
* lws-minimal-http-server-libuv
|
||||
*
|
||||
* Copyright (C) 2018 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* 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 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;
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
<meta charset="UTF-8">
|
||||
<html>
|
||||
<body>
|
||||
<img src="libwebsockets.org-logo.png"><br>
|
||||
<h1>404</h1>
|
||||
Sorry, that file doesn't exist.
|
||||
</body>
|
||||
</html>
|
||||
|
Binary file not shown.
After Width: | Height: | Size: 1.4 KiB |
|
@ -0,0 +1,12 @@
|
|||
<meta charset="UTF-8">
|
||||
<html>
|
||||
<body>
|
||||
<img src="libwebsockets.org-logo.png"><br>
|
||||
|
||||
Hello from the <b>minimal http server libuv example</b>.
|
||||
<br>
|
||||
You can confirm the 404 page handler by going to this
|
||||
nonexistant <a href="notextant.html">page</a>.
|
||||
</body>
|
||||
</html>
|
||||
|
Binary file not shown.
After Width: | Height: | Size: 6.9 KiB |
Loading…
Add table
Reference in a new issue