r/cpp • u/matthieugarrigues • Mar 20 '16
Release of the Silicon C++14 Web Framework 0.1
http://siliconframework.org/blog/silicon-0-1.html1
Mar 22 '16
So this is gonna sound super stupid but... Is this C++ for writing an actual RESTful HTTP server? Or is it C++ for writing code that interacts with a RESTful HTTP server?
I really hope it's the first one...
1
u/matthieugarrigues Mar 22 '16
It is both.
First, you have everything to build a RESTfull HTTP server:
Parameters in the url
POST and GET parameters
There is actually a meta SQL REST api here: https://github.com/matt-42/silicon/blob/master/silicon/sql_rest.hh#L57
It is also the second one because Silicon provides a generic C++ HTTP client. You can see how it is used to test the sql_rest api here: https://github.com/matt-42/silicon/blob/master/tests/rest.cc
Note that libcurl_json_client generates an object with one method matching each API call, this is why it takes the api as argument. This is done with compile time static introspection of the api. The implementation is here: https://github.com/matt-42/silicon/blob/master/silicon/clients/libcurl_client.hh
1
u/cguttesen Mar 22 '16 edited Mar 22 '16
Hi @matthieugarrigues. Thank you for your nice framework. I build the hello world on os x (el capitan and xcode 7.3). I followed your guide. In the second sentence, below the source code at http://siliconframework.org/docs/hello_world.html, I think you mean 'To generate their definition' rather than 'To generate there definition'. there->their.
1
u/matthieugarrigues Mar 23 '16
Good to know that it worked on os x too :) I just fixed the typo. Thanks
1
Mar 21 '16
There isn't a detailed explanation on what:
GET / _procedure1 = ...
means or how it works. Can somebody explain? I'm not an expert in C++, and I can understand the effects of that expression, I just don't understand how C++ interprets that (and reading the source code isn't helping).
1
u/oh-just-another-guy Mar 21 '16
Macros I think.
2
u/matthieugarrigues Mar 21 '16
No macros are used in silicon. GET and _procedure1 are two variables and the operator / build an object representing the HTTP route.
1
u/oh-just-another-guy Mar 21 '16
So they overloaded the / operator? That's pretty interesting.
2
u/matthieugarrigues Mar 21 '16
Yes. I overloaded the /, *, [] and = operator on some of the silicon internal types in order to help the declaration of http routes.
1
Mar 21 '16
Thank you. Where can I find that operator overloading in the source?
2
u/matthieugarrigues Mar 21 '16
The operator overload is in the iod library. It overloads all operators for its internal variables called "symbols" (the variables starting with _) so that , for example, when you write _s1 * _s2 it build an object mult_exp with _s1 as lhs member and _s2 as rhs member. This "grammar" is defined here: https://github.com/matt-42/iod/blob/master/iod/grammar.hh
Then, the make_http_route function decodes this grammar to build the http_route object, its implementation is here: https://github.com/matt-42/silicon/blob/master/silicon/http_route.hh#L270
1
1
0
Mar 21 '16
[deleted]
1
u/matthieugarrigues Mar 21 '16 edited Mar 21 '16
I find it pretty useful to augment the expressiveness of the language for a specific domain. But I agree that when used abusively it can be hard to understand. In the case of silicon I find it pretty minimal and I did not find any problem with it. Note that I wrote the library so I may be biased.
1
Mar 24 '16
I think this is a really interesting library. How does it compare to libcurl for client-side HTTP?
Edit: feature-wise, not interface-wise. I realize that libcurl is ancient.
1
u/matthieugarrigues Mar 28 '16
The library actually wraps libcurl to provide an high level HTTP client. So feature-wise it provides less than libcurl. One the other side Its interface allows you to call external APIs as if they were plain C++ functions. It relies on meta programming to generate an object with one member per API call, serialize the parameters and deserialize the server response. You can see it in action here:
https://github.com/matt-42/silicon/blob/master/examples/blog_test.cc
1
Mar 28 '16
auto r6 = c.http_post.post.create(_title = "My Title", _body = "My content.");Do the static objects like _title (or whatever it is) make this library exclusive to single-threaded contexts?
Edit: I guess there isn't a way around the single-thread limitation for a libcurl wrapper, because libcurl has all that global stuff.
1
u/matthieugarrigues Mar 29 '16
These static objects are const and do not prevent you from using them in different threads. You cannot use an instance of the libcurl client in different threads at the same time. But you can use different instances in different threads without problems.
1
Mar 30 '16
Ok, thanks. It's been a while since I've used libcurl... I'll give Silicon a shot the next time I need to do HTTP.
1
u/RotsiserMho C++20 Desktop app developer Mar 21 '16
Which platforms is this supported on?