bool TsHttpRpc::init_https() { ex_wstr file_ssl_cert = g_env.m_res_path; ex_path_join(file_ssl_cert, false, L"localhost.pem", NULL); ex_wstr file_ssl_key = g_env.m_res_path; ex_path_join(file_ssl_key, false, L"localhost.key", NULL); ex_astr _ssl_cert; ex_wstr2astr(file_ssl_cert, _ssl_cert); ex_astr _ssl_key; ex_wstr2astr(file_ssl_key, _ssl_key); const char *err = NULL; struct mg_bind_opts bind_opts; memset(&bind_opts, 0, sizeof(bind_opts)); bind_opts.ssl_cert = _ssl_cert.c_str(); bind_opts.ssl_key = _ssl_key.c_str(); bind_opts.error_string = &err; char addr[128] = { 0 }; ex_strformat(addr, 128, "tcp://localhost:%d", TS_HTTPS_RPC_PORT); struct mg_connection* nc = NULL; nc = mg_bind_opt(&m_mg_mgr, addr, _mg_event_handler, bind_opts); if (nc == NULL) { EXLOGE("[rpc] TsHttpRpc::init_https() localhost:%d\n", TS_HTTPS_RPC_PORT); return false; } nc->user_data = this; mg_set_protocol_http_websocket(nc); return _on_init(); }
static enum v7_err start_http_server(struct v7 *v7, const char *addr, v7_val_t obj, const char *ca_cert, const char *cert) { enum v7_err rcode = V7_OK; struct mg_connection *c; struct user_data *ud; struct mg_bind_opts opts; memset(&opts, 0, sizeof(opts)); #ifdef MG_ENABLE_SSL opts.ssl_ca_cert = ca_cert; opts.ssl_cert = cert; #else (void) ca_cert; (void) cert; #endif c = mg_bind_opt(&sj_mgr, addr, http_ev_handler, opts); if (c == NULL) { rcode = v7_throwf(v7, "Error", "Cannot bind"); goto clean; } mg_set_protocol_http_websocket(c); c->user_data = ud = (struct user_data *) malloc(sizeof(*ud)); ud->v7 = v7; ud->obj = obj; ud->handler = v7_get(v7, obj, "_cb", 3); v7_own(v7, &ud->obj); clean: return rcode; }
/** * @brief Function for application main entry. */ int main(void) { cs_log_set_file(stdout); bleconfig_init(); { struct mg_mgr mgr; mg_mgr_init(&mgr, NULL); // Initialize event manager object // Note that many connections can be added to a single event manager // Connections can be created at any point, e.g. in event handler function const char *err; struct mg_bind_opts opts; struct mg_connection *nc = NULL; memset(&opts, 0x00, sizeof(opts)); opts.error_string = &err; nc = mg_bind_opt(&mgr, "80", ev_handler, opts); // Create listening connection and add it to the event manager if (nc == NULL) { printf("Failed to create listener: %s\n", err); return 1; } mg_set_protocol_http_websocket(nc); for (;;) { // Start infinite event loop bleconfig_poll(); mg_mgr_poll(&mgr, 0); } } }
int main(void) { struct mg_mgr mgr; struct mg_connection *nc; struct mg_bind_opts bind_opts; const char *err; mg_mgr_init(&mgr, NULL); memset(&bind_opts, 0, sizeof(bind_opts)); bind_opts.ssl_cert = s_ssl_cert; bind_opts.ssl_key = s_ssl_key; bind_opts.error_string = &err; printf("Starting SSL server on port %s, cert from %s, key from %s\n", s_http_port, bind_opts.ssl_cert, bind_opts.ssl_key); nc = mg_bind_opt(&mgr, s_http_port, ev_handler, bind_opts); if (nc == NULL) { printf("Failed to create listener: %s\n", err); return 1; } // Set up HTTP server parameters mg_set_protocol_http_websocket(nc); s_http_server_opts.document_root = "."; // Serve current directory s_http_server_opts.enable_directory_listing = "yes"; for (;;) { mg_mgr_poll(&mgr, 1000); } mg_mgr_free(&mgr); return 0; }
static void mg_init(struct mg_mgr *mgr) { LOG(LL_INFO, ("MG task running")); stop_nwp(); /* See function description in wifi.c */ LOG(LL_INFO, ("Starting NWP...")); int role = sl_Start(0, 0, 0); if (role < 0) { LOG(LL_ERROR, ("Failed to start NWP")); return; } { SlVersionFull ver; unsigned char opt = SL_DEVICE_GENERAL_VERSION; unsigned char len = sizeof(ver); memset(&ver, 0, sizeof(ver)); sl_DevGet(SL_DEVICE_GENERAL_CONFIGURATION, &opt, &len, (unsigned char *) (&ver)); LOG(LL_INFO, ("NWP v%d.%d.%d.%d started, host v%d.%d.%d.%d", ver.NwpVersion[0], ver.NwpVersion[1], ver.NwpVersion[2], ver.NwpVersion[3], SL_MAJOR_VERSION_NUM, SL_MINOR_VERSION_NUM, SL_VERSION_NUM, SL_SUB_VERSION_NUM)); } GPIO_IF_LedToggle(MCU_RED_LED_GPIO); data_init_sensors(TMP006_ADDR, BM222_ADDR); sl_fs_init(); #if defined(WIFI_STA_SSID) if (!wifi_setup_sta(WIFI_STA_SSID, WIFI_STA_PASS)) { LOG(LL_ERROR, ("Error setting up WiFi station")); } #elif defined(WIFI_AP_SSID) if (!wifi_setup_ap(WIFI_AP_SSID, WIFI_AP_PASS, WIFI_AP_CHAN)) { LOG(LL_ERROR, ("Error setting up WiFi AP")); } #else #error WiFi not configured #endif /* We don't need SimpleLink's web server. */ sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID); const char *err = ""; struct mg_bind_opts opts; memset(&opts, 0, sizeof(opts)); opts.error_string = &err; struct mg_connection *nc = mg_bind_opt(mgr, "80", mg_ev_handler, opts); if (nc != NULL) { mg_set_protocol_http_websocket(nc); nc->ev_timer_time = mg_time(); /* Start data collection */ } else { LOG(LL_ERROR, ("Failed to create listener: %s", err)); } }
int main() { struct mg_mgr mgr; Serial pc(SERIAL_TX, SERIAL_RX, 115200); printf("Mongoose demo\n"); led_green = 1; /* off */ led_blue = 1; /* off */ SimpleLinkInterface wifi(PG_10, PG_11, SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS); sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID); wifi.connect(WIFI_STA_SSID, WIFI_STA_PASS); const char *ip = wifi.get_ip_address(); const char *gw = wifi.get_gateway(); const char *mac = wifi.get_mac_address(); printf("IP address is: %s\n", ip ? ip : "No IP"); printf("GW address is: %s\n", gw ? gw : "No IP"); printf("MAC address is: %s\n", mac ? mac : "No MAC"); mg_mgr_init(&mgr, NULL); const char *err; struct mg_bind_opts opts = {}; opts.error_string = &err; mg_connection *nc = mg_bind_opt(&mgr, HTTP_SERVER_PORT, ev_handler, opts); if (nc == NULL) { printf("Failed to create listener: %s\r\n", err); led_red = 0; /* on */ return 1; } mg_set_protocol_http_websocket(nc); printf("Server address: http://%s:%s\n", ip, HTTP_SERVER_PORT); while (true) { mg_mgr_poll(&mgr, 1000); led_green = !led_green; } }
Server::Server(ManagerConfig *config, const std::string &config_file) { srand((unsigned) time(0)); m_config = config; m_user = CONFIG_STRING(m_config, "service.admin_username"); m_password = CONFIG_STRING(m_config, "service.admin_password"); mg_mgr_init(&m_mgr, this); struct mg_bind_opts opts; memset(&opts, 0, sizeof(opts)); const char *error_string; opts.error_string = &error_string; m_nc = mg_bind_opt(&m_mgr, std::string(":" + boost::lexical_cast<std::string>(CONFIG_INT(m_config, "service.port"))).c_str(), &_event_handler, opts); if (!m_nc) { std::cerr << "Error creating server: " << error_string << "\n"; exit(1); } if (!CONFIG_STRING(m_config, "service.cert").empty()) { const char *err_str = mg_set_ssl(m_nc, CONFIG_STRING(m_config, "service.cert").c_str(), NULL); if (err_str) { std::cerr << "Error setting SSL certificate: " << err_str << "\n"; exit(1); } } mg_set_protocol_http_websocket(m_nc); s_http_server_opts.document_root = CONFIG_STRING(m_config, "service.data_dir").c_str(); std::ifstream header(std::string(CONFIG_STRING(m_config, "service.data_dir") + "/header.html").c_str(), std::ios::in); if (header) { header.seekg(0, std::ios::end); m_header.resize(header.tellg()); header.seekg(0, std::ios::beg); header.read(&m_header[0], m_header.size()); header.close(); } std::ifstream footer(std::string(CONFIG_STRING(m_config, "service.data_dir") + "/footer.html").c_str(), std::ios::in); if (footer) { footer.seekg(0, std::ios::end); m_footer.resize(footer.tellg()); footer.seekg(0, std::ios::beg); footer.read(&m_footer[0], m_footer.size()); footer.close(); } m_storageCfg = new Config(); m_storageCfg->load(config_file); Logging::initManagerLogging(m_storageCfg); std::string error; m_storage = StorageBackend::createBackend(m_storageCfg, error); if (m_storage == NULL) { std::cerr << "Error creating StorageBackend! " << error << "\n"; std::cerr << "Registering new Spectrum 2 manager users won't work" << "\n"; } else if (!m_storage->connect()) { delete m_storage; m_storage = NULL; std::cerr << "Can't connect to database!\n"; } m_apiServer = new APIServer(config, m_storage); }