C and C++ web framework.
http://rapida.vilor.one/docs
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.4 KiB
52 lines
1.4 KiB
#include "../../include/rapida.h" |
|
#include "../../include/servers/tcp.h" |
|
#include <stdio.h> |
|
#include <stdlib.h> |
|
#include <string.h> |
|
|
|
static void products_handler(rpd_req *req, rpd_res *res, void *userdata) |
|
{ |
|
char *body = "<!DOCTYPE html>" |
|
"<html>" |
|
"<head><title>Product</title></head>" |
|
"<body>" |
|
"<h1>Product page</h1>" |
|
"<pre>" |
|
"Category: %s\n" |
|
"Id: %s" |
|
"</pre>" |
|
"</body>" |
|
"</html>"; |
|
rpd_keyval_item *cat = rpd_keyval_find(&req->params, "category"); |
|
rpd_keyval_item *id = rpd_keyval_find(&req->params, "id"); |
|
|
|
if (!cat || !id) { |
|
res->status = rpd_res_st_internal_server_error; |
|
return; |
|
} |
|
|
|
int bufflen = strlen(body) + strlen(cat->val) + strlen(id->val); |
|
res->body = (char *) malloc(sizeof(char) * (bufflen + 1)); |
|
if (!res->body) { |
|
perror("malloc"); |
|
res->status = rpd_res_st_internal_server_error; |
|
return; |
|
} |
|
res->body[bufflen] = '\0'; |
|
|
|
sprintf(res->body, body, cat->val, id->val); |
|
|
|
res->status = rpd_res_st_ok; |
|
|
|
rpd_keyval_insert(&res->headers, "Content-Type", "text/html"); |
|
} |
|
|
|
int main() |
|
{ |
|
rpd_app app; |
|
rpd_app_create(&app); |
|
|
|
rpd_app_add_route(&app, "/products/:category/:id", &products_handler, NULL); |
|
|
|
return rpd_tcp_server_start(&app, "http://localhost:8080"); |
|
}
|
|
|