|
|
|
#include "../../include/rapida.h"
|
|
|
|
#include "../../include/servers/tcp.h"
|
|
|
|
#include <string.h> /* for strdup() */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* \brief Process home page request.
|
|
|
|
*
|
|
|
|
* \param req Request.
|
|
|
|
* \param res Response.
|
|
|
|
* \param userdata Additional data that you can pass to this handler
|
|
|
|
* when adding a route.
|
|
|
|
*/
|
|
|
|
static void home_page_handler(rpd_req *req, rpd_res *res, void *userdata)
|
|
|
|
{
|
|
|
|
/* Check request method */
|
|
|
|
switch (req->method) {
|
|
|
|
case HEAD:
|
|
|
|
/* Process GET request */
|
|
|
|
res->status = rpd_res_st_ok;
|
|
|
|
|
|
|
|
/* Please allocate data on the heap,
|
|
|
|
* because after calling this handler
|
|
|
|
* Rapida will free it all.
|
|
|
|
*/
|
|
|
|
res->content_type = strdup("text/plain");
|
|
|
|
case GET:
|
|
|
|
res->body = strdup("Hello World!");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Other request methods are not implemented,
|
|
|
|
* so set the appropriate response status.
|
|
|
|
*/
|
|
|
|
res->status = rpd_res_st_not_implemented;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* You don't have to do something like send a response.
|
|
|
|
* Rapida will do this after calling this handler.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
rpd_app app;
|
|
|
|
/* Initialize application. */
|
|
|
|
rpd_app_create(&app);
|
|
|
|
|
|
|
|
/* Add home "/" page handler. */
|
|
|
|
rpd_app_add_route(&app, "/", &home_page_handler, NULL);
|
|
|
|
|
|
|
|
/* Run the application and return its status code. */
|
|
|
|
return rpd_tcp_server_start(&app, "http://localhost:8080");
|
|
|
|
}
|