Top
Best
New

Posted by prophittcorey 6 hours ago

A Modern Web App in C(prophitt.me)
314 points | 130 commentspage 2
hughw 4 hours ago|
I disagree with the first sentence. In 199x the first people I knew making web pages dropped perl scripts, not C programs, into cgi-bin.
fleeno 12 minutes ago||
My first CGI programs around that time were in bash! I wrote so much useless but fun things back then.
forinti 1 hour ago|||
I learnt Perl because I had to make a page with a form that would then be turned into Postscript and printed. My only options were C, which I knew well, and Perl, which I had never seen before. So I got to learning Perl. Doing it in C just seemed like too much work for the task.
nkozyra 4 hours ago|||
May have pre-dated you by a bit. Perl definitely took foot not long after but the earliest cgi-bin days were dominated by C before people got tired of fighting compilation for web apps.

IIRC the spec itself says it was built for C applications.

mod-perl was a godsend, of course.

minaguib 4 hours ago|||
Perl's CGI.pm was updated as recently as 5 months ago :)

https://metacpan.org/pod/CGI

icedchai 1 hour ago|||
Same. My first "web app" was a Perl CGI that saved data to mSQL (not MySQL), probably early 1995.
wainstead 53 minutes ago||
Same!
rjrjrjrj 3 hours ago|||
I started using Perl in 1996.

But the very first CGI scripts I wrote were in C, late 95 or early 96. Apache on Slackware Linux.

layer8 2 hours ago|||
I still have such Perl scripts deployed, though using FastCGI.
PaulDavisThe1st 3 hours ago|||
We did the CGI-bin in C thing at amzn in 1994.
user0120397125 3 hours ago||
[dead]
jjuel 3 hours ago||
I just really wish it had some examples of code in there to get a better idea of what is really going on. It sounds interesting.
tomcam 1 hour ago|
They use this framework: https://learnbchs.org

Their example is:

    #include <err.h> /* err(3) */
    #include <stdlib.h> /* EXIT_xxxx */
    #include <stdio.h> /* puts(3) */
    #include <unistd.h> /* pledge(2) */ 
    int main(void)
    {
      if (pledge("stdio", NULL) == -1) 
        err(EXIT_FAILURE, "pledge");
        puts("Status: 200 OK\r");
        puts("Content-Type: text/html\r");
        puts("\r");
        puts("Hello, world!\n");
        return EXIT_SUCCESS;
      }
macleginn 3 hours ago||
"A modern web application typically puts a web server like nginx in front and reverse-proxies to an application server written in another language like Ruby, Python, Go, or Rust. That always felt a bit strange to me: you essentially have two web servers, and one of them is also responsible for your business logic." -- Is the fundamental difference here that the business-logic binary is not always running but gets spun out with each request? This looks like a relatively minor thing, to be honest.
prophittcorey 1 hour ago||
I meant to express the layers of abstraction feel strange to me in a typical setup.

A typical setup today looks like:

nginx -> puma/unicorn/gunicorn/etc -> your code

With cgi that middle layer is gone. It's just:

nginx -> your code

The layer reduction is nice when it comes to things like debugging. You don't have to guess if the middle server is removing a header, failing to forward a header, etc.

It's obvious who does that with cgi.

bijowo1676 4 hours ago||
Author described good ole PHP 3.

Early PHP (3 and 4) were the thin layer over the C libraries and were pretty fast and easy. it came about right when CGI was deprecated and it solved CGI's problems

mc007 3 hours ago||
came back to C/C++ for same reasons, no supply chain trouble, fits in my head, runs forever, ... here a playground for win32, using partially webview2, all batteries included, openai client , tts, stt, vpl, images, audio, video, ... comes with webview2 apps, settings (and encryption), config, rest server, ipc, testing, CLI11, postgres, installer, doc gen, modbus, OpenCV, daemon, computer use, agent memory, image tools, mcp server/client, experimental flow editor - what a fun and exciting times :) https://github.com/polymech-info/win32-boilerplate
codazoda 1 hour ago||
I agree on "fits in my head". I haven't used C++ but I used to love ANSI C. I had a reference manual for it that was something like 30 pages and basically covered the entire language. There's something magical about the compact size of regular C.
rietta 1 hour ago||
It's been a long time since I have tried to do Windows work in C/C++. Is this Win32 the actual Win32 API of yesteryear or something else. I thought CMoke was a Unix tooling.
criddell 6 minutes ago||
Microsoft added CMake support to Visual Studio in 2017.
fmo91 5 hours ago||
It's not very clear to me why the author has chosen C in the first place. I appreciate the effort of sharing the experience with us, anyway.
prophittcorey 5 hours ago||
I didn't want to state why C and not "X" because the comments would have devolved into an all out language war.
kanbankaren 4 hours ago|||
As someone who works on C compilers, I find the obession with C language insane.

For any greenfield project, pick up C++ in lieu of C if you want a C language.

There is no runtime penalty for using C++ if you stick to plain structs and don't use virtual functions, but you get better static type safety, lvalue references, namespaces, etc. even if you don't use the OO features.

Even for virtual functions, the overhead is minimal that it is just noise.

C compilers themselves are written in C++ for the last 15 years. Oh, the irony!

glouwbug 2 hours ago|||
Because in practice it’s really hard to not succumb to C++ feature creep. You start with good intentions and limit yourself to namespaces and structs, but then the “this will benefit from a vector, or this from a map” appears, then you realize you’d like a templated use case, and a single raw pointer or resource handle from a call to something like fopen() suddenly is not releasing its resource on a linked list pop and now you’re writing move semantics on top of copy semantics because a destructor declaration silently enforces copies on a std::move without move semantics. You reach for a unique pointer and, and all of a sudden, you have most of modern C++ without a single virtual call.

So people obsess over C because you’re hard limited by the upper feature bound. And that’s not so bad because C gets feature updates too - microscopic but effective feature updates; see C29 for the poor man’s RAII: defer

ricardobeat 1 hour ago||
C3 lang gives you defer + a lot of significant ergonomic improvements right now: https://c3-lang.org
glouwbug 6 minutes ago|||
C3 is very good
feelamee 38 minutes ago|||
and what it does take away? most "better C" languages basically looks like C++ (except zig, maybe). Nobody write something real in them, so nobody knows about their pitfalls.
kevin_thibedeau 3 hours ago||||
T> here is no runtime penalty for using C++

Large portions of the standard lib depend on heap allocations. C is a much safer target for environments with limited resources.

prophittcorey 4 hours ago||||
I generally like "C-style C++", but I find it difficult to draw the line between C and C++. It's too easy to keep reaching into C++ for "just one more thing."

That may be a me problem.

PaulDavisThe1st 3 hours ago||||
It's what we did at amzn in 1994 ...
bigbuppo 1 hour ago|||
Wasn't amazon running on tandem back then?
PaulDavisThe1st 1 hour ago||
In 1994, amzn ran on a single solaris workstation. Same at the beginning of 1995. I can't comment after that.
ricardobeat 1 hour ago|||
There is a huge mental overhead penalty...
christoff12 5 hours ago|||
Fair enough. But I am also curious if you'd like to share your rationale.
Kevin_Flynn 5 hours ago|||
I can never figure out why everything isn't in C. :|
ricardobeat 1 hour ago||
Is it because you segfault before reaching the answer?
layer8 2 hours ago|||
The underlying libraries you use tend to be in C anyway, so it simplifies things to just use them with their native interface, and not having to add another language runtime and its idiosyncrasies.
uecker 5 hours ago|||
Yesterday or so there was a post about using Zig. When I read it I thought that using C would work equally well.
christoff12 5 hours ago||
Yeah, I thought it was interesting that both of these[0] were on the front page.

[0] - the other one: https://news.ycombinator.com/item?id=50051573

EGreg 5 hours ago||
I'm old enough to remember when the OKCupid guys wrote their own webserver in C++!

I took the concept of "slots" in requests, from it: https://github.com/okws/okws

rietta 3 hours ago||
There is Boost in C++. It is very mature and honestly modern C++ has a lot of extremely powerful stdlib functionality and classes that will still be orders of magnitude more performant that the vast majority of the web app languages we use today. See https://www.boost.org/
Surac 3 hours ago||
i also do that way, but i also have written my own webserver so i have no cgi interface. it serves files and provides "special" sites for the application written in. I use html and css as a frontend. nealy no js code in the html. Things i hate: - applicatin status handling - mixing html, css, js and c in one file - having no web "widgets" - having to make them by myself
codazoda 5 hours ago||
It hasn't been touched in a long time, maybe that's because it's "complete", but here's a quick CGI library:

https://github.com/codazoda/cgi-lib

hollowonepl 4 hours ago|
I was not thinking about vanilla C and CGI but I wrote my own modern web api server app in C++ (std+boost only) and I found it extremely pleasant experience. very reliable and little supply chain/dependency hell problematic too.

It’s for intranet business app so although it’s not yet production ready yet, definitively winning against many other modern approaches in web development in my consideration, and quite refreshing POC project.

whizzter 1 hour ago|
Yeah, shuddering at trying to manage memory vs static strings (with OOB risks) in plain C, modern C++ does give so much.

I'm actually also rewriting a header-only server (std c++ and sockets) I wrote a few years back to take advantage of C++26 reflections and similar to give it something similar in capability to AspNetCore+EFCore.

More comments...