Statistics
| Branch: | Tag: | Revision:

mongoose / examples / restful_server / restful_server.c @ eaef5bd1

History | View | Annotate | Download (3.92 KB)

1
/*
2
 * Copyright (c) 2014 Cesanta Software Limited
3
 * All rights reserved
4
 */
5

    
6
#include "mongoose.h"
7

    
8
static const char *s_http_port = "8000";
9
static struct mg_serve_http_opts s_http_server_opts;
10

    
11
static void handle_sum_call(struct mg_connection *nc, struct http_message *hm) {
12
  char n1[100], n2[100];
13
  double result;
14

    
15
  /* Get form variables */
16
  mg_get_http_var(&hm->body, "n1", n1, sizeof(n1));
17
  mg_get_http_var(&hm->body, "n2", n2, sizeof(n2));
18

    
19
  /* Send headers */
20
  mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
21

    
22
  /* Compute the result and send it back as a JSON object */
23
  result = strtod(n1, NULL) + strtod(n2, NULL);
24
  mg_printf_http_chunk(nc, "{ \"result\": %lf }", result);
25
  mg_send_http_chunk(nc, "", 0); /* Send empty chunk, the end of response */
26
}
27

    
28
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
29
  struct http_message *hm = (struct http_message *) ev_data;
30

    
31
  switch (ev) {
32
    case MG_EV_HTTP_REQUEST:
33
      if (mg_vcmp(&hm->uri, "/api/v1/sum") == 0) {
34
        handle_sum_call(nc, hm); /* Handle RESTful call */
35
      } else if (mg_vcmp(&hm->uri, "/printcontent") == 0) {
36
        char buf[100] = {0};
37
        memcpy(buf, hm->body.p,
38
               sizeof(buf) - 1 < hm->body.len ? sizeof(buf) - 1 : hm->body.len);
39
        printf("%s\n", buf);
40
      } else {
41
        mg_serve_http(nc, hm, s_http_server_opts); /* Serve static content */
42
      }
43
      break;
44
    default:
45
      break;
46
  }
47
}
48

    
49
int main(int argc, char *argv[]) {
50
  struct mg_mgr mgr;
51
  struct mg_connection *nc;
52
  struct mg_bind_opts bind_opts;
53
  int i;
54
  char *cp;
55
  const char *err_str;
56
#if MG_ENABLE_SSL
57
  const char *ssl_cert = NULL;
58
#endif
59

    
60
  mg_mgr_init(&mgr, NULL);
61

    
62
  /* Use current binary directory as document root */
63
  if (argc > 0 && ((cp = strrchr(argv[0], DIRSEP)) != NULL)) {
64
    *cp = '\0';
65
    s_http_server_opts.document_root = argv[0];
66
  }
67

    
68
  /* Process command line options to customize HTTP server */
69
  for (i = 1; i < argc; i++) {
70
    if (strcmp(argv[i], "-D") == 0 && i + 1 < argc) {
71
      mgr.hexdump_file = argv[++i];
72
    } else if (strcmp(argv[i], "-d") == 0 && i + 1 < argc) {
73
      s_http_server_opts.document_root = argv[++i];
74
    } else if (strcmp(argv[i], "-p") == 0 && i + 1 < argc) {
75
      s_http_port = argv[++i];
76
    } else if (strcmp(argv[i], "-a") == 0 && i + 1 < argc) {
77
      s_http_server_opts.auth_domain = argv[++i];
78
#if MG_ENABLE_JAVASCRIPT
79
    } else if (strcmp(argv[i], "-j") == 0 && i + 1 < argc) {
80
      const char *init_file = argv[++i];
81
      mg_enable_javascript(&mgr, v7_create(), init_file);
82
#endif
83
    } else if (strcmp(argv[i], "-P") == 0 && i + 1 < argc) {
84
      s_http_server_opts.global_auth_file = argv[++i];
85
    } else if (strcmp(argv[i], "-A") == 0 && i + 1 < argc) {
86
      s_http_server_opts.per_directory_auth_file = argv[++i];
87
    } else if (strcmp(argv[i], "-r") == 0 && i + 1 < argc) {
88
      s_http_server_opts.url_rewrites = argv[++i];
89
#if MG_ENABLE_HTTP_CGI
90
    } else if (strcmp(argv[i], "-i") == 0 && i + 1 < argc) {
91
      s_http_server_opts.cgi_interpreter = argv[++i];
92
#endif
93
#if MG_ENABLE_SSL
94
    } else if (strcmp(argv[i], "-s") == 0 && i + 1 < argc) {
95
      ssl_cert = argv[++i];
96
#endif
97
    } else {
98
      fprintf(stderr, "Unknown option: [%s]\n", argv[i]);
99
      exit(1);
100
    }
101
  }
102

    
103
  /* Set HTTP server options */
104
  memset(&bind_opts, 0, sizeof(bind_opts));
105
  bind_opts.error_string = &err_str;
106
#if MG_ENABLE_SSL
107
  if (ssl_cert != NULL) {
108
    bind_opts.ssl_cert = ssl_cert;
109
  }
110
#endif
111
  nc = mg_bind_opt(&mgr, s_http_port, ev_handler, bind_opts);
112
  if (nc == NULL) {
113
    fprintf(stderr, "Error starting server on port %s: %s\n", s_http_port,
114
            *bind_opts.error_string);
115
    exit(1);
116
  }
117

    
118
  mg_set_protocol_http_websocket(nc);
119
  s_http_server_opts.enable_directory_listing = "yes";
120

    
121
  printf("Starting RESTful server on port %s, serving %s\n", s_http_port,
122
         s_http_server_opts.document_root);
123
  for (;;) {
124
    mg_mgr_poll(&mgr, 1000);
125
  }
126
  mg_mgr_free(&mgr);
127

    
128
  return 0;
129
}