by Negitivefrags · 9y ago · 0 comments
http::controller mypage
{
//Browsing to http://server/mypage/test?some_str=blah¶m=5 calls this
http::response test( const std::string& some_str, const unsigned param )
{
return web::response{ 200, "You did awesome stuff" };
}
//This method can only be called with HTTP POST
[[POST]] http::response save_some_data( const std::string& blah )
{
return 200;
}
};
int main()
{
http::server server( "localhost", 80 );
server.add_route( "mypage", std::make_unique< mypage >() );
server.run();
}
It's all the magic of a modern web framework but in a fast language.Due to web assembly you can easily have your server and client side scripting be done in the same language, with the same libraries which is also huge bonus.
I honestly can't think of a better stack.
I very much doubt that. Do you have anything to back up that statement?
It also doesn't require any "meta" level programming, just plain Haskell. And Haskell is blazing fast, not far behind C++ (especially when using high level C++ APIs like std::string)
Granted most things don't /need/ the raw throughput potential of C++, but if parts of your code really does need it then it tends to be easier to just make the whole thing in C++ than deal with bindings to other languages.
ALSO: Your example is much slower than the same server written in Node.js, haha. Because the language doesn't support evented programming. The language may be fast, but the paradigm of blocking while waiting for I/O is slow!
Back in 2001 I was working on what was at the time the fastest webserver in the world: Zeus. It was written in C++ and built around select(), which gave it effectively evented programming. One process per core. Primarily for serving static content, although there was a fastcgi interface and, god help you, IBM Websphere integration.
There were a few quirks to the codebase. No STL; instead our own containers and length-based string classes. There was also a "stringrange" type class, which referred to a range of a longer string. So it parsed in the entire HTTP header as an octet array, then created objects to refer to individual headers. Since the input header was immutable this worked beautifully. Considerable effort was put into avoiding unnecessary copies.
It also had at least a dozen target platforms; Linux, SunOS, IRIX, and architectures from ARM to Itanium. I'm not sure on how many of those we ever sold a license.
Yes, sure you can do the web server and even middleware as async, but there are many services (redis, memcached etc.) and having async modules for them all is the key.
there are plenty of event libraries for C++, many predating Node.js (which is written in C++ btw) by decades.
There is no reason why the op example can't be implemented on top of such a library.
Unless your was sarcasm, in which case Poe's law strikes again.
Uhh, how do you think Node.js is implemented?
Look, even PHP has frameworks to do evented programming: Kraken, amphp, ReactPHP. But it lacks an ecosystem of modules that actually do things in an async way and work with the same framework.
It's the ecosystem of modules and the ethos that matters.
[1]: https://msdn.microsoft.com/en-us/library/dd492427.aspx [2]: https://github.com/libuv/libuv
The main problem is that web apps have a huge surface of attack, a huge part of any web application is handling untrusted user input. For this reason you're taking significant risks by using a memory-unsafe language. Also web services are rarely CPU-bound, so the added performance matters less.
That's not actually true; not all web frameworks work by having pages that parse query params on every navigation. Callback based frameworks like Seaside let you write apps where form input is the only attack vector and messing with URL's will only make you lose your session. That pretty much removes the largest attack vector in most web apps.
If the framework is written in a safer language then it's not much different than if you write a web application in, say, Django where you call native C and C++ modules. That's very common.
More seriously Node.js has some pretty major performance problems, like the fact that it's single threaded. You'll make excellent use of one core, but that's about it. It looks great on I/O bound request/second benchmarks, but as soon as your code needs to do meaningful work it doesn't scale anymore. The solution to that is to spawn multiple node instances to make use of the actual machine resources, but now you're on your own and why did you bother with node.js then in the first place?
And none of that snippet you are replying to even blocks, so... what?