Vely logo Empower C
install  tutorials  examples
documentation  license  about

12.1.0 released on Sep 19, 2022

Tutorial-stock app


What you'll learn

You'll learn how to:


To run the application, you need to install Apache web server and MariaDB database. You can use a different web server and database, and the setup would be similar.

Next, install Vely - it's simple by using standard packaging tools such as apt, dnf, pacman, zypper.  After installing Vely, turn on syntax highlighting in vim if you're using it:
vv -m

It is a good idea to create a separate source code directory for each application (and you can name it whatever you like):
mkdir stockapp
cd stockapp


This application saves the names of stock tickers and their prices, and you can view them in a list.

Start by creating "stock.v" file:
vi stock.v

Copy the following and save the file:
#include "vely.h"

void stock()
   out-header default
       input-param action
       input-param stock_name
       input-param stock_price
       if (!strcmp (action, "add")) {  // Add data to stock table, update if the stock exists
           run-query @db = "insert into stock (stock_name, stock_price) values ('%s', '%s') on duplicate key update stock_price='%s'" : stock_name, stock_price, stock_price error define err no-loop
           if (strcmp (err, "0")) {
               report-error "Cannot update stock price, error [%s]", err
              @Stock price updated!
       } else if (!strcmp (action, "show")) {    // Show stock names and values
                   @<td>Stock name</td>
                   @<td>Stock price</td>
               run-query @db = "select stock_name, stock_price from stock" output stock_name, stock_price
                       query-result  stock_name
                       query-result  stock_price
       } else {
           @<div>Unrecognized request!</div>

For the purpose of this tutorial, you will create database "dbstock" owned by user "vely" with password "your_password". You can change these to whatever works for you, but remember to change them everywhere in this post.

First, login to MariaDB database as root (using mysql utility), and execute:
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;

Then login to MariaDB again and set the current database:
mysql -u vely -pyour_password

and create the database object(s) needed for the application, in this case just a "stock" table in the "dbstock" database.
use dbstock;
create table if not exists stock (stock_name varchar(100) primary key, stock_price bigint);

Finally, to let Vely know where your database is, so your application can log into it, create database configuration file named "db". Why "db"? If you look at "stock.v" file, for example in code like:
run-query @db = "insert into stock ..."

the database name is given with "@" sign, in this case "@db", so the name of the database configuration file should be "db" (you can choose any name you like though). So create file "db":
vi db

and save the following in it:


The above is a standard MariaDB client options file. Vely uses native database connectivity, so you can specify any options that a given database lets you.

Build application

First you need to create an application. The application is named "stockapp", but you can name it anything. It's simple:
sudo vf -i -u $(whoami) stockapp

This will create application home under Vely directory (which is "/var/lib/vv") and do application setup for you.

To build your application use vv command line utility:
vv -q --db=mariadb:db stockapp

What you're doing here is making your application (-q option), and specifying that you are using MariaDB database described in configuration file "db" (--db option), and finally your application name is "stockapp". That's pretty simple, however keep in mind that Vely has quite a few other useful options you can use. For now, this will suffice.

Note that you can use any number of databases and different database vendors in your application. This example is simple, with only one database.

Run application

To start the application server for your application:
vf stockapp

This will by default run anywhere between 0 and 20 server processes for your application, depending on the load. It means when the user load is low, your application will use virtually no memory at all. Check out vf for more options.

You can see the response from your web application right away from command line (see more about FastCGI_client):
export QUERY_STRING="req=stock&action=add&stock_name=XYZ&stock_price=340"
cgi-fcgi -connect /var/lib/vv/stockapp/sock/sock  /

Setup web access

To access your application via web browser or other web clients, you need to setup a web server. It can be Apache, Nginx or any other server that supports FastCGI proxying (most, if not all, web servers and load balancers do this). Here, you will setup Apache, but the setup is similar for other web servers (if you're having trouble with this, see apache_reverse_proxy).

For Debian systems (like Ubuntu), enable proxy and proxy_fcgi modules:
sudo a2enmod proxy
sudo a2enmod proxy_fcgi
sudo systemctl restart apache2

Next, open your Apache configuration file in editor, so for Debian systems (like Ubuntu):
sudo vi /etc/apache2/apache2.conf

or for Fedora systems (like Red Hat):
sudo vi /etc/httpd/conf/httpd.conf

Add to the end of the file:
ProxyPass "/stockapp" unix:///var/lib/vv/stockapp/sock/sock|fcgi://localhost/stockapp

(Note that, depending on your web server configuration, there may be a better place to add the ProxyPass directive; for the purposes of this example, the above will suffice.)

Save the file and restart Apache, for Debian systems (like Ubuntu):
sudo systemctl restart apache2

or for Fedora systems (like RedHat):
sudo systemctl restart httpd

In this case, you will be connecting to your application via socket, but you can use a TCP port as well (which comes in handy when your application resides in a docker container or something similar).

Now try it - enter this in web browser (assuming you're on a local machine, change the base URL if not):

Note: if you're not using localhost (i.e., port 80 may be blocked and you may need to open it via firewall. Use "firewall-cmd" (for Fedora based systems) or "ufw" (for Debian systems) to open access to port 80.

The result is:


You've just updated the stock price for ticker "XYZ" to 440. Try different tickers and prices to build a list of stocks, which you can view with:

The result is:


Congratulations! Your web application is now reverse proxied behind a web server.

You can also view the output without a browser, by using curl:
curl -s ""

The result is:


curl -s ""

The result is:


Command line

You can run your application from the command line too. The command-line executable is always made along with the FastCGI application server and it's named the same as your application (in this case "stockapp"). It works exactly the same. You can write some requests to be fulfilled as web requests and others to run from the command line. To do that, you'd provide the request as environment variables, for instance to output the list of stocks as HTML:
export QUERY_STRING="req=stock&action=show"

The result is:


To suppress HTTP headers (for example, if you're writing output for later use or non-HTML output), set VV_SILENT_HEADER:

The result is:



Tutorials ( tutorial-file_manager   tutorial-hello_world   tutorial-multitenant_cloud   tutorials   tutorial-stock_app   tutorial-utility  )  SEE ALL (documentation)

Copyright (c) 2022 DaSoftver LLC. Vely is a trademark of Dasoftver LLC. The software and information herein are provided "AS IS" and without any warranties or guarantees of any kind. Icons copyright PaweĊ‚ Kuna licensed under MIT. This web page is licensed under CC-BY-SA-4.0.