mongoose/examples/http-server/main.c

72 lines
2.3 KiB
C
Raw Normal View History

2020-12-05 19:26:32 +08:00
// Copyright (c) 2020 Cesanta Software Limited
// All rights reserved
#include "mongoose.h"
static const char *s_debug_level = "2";
2021-01-03 16:43:56 +08:00
static const char *s_root_dir = ".";
2020-12-05 19:26:32 +08:00
static const char *s_listening_address = "http://localhost:8000";
static const char *s_enable_hexdump = "no";
static const char *s_ssi_pattern = "#.shtml";
2020-12-05 19:26:32 +08:00
static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
if (ev == MG_EV_HTTP_MSG) {
2021-01-03 16:55:00 +08:00
struct mg_http_serve_opts opts = {s_root_dir, s_ssi_pattern};
mg_http_serve_dir(c, ev_data, &opts);
2020-12-05 19:26:32 +08:00
}
(void) fn_data;
}
static void usage(const char *prog) {
fprintf(stderr,
"Mongoose v.%s, built " __DATE__ " " __TIME__
"\nUsage: %s OPTIONS\n"
" -H yes|no - enable traffic hexdump, default: '%s'\n"
" -S GLOB - glob pattern for SSI files, default: '%s'\n"
2020-12-05 19:26:32 +08:00
" -d DIR - directory to serve, default: '%s'\n"
2021-01-25 18:35:14 +08:00
" -l ADDR - listening address, default: '%s'\n"
" -v LEVEL - debug level, from 0 to 4, default: '%s'\n",
2021-01-03 16:55:00 +08:00
MG_VERSION, prog, s_debug_level, s_enable_hexdump, s_ssi_pattern,
s_root_dir, s_listening_address);
2020-12-05 19:26:32 +08:00
exit(EXIT_FAILURE);
}
2021-01-19 22:21:50 +08:00
int main(int argc, char *argv[]) {
struct mg_mgr mgr;
2020-12-05 19:26:32 +08:00
struct mg_connection *c;
int i;
2021-01-19 22:21:50 +08:00
// Parse command-line flags
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-d") == 0) {
s_root_dir = argv[++i];
} else if (strcmp(argv[i], "-H") == 0) {
s_enable_hexdump = argv[++i];
} else if (strcmp(argv[i], "-S") == 0) {
s_ssi_pattern = argv[++i];
} else if (strcmp(argv[i], "-l") == 0) {
s_listening_address = argv[++i];
2021-01-25 18:35:14 +08:00
} else if (strcmp(argv[i], "-v") == 0) {
s_debug_level = argv[++i];
2021-01-19 22:21:50 +08:00
} else {
usage(argv[0]);
}
}
2020-12-05 19:26:32 +08:00
// Initialise stuff
mg_log_set(s_debug_level);
2021-01-19 22:21:50 +08:00
mg_mgr_init(&mgr);
if ((c = mg_http_listen(&mgr, s_listening_address, cb, &mgr)) == NULL) {
2020-12-05 19:26:32 +08:00
LOG(LL_ERROR, ("Cannot listen on %s. Use http://ADDR:PORT or :PORT",
s_listening_address));
exit(EXIT_FAILURE);
}
if (mg_casecmp(s_enable_hexdump, "yes") == 0) c->is_hexdumping = 1;
2021-01-19 22:21:50 +08:00
// Start infinite event loop
LOG(LL_INFO, ("Starting Mongoose web server v%s", MG_VERSION));
for (;;) mg_mgr_poll(&mgr, 1000);
mg_mgr_free(&mgr);
2020-12-05 19:26:32 +08:00
return 0;
}