/*
 * Copyright (c) 2014 Cesanta Software Limited
 * All rights reserved
 *
 * To test this server, do
 *   $ curl -d '{"id":1,method:"sum",params:[22,33]}' 127.0.0.1:8000
 */

#include "mongoose.h"

static const char *s_http_port = "8000";

static int rpc_sum(char *buf, int len, struct mg_rpc_request *req) {
  double sum = 0;
  int i;

  if (req->params[0].type != JSON_TYPE_ARRAY) {
    return mg_rpc_create_std_error(buf, len, req,
                                   JSON_RPC_INVALID_PARAMS_ERROR);
  }

  for (i = 0; i < req->params[0].num_desc; i++) {
    if (req->params[i + 1].type != JSON_TYPE_NUMBER) {
      return mg_rpc_create_std_error(buf, len, req,
                                     JSON_RPC_INVALID_PARAMS_ERROR);
    }
    sum += strtod(req->params[i + 1].ptr, NULL);
  }
  return mg_rpc_create_reply(buf, len, req, "f", sum);
}

static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
  struct http_message *hm = (struct http_message *) ev_data;
  static const char *methods[] = {"sum", NULL};
  static mg_rpc_handler_t handlers[] = {rpc_sum, NULL};
  char buf[100];

  switch (ev) {
    case MG_EV_HTTP_REQUEST:
      mg_rpc_dispatch(hm->body.p, hm->body.len, buf, sizeof(buf), methods,
                      handlers);
      mg_printf(nc,
                "HTTP/1.0 200 OK\r\nContent-Length: %d\r\n"
                "Content-Type: application/json\r\n\r\n%s",
                (int) strlen(buf), buf);
      nc->flags |= MG_F_SEND_AND_CLOSE;
      break;
    default:
      break;
  }
}

int main(void) {
  struct mg_mgr mgr;
  struct mg_connection *nc;

  mg_mgr_init(&mgr, NULL);
  nc = mg_bind(&mgr, s_http_port, ev_handler);
  mg_set_protocol_http_websocket(nc);

  printf("Starting JSON-RPC server on port %s\n", s_http_port);
  for (;;) {
    mg_mgr_poll(&mgr, 1000);
  }
  mg_mgr_free(&mgr);

  return 0;
}
