Blog module
27th February 2024
I decided to write a blog module. Nothing fancy, just some code that helps with the common header and footer for each post and consolidates them for the home page.
I’ve been avoiding writing this code for a bit. My intention was to build a fairly complete (but basic) web server first, and this falls outside of that scope. In the big picture of this project I see the blog as an application that would be hosted by an application server. I’m not there yet. However, I’m fed up with copying each post to three different places, so time for a quick diversion to write some code that generates blog pages.
I started with an update to the routes
list adding a new type RT_BUFFER
. I updated the route
structure, changing the to
member form a char*
to a void*
. Where before it could only point to a string, it can now point to anything.
Next I updated the client_read
function so when routing the request it recognises the new RT_BUFFER
type and makes a call to client_prep_buffer
a new function that reads it’s content from the buffer in the route instead of a file as I had done previously.
To test this all worked I created a buffer with a simple ”hello, world!” message and added it to the list of routes.
struct buffer* test = buf_new(256);
buf_append_str(test, "hello, world!");
routes_add(routes, RT_BUFFER, "/test", test);
In all it took about 10 minutes to plan, make and test the change and it all worked lovely which naturally made me suspicious. I moved forwards anyway.
I then created a file in the blog
directory called .posts.txt
. The leading .
would mean my other routing code would ignore the file. This file would contain a list of blog posts in order, one per line. Each line would contain three fields, tab (\t
) separated: the path; the title and the date.
I also created three new files .header1.html
, .header2.html
and .footer.html
that contained fragments of HTML I would use building the pages. The plan was to then loop through the posts file and for each post, create a buffer with the page content and add it to the list of routes. This is where I figuratively ran into quicksand and my progress slowed wwwwwwaaaayyyy down.
The issue was dealing with paths. I needed to compute lots of similar paths. One for each of the three HTML fragment files, one for the posts file and then for each post one for the path of where the content was and one server path that should route to that page. I basically required a bunch of string manipulation which, as I’ve touched on before, works at a different level compared to my normal programming experience. Making sure I validated the input strings from the command line and posts file and ensured I had enough space for the concatenated path required some mental gymnastics I wasn’t ready for.
I got there in the end, but the resulting code is a mess. This was definitely an example of “make it work” and it does seem to work. I think my approach is sound. I need to review how I’m managing those paths and either convince myself that this is the correct way for C or find a cleaner way to do the same thing.
What started as a quick diversion to cut down on some admin has kicked over that can of worms that I never really completely tidied up last time. I really want to get stuck in right now, but it’s late and I have a day job.
TC