Skip to content

Commit

Permalink
Add fossa/examples/simplest_web_server
Browse files Browse the repository at this point in the history
    PUBLISHED_FROM=e7abf85ffa002279f9bf33cc80de9c1d163f4006
  • Loading branch information
cpq committed Jul 14, 2015
1 parent a36745c commit 93ba533
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
14 changes: 14 additions & 0 deletions examples/simplest_web_server/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
PROG = simplest_web_server
SOURCES = $(PROG).c ../../fossa.c
CFLAGS = -W -Wall -I../.. $(CFLAGS_EXTRA)

all: $(PROG)

$(PROG): $(SOURCES)
$(CC) $(SOURCES) -o $@ $(CFLAGS)

$(PROG).exe: $(SOURCES)
cl $(SOURCES) /I../.. /MD /Fe$@

clean:
rm -rf *.gc* *.dSYM *.exe *.obj *.o a.out $(PROG)
34 changes: 34 additions & 0 deletions examples/simplest_web_server/simplest_web_server.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2015 Cesanta Software Limited
// All rights reserved

#include "fossa.h"

static const char *s_http_port = "8000";
static struct ns_serve_http_opts s_http_server_opts;

static void ev_handler(struct ns_connection *nc, int ev, void *p) {
if (ev == NS_HTTP_REQUEST) {
ns_serve_http(nc, p, s_http_server_opts);
}
}

int main(void) {
struct ns_mgr mgr;
struct ns_connection *nc;

ns_mgr_init(&mgr, NULL);
nc = ns_bind(&mgr, s_http_port, ev_handler);

// Set up HTTP server parameters
ns_set_protocol_http_websocket(nc);
s_http_server_opts.document_root = "."; // Serve current directory
s_http_server_opts.enable_directory_listing = "yes";

printf("Starting web server on port %s\n", s_http_port);
for (;;) {
ns_mgr_poll(&mgr, 1000);
}
ns_mgr_free(&mgr);

return 0;
}

0 comments on commit 93ba533

Please sign in to comment.