Got the ulfius rest server working
This commit is contained in:
@@ -12,8 +12,8 @@
|
|||||||
#define U_DISABLE_WEBSOCKET
|
#define U_DISABLE_WEBSOCKET
|
||||||
#include <ulfius.h>
|
#include <ulfius.h>
|
||||||
|
|
||||||
#define PORT 8080
|
#define PORT 1337
|
||||||
#define PREFIX "/test"
|
#define PREFIX "/abshum"
|
||||||
|
|
||||||
int callback_welcome (const struct _u_request * request, struct _u_response * response, void * user_data) {
|
int callback_welcome (const struct _u_request * request, struct _u_response * response, void * user_data) {
|
||||||
ulfius_set_string_body_response(response, 200, "Welcome to the HumidityPi rest service!");
|
ulfius_set_string_body_response(response, 200, "Welcome to the HumidityPi rest service!");
|
||||||
@@ -21,6 +21,8 @@ int callback_welcome (const struct _u_request * request, struct _u_response * re
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
char * print_map(const struct _u_map * map) {
|
char * print_map(const struct _u_map * map) {
|
||||||
char * line, * to_return = NULL;
|
char * line, * to_return = NULL;
|
||||||
const char **keys, * value;
|
const char **keys, * value;
|
||||||
@@ -51,15 +53,49 @@ char * print_map(const struct _u_map * map) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int callback_post_test (const struct _u_request * request, struct _u_response * response, void * user_data) {
|
char * read_file(const char * filename) {
|
||||||
char * post_params = print_map(request->map_post_body);
|
char * buffer = NULL;
|
||||||
char * response_body = msprintf("Hello World!\n%s", post_params);
|
long length;
|
||||||
ulfius_set_string_body_response(response, 200, response_body);
|
FILE * f = fopen (filename, "rb");
|
||||||
o_free(response_body);
|
if (filename != NULL) {
|
||||||
o_free(post_params);
|
|
||||||
return U_CALLBACK_CONTINUE;
|
if (f) {
|
||||||
|
fseek (f, 0, SEEK_END);
|
||||||
|
length = ftell (f);
|
||||||
|
fseek (f, 0, SEEK_SET);
|
||||||
|
buffer = o_malloc (length + 1);
|
||||||
|
if (buffer) {
|
||||||
|
fread (buffer, 1, length, f);
|
||||||
|
}
|
||||||
|
buffer[length] = '\0';
|
||||||
|
fclose (f);
|
||||||
|
}
|
||||||
|
return buffer;
|
||||||
|
} else {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int callback_all_test_foo (const struct _u_request * request, struct _u_response * response, void * user_data) {
|
||||||
|
//char * url_params = print_map(request->map_url);
|
||||||
|
//char * response_body = msprintf("parameters from the url are \n%s\n\n",
|
||||||
|
//url_params);
|
||||||
|
int i;
|
||||||
|
float temphum[2];
|
||||||
|
const char **keys;
|
||||||
|
keys = u_map_enum_keys(request->map_url);
|
||||||
|
|
||||||
|
for (i = 0; i < 2; i++) {
|
||||||
|
temphum[i] = atoi(u_map_get(request->map_url, keys[i]));
|
||||||
|
}
|
||||||
|
char *response_body = msprintf("Temp: %f, Hum: %f",temphum[0], temphum[1]);
|
||||||
|
|
||||||
|
ulfius_set_string_body_response(response, 200, response_body);
|
||||||
|
//o_free(url_params);
|
||||||
|
return U_CALLBACK_CONTINUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
@@ -76,7 +112,7 @@ int main(void) {
|
|||||||
|
|
||||||
// Endpoint list declaration
|
// Endpoint list declaration
|
||||||
ulfius_add_endpoint_by_val(&instance, "GET", "/welcome", NULL, 0, &callback_welcome, NULL);
|
ulfius_add_endpoint_by_val(&instance, "GET", "/welcome", NULL, 0, &callback_welcome, NULL);
|
||||||
ulfius_add_endpoint_by_val(&instance, "POST", "/test", NULL, 0, &callback_post_test, NULL);
|
ulfius_add_endpoint_by_val(&instance, "GET", PREFIX, "/:temp/:hum", 0, &callback_all_test_foo, NULL);
|
||||||
|
|
||||||
// Start the framework
|
// Start the framework
|
||||||
if (ulfius_start_framework(&instance) == U_OK) {
|
if (ulfius_start_framework(&instance) == U_OK) {
|
||||||
|
Reference in New Issue
Block a user