1/*
2 * lws-minimal-http-server-fts
3 *
4 * Written in 2010-2019 by Andy Green <andy@warmcat.com>
5 *
6 * This file is made available under the Creative Commons CC0 1.0
7 * Universal Public Domain Dedication.
8 *
9 * This demonstrates how to use lws full-text search
10 */
11
12#include <libwebsockets.h>
13#include <string.h>
14#include <signal.h>
15
16#define LWS_PLUGIN_STATIC
17#include <protocol_fulltext_demo.c>
18
19const char *index_filepath = "./lws-fts.index";
20static int interrupted;
21
22static struct lws_protocols protocols[] = {
23	LWS_PLUGIN_PROTOCOL_FULLTEXT_DEMO,
24	LWS_PROTOCOL_LIST_TERM
25};
26
27static struct lws_protocol_vhost_options pvo_idx = {
28	NULL,
29	NULL,
30	"indexpath",		/* pvo name */
31	NULL	/* filled in at runtime */
32};
33
34static const struct lws_protocol_vhost_options pvo = {
35	NULL,		/* "next" pvo linked-list */
36	&pvo_idx,	/* "child" pvo linked-list */
37	"lws-test-fts",	/* protocol name we belong to on this vhost */
38	""		/* ignored */
39};
40
41/* override the default mount for /fts in the URL space */
42
43static const struct lws_http_mount mount_fts = {
44	/* .mount_next */		NULL,		/* linked-list "next" */
45	/* .mountpoint */		"/fts",		/* mountpoint URL */
46	/* .origin */			NULL,	/* protocol */
47	/* .def */			NULL,
48	/* .protocol */			"lws-test-fts",
49	/* .cgienv */			NULL,
50	/* .extra_mimetypes */		NULL,
51	/* .interpret */		NULL,
52	/* .cgi_timeout */		0,
53	/* .cache_max_age */		0,
54	/* .auth_mask */		0,
55	/* .cache_reusable */		0,
56	/* .cache_revalidate */		0,
57	/* .cache_intermediaries */	0,
58	/* .origin_protocol */		LWSMPRO_CALLBACK, /* dynamic */
59	/* .mountpoint_len */		4,		/* char count */
60	/* .basic_auth_login_file */	NULL,
61};
62
63static const struct lws_http_mount mount = {
64	/* .mount_next */		&mount_fts,	/* linked-list "next" */
65	/* .mountpoint */		"/",		/* mountpoint URL */
66	/* .origin */			"./mount-origin", /* serve from dir */
67	/* .def */			"index.html",	/* default filename */
68	/* .protocol */			NULL,
69	/* .cgienv */			NULL,
70	/* .extra_mimetypes */		NULL,
71	/* .interpret */		NULL,
72	/* .cgi_timeout */		0,
73	/* .cache_max_age */		0,
74	/* .auth_mask */		0,
75	/* .cache_reusable */		0,
76	/* .cache_revalidate */		0,
77	/* .cache_intermediaries */	0,
78	/* .origin_protocol */		LWSMPRO_FILE,	/* files in a dir */
79	/* .mountpoint_len */		1,		/* char count */
80	/* .basic_auth_login_file */	NULL,
81};
82
83void sigint_handler(int sig)
84{
85	interrupted = 1;
86}
87
88int main(int argc, const char **argv)
89{
90	int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE;
91	struct lws_context_creation_info info;
92	struct lws_context *context;
93	const char *p;
94
95	signal(SIGINT, sigint_handler);
96
97	if ((p = lws_cmdline_option(argc, argv, "-d")))
98		logs = atoi(p);
99
100	lws_set_log_level(logs, NULL);
101	lwsl_user("LWS minimal http server fulltext search | "
102		  "visit http://localhost:7681\n");
103
104	memset(&info, 0, sizeof info);
105	info.port = 7681;
106	info.mounts = &mount;
107	info.protocols = protocols;
108	info.pvo = &pvo;
109	info.options =
110		LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
111
112	pvo_idx.value = index_filepath;
113
114	context = lws_create_context(&info);
115	if (!context) {
116		lwsl_err("lws init failed\n");
117		return 1;
118	}
119
120	while (n >= 0 && !interrupted)
121		n = lws_service(context, 0);
122
123	lws_context_destroy(context);
124
125	return 0;
126}
127