Ejemplo n.º 1
0
static void APIHelloWorld(Messaging::Context&, Marshal::Object& response)
{
	// log server
	logs.info() << "[method:hello_world] sending hello world !";
	// the response
	response["message"] = "Hellow world !";
}
Ejemplo n.º 2
0
int main()
{
	// Welcome !
	logs.notice() << "Yuni DBI Example";
	logs.info() << "using the SQLite default adapter";
	logs.info(); // empty line

	return RunExample() ? 0 : EXIT_FAILURE;
}
Ejemplo n.º 3
0
static bool RunExample()
{
	// our connection pool
	DBI::ConnectorPool connectors;

	// database location
	logs.info() << "opening the database file";
	auto error = connectors.open(new DBI::Adapter::SQLite(), "dbi-example.db");
	if (error)
	{
		logs.error() << "impossible to establish a connection to the database";
		return false;
	}

	// begin a new transaction
	auto tx = connectors.begin();

	// create a new table if not already exists
	logs.info() << "creating the table 'example'";
	tx.perform("CREATE TABLE IF NOT EXISTS example (x INTEGER PRIMARY KEY);");

	// truncate the table, if it already exists
	logs.info() << "truncating the table";
	tx.truncate("example");

	// fill it with some values
	logs.info() << "inserting some data";
	tx.perform("INSERT INTO example (x) VALUES (42), (3), (-1), (-10), (-3);");

	// iterating through a result set
	{
		uint rowindex = 0;

		logs.info();
		logs.info() << "iterating through all values";
		auto stmt = tx.prepare("SELECT * FROM example WHERE x > $1 ORDER BY x");
		stmt.bind(0, -5); // x > -5
		stmt.execute();

		// iterate through all values
		stmt.each([&] (DBI::Row& row) -> bool
		{
			sint64 x = row[0].asInt64();
			logs.info() << "  :: resultset: in row " << (++rowindex) << ":  x = " << x;
			return true; // continue
		});
	}

	// commit changes
	logs.info() << "commit";
	if (DBI::errNone != (error = tx.commit()))
	{
		logs.error() << "commit failed";
		return false;
	}

	return true;
}
Ejemplo n.º 4
0
static bool StartService(Messaging::Service& service)
{
	logs.info() << "starting";
	Net::Error error = service.start();

	switch (error)
	{
		case Net::errNone:
			break;
		default:
			logs.fatal() << "impossible to start the service: " << error;
			return false;
	}

	logs.info() << "examples:";
	logs.info() << "# curl -i -X GET 'http://localhost:6042/hello_world'";
	logs.info() << "# curl -i -X GET 'http://localhost:6042/sum?a=50&b=42'";
	logs.info() << "<ctrl> + C to quit";
	logs.info(); // empty line for beauty
	return true;
}
Ejemplo n.º 5
0
static void APISum(Messaging::Context& context, Marshal::Object& response)
{
	// our parameters
	double a, b;

	if (not context.params["a"].to(a) or not context.params["b"].to(b))
	{
		// the parameters are not valid, exiting with a 400 status code
		// (Bad Request, The request cannot be fulfilled due to bad syntax.)
		// see http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
		context.httpStatus = 400;
		// log server
		logs.error() << "[method:sum] invalid parameters";
		return;
	}

	// log server
	logs.info() << "[method:sum] " << a << " + " << b << " = " << (a + b);
	// the response
	response["a"] = a;
	response["b"] = b;
	response["sum"] = (a + b);
}
Ejemplo n.º 6
0
int main(int, char**)
{
	// Welcome !
	logs.notice() << "Yuni REST Server Example";
	logs.info() << "http://www.libyuni.org";
	logs.info(); // empty line

	// The service itself
	Messaging::Service  service;

	// Prepare the API
	PrepareTheAPI(service);
	// Preparing all transports
	PrepareTransports(service);

	// Starting the new service
	if (not StartService(service))
		return EXIT_FAILURE;

	// Waiting for the end of the server
	service.wait();

	return 0;
}