about summary refs log tree commit diff
diff options
context:
space:
mode:
authorsternenseemann <0rpkxez4ksa01gb3typccl0i@systemli.org>2021-05-20 01:10:30 +0200
committersternenseemann <0rpkxez4ksa01gb3typccl0i@systemli.org>2021-05-20 01:10:30 +0200
commita6a280037842ac79655355a9d200aafee00a61ba (patch)
tree9b5de45b96b664da9db342e903f23ad7fc993fb5
parentb97c3c15b11e1a761d77b37b0a811aa3090cc75a (diff)
refactor(main): split entry loading out of routing logic
-rw-r--r--main.c29
1 files changed, 15 insertions, 14 deletions
diff --git a/main.c b/main.c
index 0c8378c..4b01b64 100644
--- a/main.c
+++ b/main.c
@@ -130,7 +130,7 @@ int main(void) {
     int count = 0;
     int status = 500;
 
-    // Routing: determine page_type and feed_type
+    // Routing: determine page_type
     // already allocate data for single entries
     if(script_name == NULL) {
         fputs("Missing CGI environment variable SCRIPT_NAME\n", stderr);
@@ -143,6 +143,20 @@ int main(void) {
     } else if(strcmp(path_info, "/atom.xml") == 0) {
         page_type = PAGE_TYPE_FEED;
     } else {
+        page_type = PAGE_TYPE_ENTRY;
+    }
+
+    // populate data necessary for responses or switch to error page type
+    if(page_type == PAGE_TYPE_INDEX || page_type == PAGE_TYPE_FEED) {
+        count = make_index(BLOG_DIR, script_name, 0, &entries);
+
+        if(count < 0) {
+            page_type = PAGE_TYPE_ERROR;
+            status = 500;
+        } else {
+            status = 200;
+        }
+    } else {
         // single entry is just a special index
         entries = malloc(sizeof(struct entry));
         if(entries == NULL) {
@@ -152,25 +166,12 @@ int main(void) {
         }
 
         if(status == 200 && entry_get_text(entries) != -1) {
-            page_type = PAGE_TYPE_ENTRY;
             count = 1;
         } else {
             page_type = PAGE_TYPE_ERROR;
         }
     }
 
-    // construct index for feeds and index page
-    if(page_type == PAGE_TYPE_INDEX || page_type == PAGE_TYPE_FEED) {
-        count = make_index(BLOG_DIR, script_name, 0, &entries);
-
-        if(count < 0) {
-            page_type = PAGE_TYPE_ERROR;
-            status = 500;
-        } else {
-            status = 200;
-        }
-    }
-
     // confirm status and page_type match
     assert(status == 200 || page_type == PAGE_TYPE_ERROR);
     assert(page_type != PAGE_TYPE_ERROR || status != 200);