Statistics
| Branch: | Tag: | Revision:

mongoose / examples / connected_device_1 / server.c @ eaef5bd1

History | View | Annotate | Download (1.04 KB)

1
// Copyright (c) 2015 Cesanta Software Limited
2
// All rights reserved
3

    
4
#include "mongoose.h"
5

    
6
static const char *s_http_port = "8000";
7
static struct mg_serve_http_opts s_http_server_opts;
8

    
9
static void ev_handler(struct mg_connection *nc, int ev, void *p) {
10
  if (ev == MG_EV_HTTP_REQUEST) {
11
    mg_serve_http(nc, p, s_http_server_opts);  // Serves static content
12
  }
13
}
14

    
15
int main(void) {
16
  struct mg_mgr mgr;
17
  struct mg_connection *nc;
18
  cs_stat_t st;
19

    
20
  mg_mgr_init(&mgr, NULL);
21
  nc = mg_bind(&mgr, s_http_port, ev_handler);
22
  if (nc == NULL) {
23
    fprintf(stderr, "Cannot bind to %s\n", s_http_port);
24
    exit(1);
25
  }
26

    
27
  // Set up HTTP server parameters
28
  mg_set_protocol_http_websocket(nc);
29
  s_http_server_opts.document_root = "web_root";  // Set up web root directory
30

    
31
  if (mg_stat(s_http_server_opts.document_root, &st) != 0) {
32
    fprintf(stderr, "%s", "Cannot find web_root directory, exiting\n");
33
    exit(1);
34
  }
35

    
36
  printf("Starting web server on port %s\n", s_http_port);
37
  for (;;) {
38
    mg_mgr_poll(&mgr, 1000);
39
  }
40
  mg_mgr_free(&mgr);
41

    
42
  return 0;
43
}