19.0.0 released Nov 08, 2023
Develop web applications in C programming language


Vely is a framework and language that generates C code underneath. Because it has lots of functionality, you may not need to write much C code, if any. However, if you need to, you can write as much C code as necessary. The example here demonstrates this.
Your C application
Create new "c-app" application first, in a new directory (you can name it anything you like):
mkdir -p vely_c
cd vely_c

The vf command is a Vely program manager and it will create a new application (see how-vely-works) named "c-app":
sudo vf -i -u $(whoami) c-app

To get vim highlighting of Vely syntax:
vv -m

Create a source code file "using_c.vely":
vi using_c.vely

and copy and paste this to it:
#include <math.h>

int factorial(int num);

%% /using-c
    out-header default
    input-param inp
    int res = factorial (atoi(inp)); // call C function
    @Factorial of <<p-out inp>> is <<p-num res>>!
    double sr = sqrt ((double)res); // call standard C's sqrt
    @And its square root is <<p-dbl sr>>!
%%

// C function to calculate factorial of a number 'num'
int factorial(int num)
{
    int res = 1;
    int i;
    for (i = 2; i <= num; i++) {
        res *= i;
    }
    return res;
}

Note that the source file name ("using_c.vely") should match the request name, which is "using-c". If you're using hyphens (which is useful for web applications), just substitute with underscore. The fact that a request is implemented in a file with the same name helps keep your applications neat, tidy and easy to peruse.

The code here is pretty self-explanatory. You'd get an input-parameter "inp", call a C function "factorial()", then get the square root of it. As you go along, you output the results of computation. Included is C's header file "math.h" since you're using math function here to calculate a square root. Just like in any other C program, you'd declare the function up top. The request itself (between "%%" signs) will translate into a C function with the name that's a decorated version of a request name "/using-c", in this case just "using_c()", see request-handler for more.
Make an executable program
Now, make a native executable:
vv -q --lflag=-lm

Note the use of "--lflag" option that lets you specify additional C linker options, in this case you're using the math library ("-lm"). You can also add C flags for compilations (like -D for defines for instance) using "--cflag".
Execute from command line
You can now run your program! Here's how to do it from command line - you'd specify input parameter "inp" to have value of "5":
vv -r --req="/using-c?inp=5" --exec --silent-header

The result:
Factorial of 5 is 120!
And its square root is 10.954451!

That's a success right there! How did this work? Here you're using vv utility to call a program for convenience, but if you omit "--exec" option:
vv -r --req="/using-c?inp=5" --silent-header

here's what you get:
export CONTENT_TYPE=
export CONTENT_LENGTH=
export VV_SILENT_HEADER=yes
export REQUEST_METHOD=GET
export SCRIPT_NAME="/c-app"
export PATH_INFO="/using-c"
export QUERY_STRING="inp=5"
/var/lib/vv/bld/c-app/c-app

This is what's executed with "--exec" option. The above output you can copy and paste to your bash scripts to directly execute your program, which is located at "/var/lib/vv/bld/c-app/c-app". You can see that input parameter "inp" is provided as query string "inp=5". This is all very neat, because this is how web programs work and you can run this program from the web without modifications! That's next.
Run as application server
First, try running your program as an application server. That means a daemon, a resident server process that remains in memory and can serve many requests simultaneously. With Vely, that's a breeze, because it will take care of all the infrastructure (that's why it's a "framework"). Here's how you do that:
vf -w 5 c-app

The above will start 5 application server processes to serve incoming requests (you can also have a dynamic number of processes too, see vf). Testing your server is easy:
vv -r --req="/using-c?inp=5" --exec --server --silent-header

Note the "--server" option. It says to contact a server and execute the same request as before. But now, each of the 5 processes you started is staying resident in memory and serving the incoming requests. This way, your server can serve a large number of concurrent requests in parallel. Because each process stays alive, you get great performance.

Again, you can see what's going on behind scenes by omitting "--exec":
vv -r --req="/using-c?inp=5" --server --silent-header

The result being:
export CONTENT_TYPE=
export CONTENT_LENGTH=
export VV_SILENT_HEADER=yes
export REQUEST_METHOD=GET
export SCRIPT_NAME="/c-app"
export PATH_INFO="/using-c"
export QUERY_STRING="inp=5"
cgi-fcgi -connect /var/lib/vv/c-app/sock/sock  /c-app

Here a "cgi-fcgi" client program will contact the server you started, get a response, and print it out. You can make your own client application by using FastCGI-API; this way you can do whatever you want with the response, and you can do so in a multi-threaded application, since Vely's FastCGI API is MT-safe.
Run as web application
Of course, your application server will probably serve web requests. Check out connect-apache-unix-socket on how to connect Apache web server to your application server, or the same for Nginx: connect-nginx-unix-socket. FastCGI is supported widely among web servers, so you can use pretty much any web server of your choice.

Here's a brief intro for Nginx:
Setup web server
This shows how to connect your application listening on a Unix socket (started with vf) to Nginx web server.

- Step 1:
You will need to edit the Nginx configuration file. For Ubuntu and similar:
sudo vi /etc/nginx/sites-enabled/default

while on Fedora and other systems it might be at:
sudo vi /etc/nginx/nginx.conf


Add the following in the "server {}" section ("/c-app" is the application path (see request-URL) and "c-app" is your application name):
location /c-app { include /etc/nginx/fastcgi_params; fastcgi_pass  unix:///var/lib/vv/c-app/sock/sock; }

- Step 2:
Finally, restart Nginx:
sudo systemctl restart nginx

Note: you must not have any other URL resource that starts with "/c-app" (such as for example "/c-app.html" or "/c-app_something" etc.) as the web server will attempt to pass them as a reverse proxy request, and they will likely not work. If you need to, you can change the application path to be different from "/c-app", see request-URL.
Access application server from the browser
Use the following URL(s) to access your application server from a client like browser (see request-URL). Use actual IP or web address instead of 127.0.0.1 if different.
# Call your application server to calculate factorial of 5 
http://127.0.0.1/c-app/using-c?inp=5

Note: if your server is on the Internet and it has a firewall, you may need to allow HTTP traffic - see ufw, firewall-cmd etc.

The result is, from the browser:

Vely

Conclusion
In this article you've learned how to build web applications in C programming language. The same code makes a command line program, application server and a web application.
See also
Examples
example-client-API  
example-cookies  
example-create-table  
example-develop-web-applications-in-C-programming-language  
example-distributed-servers  
example-docker  
example-encryption  
example-file-manager  
example-form  
example-hash-server  
example-hello-world  
example-how-to-design-application  
example-how-to-use-regex  
example-json  
example-multitenant-SaaS  
example-postgres-transactions  
examples  
example-sendmail  
example-shopping  
example-stock  
example-uploading-files  
example-using-mariadb-mysql  
example-using-trees-for-in-memory-queries  
example-utility  
example-write-report    
See all
documentation


You are free to copy, redistribute and adapt this web page (even commercially), as long as you give credit and provide a dofollow link back to this page - see full license at CC-BY-4.0. Copyright (c) 2019-2023 Dasoftver LLC. Vely and elephant logo are trademarks of Dasoftver LLC. The software and information on this web site are provided "AS IS" and without any warranties or guarantees of any kind. Icons from table-icons.io copyright Paweł Kuna, licensed under MIT license.