Vely is a software for writing internet and command-line purposes in C. Vely combines excessive efficiency and the low footprint related to C programming with ease of use and improved security paying homage to languages like PHP. It’s free and open supply software program, and licensed beneath GPLv3 and LGPL 3 for libraries, so you’ll be able to even construct business software program with it.
Vely works on main Linux distributions and processor architectures. You can use webservers, similar to Apache, Nginx, or others, and databases similar to MariaDB, PostgreSQL, and SQLite.
You can use Vely for internet purposes, command-line packages, as middleware, database purposes, companies software program, knowledge integration, IoT (Internet of Things), and anyplace else. It’s nicely suited to the cloud, works simply in a container, and, as a consequence of low useful resource necessities, it is also a sensible choice when reminiscence and processing energy are at a premium.
Install Vely
To strive Vely, set up the Apache webserver and MariaDB database. You can use a special webserver and database, and the setup could be related, however on this instance, I take advantage of Apache and MariaDB.
Next, set up Vely. On Linux use a bundle supervisor similar to dnf
or apt
.
Stock tickers undertaking
This instance saves the names of inventory tickers and their costs so you’ll be able to view them in a listing.
Start by creating inventory.v
file, and paste this code into it:
#embrace "vely.h"void inventory() {
out-header default
@<html>
@<physique>
input-param motion
input-param stock_name
input-param stock_price
if (!strcmp (motion, "add")) {
// Add to inventory desk, replace if inventory exists
run-query#add_data@db = "insert into stock (stock_name,
stock_price) values ('%s', '%s') on duplicate key
update stock_price="%s"" : stock_name, stock_price,
stock_price
end-query
error#add_data to outline err
if (strcmp (err, "0")) {
report-error "Cannot update stock price, error [%s]", err
}
@<div>
@Stock value up to date!
@</div>
} else if (!strcmp (motion, "show")) {
// Show inventory names and values
@<desk>
@<tr>
@<td>Stock title</td>
@<td>Stock value</td>
@</tr>
run-query#show_data@db = "select stock_name,
stock_price from stock" output stock_name,
stock_price
@<tr>
@<td>
query-result#show_data, stock_name
@</td>
@<td>
query-result#show_data, stock_price
@</td>
@</tr>
end-query
@</desk>
} else {
@<div>Unrecognized request!</div>
}
@</physique>
@</html>
}
Build the database
For this instance, create a database named dbstock
, owned by person vely
with the password your_password
. These are arbitrary names, and in actual life, you need to use no matter values you need, so long as they’re constant all through your code.
First, log in to the MariaDB database as root and execute this:
CREATE DATABASE IF NOT EXISTS dbstock;
FLUSH privileges;
CREATE USER IF NOT EXISTS vely@localhost IDENTIFIED BY 'your_password';
FLUSH privileges;
GRANT ALL privileges ON dbstock.* TO vely@localhost;
FLUSH privileges;
exit;
Now log in to MariaDB once more and set the present database:
$ mysql -u vely -pyour_password
Now you’ll be able to create the database objects wanted for the applying. You want a inventory
desk within the dbstock
database for this instance.
USE dbstock;
CREATE TABLE IF NOT EXISTS inventory (stock_name VARCHAR(100) PRIMARY KEY, stock_price BIGINT);
Finally, create a database configuration file named db
in order that your utility can log into the database. You should name it db
as a result of that is what the code in inventory.v
makes use of. For occasion:
[...]
run-question#add_data@db = "insert into stock ..."
[...]
The database title is preceded by the @
signal, on this case, @db
, so the title of the database configuration file is db
. As with different values, you’ll be able to title your database configuration file no matter you need, so long as your code is constant.
Here’s the configuration for the db
file:
[client]
person=vely
password=your_password
database=dbstock
The above is a regular MariaDB client options file. Vely makes use of native database connectivity, so you’ll be able to specify any choices a given database permits.
Build the applying
Next, you’ll be able to create your Vely utility. For this instance, you are going to create an online app referred to as stockapp
:
$ sudo vf -i -u $(whoami) stockapp
This creates an utility house beneath the Vely listing (/var/lib/vv
) and performs the required utility setup steps for you.
To construct your utility, use the vv
command:
$ vv -q --db=mariadb:db stockapp
Here’s what every choice means:
-q
builds an utility--db
specifies the database for use (mariadb:db
, as laid out in your configuration file)stockapp
is the applying title
You can truly use any variety of databases and totally different distributors in your utility. This instance is straightforward, although, so that you solely want one database. Vely has many different helpful choices you need to use, however that is enough for now.
Configure internet entry
To entry your utility by way of an online browser or numerous internet purchasers, you must arrange a webserver. It will be Apache, Nginx, or another server that helps FastCGI proxying (most, if not all, webservers and cargo balancers do that). Here, I’ll arrange Apache, however the setup is analogous for different webservers.
The proxy
and proxy_fcgi
modules are put in and enabled by default on the Fedora set up of the Apache internet server, however it’s essential to allow them on Debian-based methods (like Ubuntu):
$ sudo a2enmod proxy
$ sudo a2enmod proxy_fcgi
$ sudo systemctl restart apache2
If you are not on a Debian-based system, you’ll be able to allow an Apache module by including it to the Apache configuration file or in a file within the /and many others/httpd/conf.modules.d/
listing, relying in your distribution’s configuration.
Next, open your Apache configuration file in a textual content editor. For instance, on a Debian-based system:
$ sudo vi /and many others/apache2/apache2.conf
On a Fedora system (together with Red Hat Enterprise Linux and CentOS):
$ sudo vi /and many others/httpd/conf/httpd.conf
Add this line to the tip of the file:
ProxyPass "/stockapp" unix:///var/lib/vv/stockapp/sock/sock|fcgi://localhost/stockapp
Depending in your webserver configuration, there could also be a greater place so as to add the ProxyPass
directive. For this instance, although, the above is enough.
Save the file and restart the webserver. On Fedora-based methods:
$ sudo systemctl restart httpd
On Debian-based methods:
$ sudo systemctl restart apache2
In this case, you are connecting to your utility via a socket, however you need to use a TCP port as a substitute (which is useful when your utility resides in a container or one thing related).
Run the applying
Start the applying server in your utility:
$ vf stockapp
By default, this runs anyplace from 0 to twenty server processes in your utility, relying on the load. When the person load is low, your utility makes use of nearly no reminiscence in any respect.
That was it! Navigate to http://127.0.0.1/stockapp?req=stock&action=add&stock_name=XYZ&stock_pri… in your internet browser to see the applying.
You’ve simply up to date the inventory value for ticker “XYZ” to 440. Try totally different tickers and costs to construct a listing of shares, which you’ll view with the URL http://127.0.0.1/stockapp?req=stock&action=show.
Congratulations, you have created your first Vely utility, reverse proxied behind an online server.
You also can view the output with out a graphical browser by using curl:
$ curl -s
"http://127.0.0.1/stockapp?req=stock&action=add&stock_name=XYZ&stock_price=440"
$ curl -s "http://127.0.0.1/stockapp?req=stock&action=show"
Run the applying from the terminal
You can run your utility from the terminal, too. A terminal command is at all times made together with the FastCGI utility server, and it is named the identical as your utility (on this case, stockapp
). It works precisely the identical as the net app. You can write some requests to your utility to be fulfilled as internet requests and others to run from the command-line. To do this, you present the request as surroundings variables. For occasion, to output the record of shares as HTML, sort:
$ export REQUEST_METHOD=GET
$ export QUERY_STRING="req=stock&action=show"
$ /var/lib/vv/bld/stockapp/stockapp
To suppress HTTP headers, use:
$ export VV_SILENT_HEADER=sure
$ /var/lib/vv/bld/stockapp/stockapp
How Vely works
Your utility works by processing requests and sending again replies. A request is one among two HTTP strategies: GET or POST.
A request at all times has a parameter req
. In the instance right here, its worth is inventory
. That means supply code compiled from file inventory.v
is known as routinely to deal with such a request.
A supply file like this could do many alternative issues, all grouped logically beneath a single request. Here, you’ve gotten one other parameter motion
, which may have a price of add
(so as to add or replace a inventory) or present
(to indicate a listing of shares). You specify stock_name
and stock_price
parameters when including or updating. Pretty simple stuff. Other than req
, you’ll be able to select parameter names nonetheless you want.
Looking on the code in inventory.v
, it is easy to observe. You use the input-param assemble to get the values in your enter parameters. Yes, these unusual issues within the C code that are not C are Vely language constructs, and so they do a number of helpful stuff for you, similar to run-query, which (as you may count on from the title) runs your queries. An simple one is @
, which is an output construct. String dealing with is made easy and dependable with out worrying about buffer overruns. Check out the full reference of Vely constructs to know Vely’s capabilities.
Vely converts all constructs in your code into pure C and makes a local executable that may be very small and quick. Your utility runs as a number of FastCGI server processes, which keep resident in reminiscence whereas accepting and processing requests. All of those processes work in parallel.
For extra data, see how Vely works and skim extra about Vely architecture.
Manage strings and reminiscence
Vely has automated rubbish assortment for all of its constructs. In reality, more often than not, you should not must free reminiscence in any respect, so utility improvement is even less complicated. Leave that to Vely and revel in computing freed from reminiscence leaks and much fewer reminiscence points than you may count on. String constructs similar to write-string
make it protected, quick, and simple to create complicated strings simply as they do easy ones.
FastCGI program supervisor
Even if you happen to do not need to develop your personal purposes with Vely, you need to use vf
, Vely’s FastCGI program manager, with any generic FastCGI program, not simply these created with Vely.
Want to be taught extra about Vely?
I typically get requested in regards to the undertaking title. Vely is brief for Vel(ocit)y. It’s quick to program with, quick to know code and preserve, and quick (and small!) at run time. It’s even simple to containerize.
Check out the documentation at vely.dev, which options downloads and examples that transcend the introduction this text gives.