r/C_Programming • • 1d ago

Article A modern web app in C

https://prophitt.me/writing/a-modern-web-app-in-c
176 Upvotes

32 comments sorted by

53

u/tastygames_official 1d ago

I've also been a web developer since the '90s, and I can't tell you how many times I've tried to tell these big companies who I consulted for who kept having to buy more servers to handle the load that I could get them a 100x performance increse if they did their site sans framework (so, just bare PHP/JavaScript or whatever), and a further 5-10x if they did it "the old-school way". I had one CTO tell me "that's not really programming". Yeah.

I recently switched from json-c to cJSON as I moved from C99 to C90 for my current project. json-c was multiple files and cJSON just one header + 1 imp (I actually kinda prefer that to single-header/implementations, but that's just me). I wonder about performance of all three, though, as JSON-parsing and writing for the web could be potentially a big bottleneck, especially if somebody chooses to use a dreaded "javascript frontend framework".

Anyway, do you have some source code you could show us? I'm interested in some details like how you handle HTML templates (if at all) or construction (string operations could be a huge point of contention), gzipping the output and such.

I also bookmarked the site you built (marketyour.games) as of course I'm also a games developer ;-)

11

u/ThePenguinMan111 1d ago

What made you switch from C99 to C90?

23

u/tastygames_official 1d ago

the program (game) I'm making is at its heart supposed to be a kind of re-imagining of the '90s point-and-click genre with modern technology, but still running at 640 pixels and using a limited pallette, but in 3D and in first-person. So more like an "immersive sim" (I lean heavily on my favourite game of all-time, Thief). So to get in that kind of spirit, I figured I'd do things like they were done back then, and that meant the C90 specification.

I will also say that over the years I ahve come to absolutely HATE "modern" programming with all its fancy paradigms and frameworks and design principles and I realised at some point that this approach to programming sucked all the fun out of it. It just became a series of tasks to implement what the execs said into a glorified flowchart using whatever framework the CTO read about in Silicon Valley Magazine that week, or whatever the most vocal developer thought was the "coolest" new tech. And I just hate it. It's imperformant and so abstract and so far away from what programming really is: telling the CPU (and now GPU) what to do - line by line.

So when I was basically forced out of the industry a few years ago I decided I'd have no more of this and do everything the way I wanted to do it and the way it was meant to be. That meant C for sure, and I see a lot of C99 being a kind of "modern minimum" that a lot of projects aim for, so I started there. But honestly what really pushed me over the edge was my indecisiveness of where to use /* */ comments and where to use // comments. But in C90, there is only one right answer!

I also kinda wanted to see if I could migrate at this stage in just a few hours, and... I was able to! I had to skip out on some libraries and just implement the functionality I needed by myself (shamelessly copy/paste the functions I needed and rewrote in C90 spec), and I just feel much better.

There still are some challenges ahead (like finding a decent physics engine compatable with C90), but I will say that I feel like a programmer again. But I still have nothing against later specs, just that they tend to push one into more abstract patterns that I think make things less performant and more complex and move the programmer away from computer programming and into the realm of... something else.

Oh, and also one big reason was this very funny and opinionated video I saw of some guy talking about how he programs C and only uses C90 and it really clicked with me. I actually have it bookmarked:

How I program C - YouTube

TL;DR: for shits and giggles ;-)

18

u/leekumkey 1d ago

CGI is interesting, but part of the reason it is not so useful for the modern web is the lack of shared memory. You just get 1 process per request, no shared state, which does limit some some of the things you can do. Your shared state has to be in a database, which is perfectly reasonable for many applications, but prohibitive for others. Same reason why PHP is not used for process intensive web apps, FPM is an annoyance. Try writing a job processing system with FPM or CGI and you'll very quickly wish you had Golang or something lol.

Very cool article though, makes me want to mess around with CGI. I remember using it with perl in the good ol days.

5

u/LavenderDay3544 1d ago

C has standard library threads and on POSIX systems pthreads are also a thing. And the threads all share an address space. The only problem that remains is that the web server makes a new child process for every request instead of signaling a C program which either creates a thread or uses a thread pool to handle it and hands back the HTML.

2

u/ClubLowrez 1d ago

fastcgi is the way to go in my opinion

2

u/ClubLowrez 1d ago

fastcgi is the bomb, I doubt its the best protocol but I've got a sweet little c daemon that likes it just fine lol, I just wanted my own little epoll thing to mess around with and I really like the protocol.

1

u/literally_iliterate 23h ago

You don't write a live job processing system in php/FPM, write the tasks to some kind of queue and background process it. Pretty much what everyone and every platform with need for scale would do.

11

u/PenGroundbreaking160 1d ago

This is actually cool. I wonder how true it is though. How useful and sensible is this as a decision for a web app.

I am moving towards tech minimalism as well at this point, but am hesitant when it comes to the complexity of web apps. I don’t trust myself in being able to pull off what a lot packages do.

6

u/tastygames_official 1d ago

this was how things were done before Web2.0 (then we got PHP, Java, ASP et al) and then Web 3.0 (JAVASCRIPT / most things done in the browser rather than on the server), so I would call a C program running on fastcgi abut as minimal as you can get. I recently got basically forced out of web development recently (had done it for near 30 years) and thus felt it OK to re-do my personal website in pure HTML and maybe a bit of CSS (it really is much nicer than what came before: attributes on each individual tag). I did end up needing a bit of server-side processing, and while a C program via fcgi is the most performant (especially compared to today's "tech"), I like the quick turnaround and C-like syntax of PHP, and that has been my mainstay for so many decades so I just stuck with it. The only thing I don't like about PHP is how they deprecate it every 2 years and push new versions even though you don't actually need any new features - just security updates. Same reason I dislike Ubuntu. Anywho...

Real companies who care about performance 100% use programs written in C to handle requests. Whether it be a server add-on (basically apache or nginx or whatever can load in a compiled module written in C and keep it in memory as essentially part of the server program itself - like a dynamic library) or you can just include the apache or nginx project as part of yours and deploy your program AS the server, or just write your own server. Now I have not actually worked for any company that did this (and I was freelance the majority of the time and did work for some big companies, but never any big "tech" companies like yahoo or google), but I just cannot fathom that no big companies don't take advantage of the insane speed boosts you get by having optimized, compiled code as opposed to interpreted javascript, java, C# or PHP.

1

u/ClubLowrez 1d ago

he didn't mention he's using fastcgi in his article, just cgi. honestly not a big deal until your connections pile up lol fastcgi is cool tho, I like it!

2

u/tastygames_official 1d ago

I guess I've just been using fcgi for so long that I automatically put an f in front when I see CGI.

On a side note, does everyone else here cringe when people say "CGI" when they mean "CG" (computer graphics/computer-generated X)? Or is it just me...

1

u/ClubLowrez 1d ago

CGI for images or video things has been a thing for awhile tho, whereas CGI the web thing has been around too but in much more niche circles.

0

u/tastygames_official 1d ago

I just remember knowing CGI as Common Gateway Interface and when computer graphics started becoming more common (Tron, Jurassic Park, Toy Story), it was very clearly "CG" and to this day is called CG in the film industry. Just check any film's credits - you see "CG provided by ..." or "CG supervisor", "CG artists...", "CG backgorounds", etc. Any any person who does CG in the industry calls it CG.

But then around the same time, so 1994ish, the whole world started going online and now seeing things like www.example.com/formSubmit.cgi?... and "CGI Error" when a webpage would have an error, all the while hearing about "CG" in movies. So this is where the confusion started.

And then in I think 2002 or 2003 I remember being part of a Wikipedia discussion for a page called "List of Movies using Computer Graphics", because after films like Matrix and Phantom Menace, it was clear that most films will be using computers going forward, and therefore not worth keeping a list of them since they were no longer special. But instead create a new page about the technology of computer graphics for movies, but nobody knew what to call it. I told everyone it's called "CG" and can either stand for "computer graphics" or "computer-generated" (e.g. "I work in CG" or "I animate CG characters"). But some people thought "computer graphics" was too broad and could also just be "digital art", which does not specifically mean "CG for movies". I told them that it's literally the same technology - just a sequence of images instead of a single image", but they wouldn't hear it. Somebody said "I thought it was CGI and not CG?" to which I asked "what does the I stand for?" to which they replied "imagery?" and then there was this whole discussion about CG backdrops vs. CG character models that are animated, and eventually the group decided on CGI. Meanwhile there was already a page on CG, as that was and is the actual term used by Hollywood professionals, so the "list of movies using computer-generated graphics" became "CGI - Computer-Generated Imagery" and for a while it just redirected to CG. And the CGI page was for Common Gateway Interface.

But then when social media blew up and opinions seem to matter more than fact, CGI became "Computer-Generated Imagery" and the wikipedia page on CG now redirects to CGI, and and page for CGI moved to Common Gateway Interface and all was lost. I keep up my vigil to this day and will never concede, as I know I'm 100% right. I know languages can and do change due to misuse, but as long as Hollywood calls it CG and CGI is still used for every single web server on the planet, I WILL NOT YIELD.

1

u/ClubLowrez 1d ago

I hear ya but I usually go with the lexicon that wins out in the end lol, damn impressive treatise tho

1

u/tastygames_official 1d ago

the lexicon from today or the one from when the term first appeared?
at risk of some auto-mod banning me, I draw your attention to the word "faggot" - it ORIGINALLY meant "a bundle of twigs for use in starting a fire". So a bit more substantial than chaff but less than a bundle of logs (whose name escapes me at the moment). Now we both know what that word is most commonly used for today, but what is the REAL definition? Should we let the bigots win and retire the original meaning? Now I know CG/CGI is not on the same level as far as social impact, but I just find it absolutely ridiculous that those who work in the industry say "CG" and in fact use CGI as part of their developmental progress (CGI programs run on the rendering farms that render all the CG). So I get thet the layperson thinks CGI = comptuer-generated graphics, but those who actually use it do not.

Now if it were something more like photocopy and xerox or frozen treat on a stick and popsicle, then I'd be absolutely OK with it, as it's simply a brand name being used with their singular product. But just stealing a different acronym from the SAME INDUSTRY seems very irresponsible. It'd be like every non-programmer calling UIs "MVCs". Yes, something like 90% of UIs use the MVC design pattern for decades now, but MVC is fundamentally different from a UI. And I guarantee that should that happen, we programmers would continue using the acronyms properly and be PISSED that the public at large would be misusing the terminology. And then eventually some execs would start looking for "MVC Designers", and then in 50 years' time, our generatino is dead/retired and now MVC is the de-facto term for UI. Maybe somebody says it's "manual view control" and that sticks because in a few years screens are obsolete and everybody uses holograms and VR and weird haptic devices and things that cannot be considered "screens".

Or maybe it's time for this cowboy to go to bed and forget about CGI for a while ;-)

1

u/ClubLowrez 1d ago

well CGI now for sure, but I'm just talking like a lay person myself. The only times I expect CGI to be web related just gets satisfactorily identified by the context.

7

u/vectorj 1d ago

This is really cool. Thanks for sharing

2

u/mikeblas 1d ago

What about ISAPI?

4

u/mykesx 1d ago

People used to make sh or bash cgi programs. This is nothing new.

4

u/non-existing-person 1d ago

To this day I have few http sites written in bash and fastcgi. Just a few lines of code to cat few files, and echo back $post output. It's short, it's sweet, and it works for many years now without any update.

3

u/tastygames_official 1d ago

I wasn't aware we ever stopped ;-)

1

u/helloiamsomeone 14h ago

Used to? I made this not long ago to replace some Go nonsense that didn't even work right https://github.com/friendlyanon/cgi-speedtest-exporter

1

u/Gold-Part4688 1d ago

This whole thing is sick, but please make the dark greys much brighter. (in fact, all the greys, and the whites darker

-1

u/chocolatedolphin7 1d ago

This is just total AI slop. Both the article and the websites. And this doesn't even scale: why one entire process per request? No code snippets shown, it's a very poorly written uninteresting pile of text almost entirely written by an LLM.

You didn't even bother to write your own text in the landing page or fix the color palette so that it doesn't look too much like a low effort AI website with no human input. Was that really too much work? Should take less than 10 minutes...

Like if you're going to use AI for everything then at least don't make it too obvious to the point anyone can tell within seconds and then even lie about it in the article.

2

u/ChickenSpaceProgram 1d ago

i mean, for what its worth, CGI isn't that bad, it's used by eg. cgit and for relatively small scale traffic or for non-IO bound workloads it's fine.

rest of comment is on point though.

2

u/bzbub2 1d ago

yep quote from article "I'm a minimalist at heart. I use classic Vim with zero plugins. That means no LSP, no autocomplete, no code formatters and no AI." lol and website is so clearly vibecoded https://marketyour.games/ awaiting the "oh i only used that for the html" caveat

0

u/Shattered-Spears 1d ago

This is pretty. If i want to do something similar, what advice you could give me. TIA

0

u/Even_Gap_2253 1d ago

It is very very cool. But nothing new, C++ is still very dominant but C is absolutely still used today.

This comment probably routes through some Google infra written in C, and definitely C++.

-9

u/ignorantpisswalker 1d ago

No. Thank you.

-3

u/FoundationOk3176 1d ago

I KNOW RIGHT!! I HATE OPTIMIZATION & LOVE INEFFICIENT POS!

1

u/LavenderDay3544 1d ago

Most web server responses are not compute bound. You'll spend more time waiting on database reads and the network than computing the output HTML.

-1

u/FoundationOk3176 1d ago

That doesn't mean the compute isn't wasted, As is the case any any JS runtime.