Web framework for C programming language
RimStone has an "extended" mode, which allows for C code to be used directly. Normally, this is not allowed, but if you use extended-mode statement in your .rim file, then you can call C code from it.
In extended mode, RimStone is effectively a web framework for C programming language; read this for more about implications on memory safety.
Here's the example of using RimStone as a web framework for C. In this case it's a simple C function to calculate the factorial of a a number. Then we'll expose this functionality in an equally simple web application.
Create the RimStone application
mkdir -p c-for-web
cd c-for-web
Copied!
Create "fact" application ("-k"):
rim -k fact
Copied!
The following file "calc-fact.rim" has the web service which will interact with web clients (such as browsers); it will also call the C function we'll create a bit further below:
cat << 'EOF' > calc-fact.rim
extended-mode
%% /calc-fact public
get-param n type number
set-number result
call-extended factorial(n, &result)
@Factorial of <<print-out n>> is <<print-out result>>
%%
EOF
Copied!
Add C code to RimStone application
Next is C file "factorial.c". This is the C function we're calling from RimStone code above:
cat << 'EOF' > factorial.c
#include "rim.h"
void factorial(rim_num f, rim_num *res)
{
*res = 1;
rim_num i;
for (i = 2; i <= f; i++) {
*res *= i;
}
}
EOF
Copied!
And an include file "factorial.h" to declare any function(s) we will use in RimStone code:
cat << 'EOF' > factorial.h
void factorial(rim_num f, rim_num *res);
EOF
Copied!
Compile and start the server
RimStone will pick up all .rim as well as all .c and .h file and make them into an application:
rim -q
Copied!
Start the server:
mrim fact
Copied!
Test the server from command line
You can test this web application from command line (note --service flag!). This is useful because you can develop web applications just from command line:
rim -r --req=/calc-fact/n=12 --exec --service
Copied!
The result is:
Content-Type: text/html;charset=utf-8
Cache-Control: max-age=0, no-cache
Pragma: no-cache
Status: 200 OK
Factorial of 12 is 479001600
Copied!
This is the exact data that would be sent to an actual web client.
Test the server with web browser
Let's test on an actual web server. In this case we'll use Nginx. Also, we'll do it locally on your computer, since not everyone has a server on the web.
First, edit Nginx config file, which for Ubuntu/Debian is at:
/etc/nginx/sites-enabled/default
Copied!
and on Fedora/RedHat is at:
/etc/nginx/nginx.conf
Copied!
Note you might have to use "sudo" to edit these files as they're generally not writable by ordinary users.
Find the "server" section, and add this inside it (replace "your-user" with your OS user name):
location /fact/ { include /etc/nginx/fastcgi_params; fastcgi_pass unix:///home/your-user/.rimstone/apps/fact/sock/.sock; }
Copied!
This will connect Nginx with the "fact" server you started above. Any requests with a URL path that start with "/fact/" will be delivered to your server, and the reply will go in reverse, ultimately being delivered to the client (such as web browser).
Restart Nginx server for this to take effect:
sudo systemctl restart nginx
Copied!
Try it from web browser with this URL:
http://127.0.0.1/fact/calc-fact/n=8
Copied!
If your Nginx runs on another port, specify it, for instance if it runs on port 81, use "127.0.0.1:81" instead.
The result is:
Factorial of 8 is 40320
Copied!
Of course, if you have a server on the web, just substitute "127.0.0.1" for your server's web address.
Copyright (c) 2019-2025 Gliim LLC. All contents on this web site is "AS IS" without warranties or guarantees of any kind.