const string off_targets_by_seq(const MongooseRequest& request) { ots_data_t result; string sequence; string species; string pam_right_param; vector<uint64_t> matches; bool pam_right; string jason_result; request.getVar("seq", sequence); request.getVar("species", species); request.getVar("pam_right", pam_right_param); pam_right = false; if ( pam_right_param == "true" ) { pam_right = true; } else if ( pam_right_param == "false" ) { pam_right = false; } else { throw runtime_error("pam_right must be the string true or false"); } result = get_util(species)->off_targets_by_seq( sequence, pam_right); jason_result = util::to_string(result); return jason_result; }
const string id_json(const MongooseRequest& request) { vector<string> seqs; string ids_text, species; request.getVar("ids", ids_text); request.getVar("species", species); if ( ids_text.empty() ) throw runtime_error("Please provide ids as a comma separated string"); if ( species.empty() ) throw runtime_error("Please provide a species"); util::lc(species); vector<string> ids = util::split(ids_text); //should maybe change this to return an object of { id: sequence } for ( vector<string>::size_type i = 0; i < ids.size(); i++ ) { cerr << "Getting sequence for " << ids[i] << " (" << species << ")\n"; unsigned long long id; try { id = stoull(ids[i]); } catch (const invalid_argument &e) { throw runtime_error("id " + ids[i] + " not an integer"); } seqs.push_back( "'" + get_util(species)->get_crispr(id) + "'" ); } return util::to_json_array(seqs); }
const string generateInfoContent(const MongooseRequest &request) { string result; result = "<h1>Sample Info Page</h1>"; result += "<br />Request URI: " + request.getUri(); result += "<br />Your IP: " + ipToString(request.getRemoteIp()); time_t tim; time(&tim); result += "<br />Current date & time: " + toString(ctime(&tim)); result += "<br /><br /><a href=\"/\">Index page</a>"; return result; }
const string find_off_targets(const MongooseRequest& request) { string species; string result = "{"; vector<uint64_t> ids_all; string ids_text; request.getVar("ids", ids_text); request.getVar("species", species); if ( ids_text.empty() ) throw runtime_error("Please provide ids"); if ( species.empty() ) throw runtime_error("Please provide a species"); util::lc(species); vector<string> ids = util::split(ids_text); for ( vector<string>::size_type i = 0; i < ids.size(); i++ ) { unsigned long long id; try { id = stoull(ids[i]); } catch (const invalid_argument &e) { throw runtime_error("id " + ids[i] + " not an integer"); } ids_all.push_back(id); } vector<ots_data_t> offs = get_util(species)->find_off_targets(ids_all, true); for ( uint i = 0; i < offs.size(); ++i ) { if ( i > 0 ) result += ","; //we use my to_string here as its a struct we want a string of result += "\"" + to_string(offs[i].id) + "\":" + util::to_string(offs[i]); } result += "}"; //result = "Got data for " + to_string(offs.size()) + " crisprs"; return result; }
virtual bool handleEvent(ServerHandlingEvent eventCode, MongooseConnection &connection, const MongooseRequest &request, MongooseResponse &response) { bool res = false; if (eventCode == MG_NEW_REQUEST) { if (request.getUri() == string("/info")) { handleInfo(request, response); res = true; } } return res; }
//wrap a json string in jsonp. return true if it was jsonp bool handle_jsonp(const MongooseRequest& request, string& result) { string callback; request.getVar("callback", callback); if ( callback.empty() ) return false; callback += "("; result.insert(0, callback); result += ");"; return true; }
void get_matches(const MongooseRequest& request, vector<uint64_t>& matches) { string seq; string pam_right_str; string species; request.getVar("seq", seq); request.getVar("pam_right", pam_right_str); request.getVar("species", species); //make species lowercase util::lc( species ); //default pam_right is 2, which means don't consider the pam if ( pam_right_str.size() == 0 ) pam_right_str = "2"; if ( seq.empty() ) throw runtime_error("Please provide a sequence"); if ( seq.size() != 20 ) throw runtime_error("Sequence must be 20 bases long"); if ( pam_right_str.empty() ) throw runtime_error("Please specify pam right"); int pam_right = atoi( pam_right_str.c_str() ); if ( ! ( pam_right == 0 || pam_right == 1 || pam_right == 2 ) ) throw runtime_error("pam_right must be 0, 1 or 2"); if ( species.empty() ) throw runtime_error("Please specify a species"); cerr << "Searching for " << seq << ", pam_right is " << pam_right << endl; get_util(species)->search_by_seq( seq, pam_right, matches ); }
const string search(const MongooseRequest& request) { string result; result = "<h1>Sample Info Page</h1>"; result += "<br />Request URI: " + request.getUri(); vector<uint64_t> matches; get_matches(request, matches); //result += "<br />Seq is: " + seq; result += "<br />Found " + to_string(matches.size()) + " matches:"; for ( uint i = 0; i < matches.size(); i++ ) { result += "<br />" + to_string(matches[i]); } return result; }
bool HttpServer::handleEvent(ServerHandlingEvent eventCode, MongooseConnection &connection, const MongooseRequest &request, MongooseResponse &response) { bool res = false; switch (eventCode) { case MG_NEW_REQUEST: // check that we want to handle this. if (request.getUri().substr(0,strlen("/racka3/")) == "/racka3/") { res = handleNewRequest(eventCode,connection,request,response); } break; default: break; } return res; }
bool HttpServer::handleNewRequest(ServerHandlingEvent eventCode, MongooseConnection &connection, const MongooseRequest &request, MongooseResponse &response) { bool res = false; cJSON* json = 0; string uri = request.getUri(); bool handled = false; // if this is a post, get the post data if (request.getRequestMethod() == "POST") { std::string buffer; char c; while (connection.read(&c,1)) { buffer.push_back(c); } json = cJSON_Parse(buffer.c_str()); // if we couldnt parse the json? make empty json if (!json) { json = cJSON_CreateObject(); } } else { // make an empty json object json = cJSON_CreateObject(); } // now act depending on the path if (uri == string("/racka3/getallplugins")) // <-- rack manipulation { _pluginHost->getAvailablePlugins(json); } else if (uri == string("/racka3/addplugin")) { _pluginHost->addPlugin(json); } else if (uri == string("/racka3/removeplugin")) { _pluginHost->removePlugin(json); } else if (uri == string("/racka3/moveplugin")) { _pluginHost->movePlugin(json); } else if (uri == string("/racka3/setparamvalue")) { _pluginHost->setPluginParam(json); } else if (uri == string("/racka3/getpluginchain")) { _pluginHost->getPluginChain(json); } else if (uri == string("/racka3/storePluginPreset")) { // <-- plugin presets _pluginHost->storePluginPreset(json); } else if (uri == string("/racka3/deletePluginPreset")) { _pluginHost->deletePluginPreset(json); } else if (uri == string("/racka3/storeRackPreset")) { // <-- rack presets _pluginHost->storeRackPreset(json); } else if (uri == string("/racka3/deleteRackPreset")) { _pluginHost->deleteRackPreset(json); } else if (uri == string("/racka3/loadRackPreset")) { _pluginHost->loadRackPreset(json); } else if (uri == string("/racka3/listRackPresets")) { _pluginHost->listRackPresets(json); } else if (uri == string("/racka3/listDevices")) { // <-- soundcard configuration _soundInterface->listDevices(json); } else if (uri == string("/racka3/setDevice")) { if (_pluginHost->getPluginChainSize()) cJSON_AddStringToObject(json,"error","cannot change sound settings unless rack is empty"); else _soundInterface->init(json); } else if (uri == string("/racka3/getCurrentDevice")) { _soundInterface->getCurrent(json); } // did we make any json? if (json->child) { // output the json char * content = cJSON_Print(json); response.setStatus(200); response.setContentType("application/json"); response.addContent(content); free(content); } else { // no json: error code for you. response.setStatus(404); } response.setCacheDisabled(); response.setConnectionAlive(false); response.write(); // clean up cJSON_Delete(json); return res; }
virtual bool handleEvent(ServerHandlingEvent eventCode, MongooseConnection& connection, const MongooseRequest& request, MongooseResponse& response) { bool res = false; //gets set to true if successful string result, content_type; if (eventCode == MG_NEW_REQUEST) { string uri = request.getUri(); //returns 0 if they match if ( ! uri.compare(0, 4, "/api") ) { cerr << "API URI: " << uri << endl; //prefix is api so we know it will be json content_type = "application/json"; try { if ( uri == string("/api/search") ) { result = search_json(request); res = true; } else if ( uri == string("/api/id") ) { result = id_json(request); res = true; } else if ( uri == string("/api/off_targets") ) { result = find_off_targets(request); res = true; } else if ( uri == string("/api/off_targets_by_seq") ) { result = off_targets_by_seq(request); res = true; } else { cerr << "Couldn't find api URI " << uri << endl; } } catch (const runtime_error& error) { string e = error.what(); result = json_error( e ); res = true; //we need this or we return 404 } if ( res ) handle_jsonp(request, result); } else { cerr << "URI: " << uri << endl; content_type = "text/html"; try { if ( uri == string("/search") ) { result = search(request); } else { cerr << "Couldn't find URI " << uri << endl; } } catch (const runtime_error& error) { result = "<h1>" + string(error.what()) + "</h1>"; } } } if ( res ) { process_response(request, response, content_type, result); last_accessed.set(); //update last accessed to now } return res; }