From 13a577dca19787eb54c268e5e83fac80670986c5 Mon Sep 17 00:00:00 2001 From: "me@ow.nekobit.net" Date: Mon, 31 Jan 2022 21:19:30 +0000 Subject: [PATCH] Paths FossilOrigin-Name: 6de5faf50051d45a938e107eb4d58d0b1eb2ae82b2f660c158594db12e0286b9 --- src/index.c | 4 ++-- src/main.c | 24 ++++++++++++++---------- src/page_config.c | 25 +++++++++++++++++++++++++ src/page_config.h | 25 +++++++++++++++++++++++++ src/path.c | 43 +++++++++++++++++++++++++++++++++++++++++++ src/path.h | 32 ++++++++++++++++++++++++++++++++ static/index.html | 6 +++--- 7 files changed, 144 insertions(+), 15 deletions(-) create mode 100644 src/page_config.c create mode 100644 src/page_config.h create mode 100644 src/path.c create mode 100644 src/path.h diff --git a/src/index.c b/src/index.c index c106b77..75e7fd8 100644 --- a/src/index.c +++ b/src/index.c @@ -34,10 +34,10 @@ void content_index(mastodont_t* api) char* status_format; mastodont_timeline_public(api, NULL, &storage, &statuses, &status_count); - /* Calculate statuses */ + /* Construct statuses into HTML */ status_format = construct_statuses(statuses, status_count, &statuses_html_count); if (status_format == NULL) - status_format = "Error in malloc! Something has gone terribly wrong..."; + status_format = "Error in malloc!"; /* Output */ printf("Content-Length: %ld\r\n\r\n", diff --git a/src/main.c b/src/main.c index 7cedf79..5ad6cc5 100644 --- a/src/main.c +++ b/src/main.c @@ -17,30 +17,34 @@ */ #include -#include #include #include #include "../config.h" #include "index.h" +#include "page_config.h" +#include "path.h" -int main() +int main(void) { - char* path = getenv("PATH_INFO"); // Content type is always HTML fputs("Content-type: text/html\r\n", stdout); // Global init mastodont_global_curl_init(); - + + // API mastodont_t api; api.url = config_instance_url; mastodont_init(&api); - - // Default index - if (path == NULL || (path && strcmp(path, "/"))) - { - content_index(&api); - } + + /******************* + * Path handling * + ******************/ + struct path_info paths[] = { + { "/config", content_config } + }; + + handle_paths(&api, paths, sizeof(paths)/sizeof(paths[0])); // Cleanup mastodont_free(&api); diff --git a/src/page_config.c b/src/page_config.c new file mode 100644 index 0000000..5d831e4 --- /dev/null +++ b/src/page_config.c @@ -0,0 +1,25 @@ +/* + * RatFE - Lightweight frontend for Pleroma + * Copyright (C) 2022 Nekobit + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include +#include "page_config.h" + +void content_config(mastodont_t* api) +{ + +} diff --git a/src/page_config.h b/src/page_config.h new file mode 100644 index 0000000..e6fd70f --- /dev/null +++ b/src/page_config.h @@ -0,0 +1,25 @@ +/* + * RatFE - Lightweight frontend for Pleroma + * Copyright (C) 2022 Nekobit + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef PAGE_CONFIG_H +#define PAGE_CONFIG_H +#include + +void content_config(mastodont_t* api); + +#endif // PAGE_CONFIG_H diff --git a/src/path.c b/src/path.c new file mode 100644 index 0000000..58e5ebd --- /dev/null +++ b/src/path.c @@ -0,0 +1,43 @@ +/* + * RatFE - Lightweight frontend for Pleroma + * Copyright (C) 2022 Nekobit + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include +#include +#include "path.h" + +void handle_paths(mastodont_t* api, struct path_info* paths, size_t paths_len) +{ + char* path = getenv("PATH_INFO"); + // "default" path + if (path == NULL || (path && strcmp(path, "/") == 0)) + { + content_index(api); + } + else if (path && path[1] == '@') + { // Account path + content_index(api); + } + else if (path) + { // Generic path + for (size_t i = 0; i < paths_len; ++i) + { + if (strcmp(path, paths[i].path) == 0) + paths[i].callback(api); + } + } +} diff --git a/src/path.h b/src/path.h new file mode 100644 index 0000000..5d35fea --- /dev/null +++ b/src/path.h @@ -0,0 +1,32 @@ +/* + * RatFE - Lightweight frontend for Pleroma + * Copyright (C) 2022 Nekobit + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef PATH_H +#define PATH_H +#include +#include + +struct path_info +{ + char* path; + void (*callback)(mastodont_t*); +}; + +void handle_paths(mastodont_t* api, struct path_info* paths, size_t paths_len); + +#endif // PATH_H diff --git a/static/index.html b/static/index.html index e1b21c7..951410f 100644 --- a/static/index.html +++ b/static/index.html @@ -3,15 +3,15 @@ %s - - + +