C performance for web applications? Say hi to Vely
Jan 4th, 2023 by Sergio Mijatovic
Image copyright (c) Sergio Mijatovic 2023
C performance for web applications? Say hi to Vely
Vely is a framework for C programmers. It works by precompiling simple statements into C.
It's Free Open Source Software (FOSS), of course.
I created Vely for rapid development of back-end applications (especially web) that are safer than pure C, while keeping C's powers of high performance.
To get started, here is a Hello World application. It is super simple. You'll get a web page from your own FastCGI application server in minutes!
Install Vely
Go to https://vely.dev/pkg/ to install Vely using standard packagers like apt, dnf, zypper or pacman, or you can download the source and build it yourself. It's pretty basic.
Get all shiny in vim
It's nice to have everything highlighted and color-coded in vim editor. So do this to get it going forward:
vv -m
Hello World
Create a directory where your code will reside:
mkdir hello
cd hello
Create the application using vf:
sudo vf -i -u $(whoami) helloworld
Create hello.vely file:
vi hello.vely
and copy this to it:
#include "vely.h"
void hello()
{
out-header default
@Hello World!
}
Make the application with vv:
vv -q
That's it. Now you can start your FastCGI application server:
vf helloworld
You can connect to it from Apache, Nginx or any other web server. Before doing that, you can test your server from command line, by simulating a web server reverse-proxy request:
export REQUEST_METHOD=GET
export SCRIPT_NAME="/helloworld"
export PATH_INFO=""
export QUERY_STRING="req=hello"
cgi-fcgi -connect /var/lib/vv/helloworld/sock/sock /
Unsurprisingly you'll get:
Content-type: text/html; charset=utf-8
Cache-Control: max-age=0, no-cache
Pragma: no-cache
Status: 200 OK
Hello World!
You've got a response a web client (like browser) can understand!
See it from browser
Now, you can try Apache or Nginx, here you will do Nginx (if you got Apache running, stop it for a moment). Needless to say, you got to have Nginx installed and running. Typically, that's done for Debian systems like:
sudo apt install nginx
or for Fedora systems like:
sudo dnf install nginx
First, you need to open Nginx configuration file, typically for Ubuntu:
sudo vi /etc/nginx/sites-enabled/default
though for others like RedHat it may be at:
sudo vi /etc/nginx/nginx.conf
and find a server{} block (it has to be there!). Then within it, copy this in a new line after
"server_name" directive:
location /helloworld {
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:///var/lib/vv/helloworld/sock/sock;
}
It will tell Nginx where is your FastCGI application server listening, in this case it's a socket file above, but it can be a TCP port too. Restart Nginx:
sudo systemctl restart nginx
Assuming you're doing this on your local computer, go to browser and copy this:
http://127.0.0.1/helloworld?req=hello
Voila, Hello World! Of course, change
"127.0.0.1" to your server name or IP if you're not local. If you don't have a browser handy, use cURL to test from command line:
curl -s "http://127.0.0.1/helloworld?req=hello"
Customize your Hello World
Want to tinker with it a bit? Easy. Here, you'll first create a string that's a bit more involved, then output it. Edit hello.vely, and copy this:
#include "vely.h"
void hello()
{
out-header default
num x = 3;
char *y = "have a nice day";
write-string define mystr
@Hello World <<p-num x>> times and <<p-out y>>!
end-write-string notrim
p-out mystr
}
Make it again:
vv -q
Your application server will automatically reload with a new executable, so you can try it again right away, the result is:
Hello World 3 times and have a nice day!
Command line
The same code that runs as an application server also runs from the command line. So do this to run it like that:
export REQUEST_METHOD=GET
export SCRIPT_NAME="/helloworld"
export PATH_INFO=""
export QUERY_STRING="req=hello"
export VV_SILENT_HEADER=yes
/var/lib/vv/bld/helloworld/helloworld
You will get same output but without headers, and if you skip setting VV_SILENT_HEADER, you'll get headers too.
What else?
You guessed it right, you can do more than Hello World in Vely. In fact, quite a bit more.
First off, Vely's got automatic memory allocation and garbage collector for all its statements. That makes it safer because it helps avoid memory issues and leaks.
You can easily access MariaDB/mySQL, PostgreSQL and SQLite databases, create and access web services including upload and download files, deal with web stuff like GET or POST or cookies. You can write complex strings safely and easily. You can encrypt, encode, do regex search and replace, use files, execute programs.
Use Vely for just about any back-end server application or program you can think of.
There's a bunch of examples and crisp-clean documentation to help you get productive quick with Vely.