Vely logo install
examples
documentation

Vely 15.2.0 released on Jan 18, 2023
Articles updated Jan 17, 2023

Example: stock



Description:


Stock ticker example:

In a nutshell: 3 source files, 55 lines of code; MariaDB; command line; web browser; Nginx; Unix sockets;

Screenshots of application


Stock value is updated:

Vely

Showing the stock:

Vely


Setup prerequisites


Install Vely - you can use standard packaging tools such as apt, dnf, pacman or zypper.

Because they are used in this example, you will need to install Nginx as a web server and MariaDB as a database.

After installing Vely, turn on syntax highlighting in vim if you're using it:

vv -m


Get the source code


The source code is a part of Vely installation. It is a good idea to create a separate source code directory for each application (and you can name it whatever you like). In this case, unpacking the source code will do that for you:

tar xvf $(vv -o)/examples/stock.tar.gz
cd stock


Setup application


The very first step is to create an application. The application will be named "stock", but you can name it anything (if you do that, change it everywhere). It's simple to do with vf:

sudo vf -i -u $(whoami) stock

This will create a new application home (which is "/var/lib/vv/stock") and do the application setup for you. Mostly that means create various subdirectories in the home folder, and assign them privileges. In this case only current user (or the result of "whoami" Linux command) will own those directories with 0700 privileges; it means a secure setup.

Setup the database


Before any coding, you need some place to store the information used by the application. First, you will create MariaDB database "db_stock" owned by user "vely" with password "your_password". You can change any of these names, but remember to change them everywhere here. And then, you will create database objects in the database.

Execute the following logged in as root in mysql utility:

--Create velydb database hosting application data (if it doesn't exist):
create database if not exists db_stock;
create user if not exists vely identified by 'your_password';
grant create,alter,drop,select,insert,delete,update on db_stock.* to vely;
-- Create database objects needed for the application (eg. tables, indexes):
use db_stock;
source setup.sql;
exit


Connect Vely to a database


In order to let Vely know where your database is and how to log into it, you will create database_config_file named "db_stock". This name doesn't have to be "db_stock", rather it can be anything - this is the name used in actual database statements in source code (like run-query), so if you change it, make sure you change it everywhere. Create it:

echo '[client]
user=vely
password=your_password
database=db_stock
protocol=TCP
host=127.0.0.1
port=3306' > db_stock

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

Build application


Use vv utility to make the application:

vv -q --db=mariadb:db_stock

Note usage of --db option to specify MariaDB database and the database configuration file name.

Start your application server


To start the application server for your web application use vf FastCGI process manager. The application server will use a Unix socket to communicate with the web server (i.e. a reverse-proxy):

vf -w 3 stock

This will start 3 daemon processes to serve the incoming requests. You can also start an adaptive server that will increase the number of processes to serve more requests, and gradually reduce the number of processes when they're not needed:

vf stock

See vf for more options to help you achieve best performance.

To stop your application server:

vf -m quit stock


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 ("/stock" is the application path (see request_URL) and "stock" is your application name):

location /stock { include /etc/nginx/fastcgi_params; fastcgi_pass  unix:///var/lib/vv/stock/sock/sock; }

- Step 2:
Finally, restart Nginx:

sudo systemctl restart nginx


Access application server from the browser


Use the following URL(s) to access your application server from a web client like browser (see request_URL). Use actual IP or web address instead of 127.0.0.1 if different.

#Add stock ticker 'XYZ' with stock price 450 
http://127.0.0.1/stock/add-stock?name=XYZ&price=450

#Display list of stock tickers 
http://127.0.0.1/stock/show-stock

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.

Access application server from command line


To access your application server from command line (instead through web browser/web server), use "cgi-fcgi" to see the application response:

#Add stock ticker 'XYZ' with stock price 450 
export REQUEST_METHOD=GET
export SCRIPT_NAME='/stock'
export PATH_INFO='/add-stock'
export QUERY_STRING='name=XYZ&price=450'
cgi-fcgi -connect /var/lib/vv/stock/sock/sock /

#Display list of stock tickers 
export REQUEST_METHOD=GET
export SCRIPT_NAME='/stock'
export PATH_INFO='/show-stock'
export QUERY_STRING=''
cgi-fcgi -connect /var/lib/vv/stock/sock/sock /

Note: to suppress output of HTTP headers, add this before running "cgi-fcgi":

export VV_SILENT_HEADER=yes

If you need to, you can also run your application as a CGI program.

Run program from command line


Execute the following to run your application from command line (as a command-line utility):

#Add stock ticker 'XYZ' with stock price 450 
export REQUEST_METHOD=GET
export SCRIPT_NAME='/stock'
export PATH_INFO='/add-stock'
export QUERY_STRING='name=XYZ&price=450'
/var/lib/vv/bld/stock/stock

#Display list of stock tickers 
export REQUEST_METHOD=GET
export SCRIPT_NAME='/stock'
export PATH_INFO='/show-stock'
export QUERY_STRING=''
/var/lib/vv/bld/stock/stock

Note: to suppress output of HTTP headers, add this before running /var/lib/vv/bld/stock/stock program:

export VV_SILENT_HEADER=yes

Note: if running your program as a command-line utility is all you want, you don't need to run an application server.

Files:


You are now done with the example! What follows are the source files in this project so you can examine how it works:

Database objects (setup.sql)


Table "stock" with stock name and price is created for this example:

create table if not exists stock (stock_name varchar(100) primary key, stock_price bigint);


Add stock ticker (add_stock.vely)


This requests (/add-stock) saves the names of stock tickers and their prices. It will obtain the stock name and price from the web client via input-param statement, and then use INSERT SQL to store this data in the database.

// SPDX-License-Identifier: Apache-2.0
// Copyright 2018 DaSoftver LLC.

#include "vely.h"

void add_stock()
{
    out-header default
    @<html>
        @<body>
        input-param name
        input-param price
        // Add data to stock table, update if the stock exists
        run-query @db_stock = "insert into stock (stock_name, stock_price) values ('%s', '%s') on duplicate key update stock_price='%s'" : name, price, price error define err no-loop
        if (strcmp (err, "0")) {
            report-error "Cannot update stock price, error [%s]", err
        }
        @<div>
            @Stock price updated!
        @</div>
        @</body>
    @</html>
}


Show stock tickers (show_stock.vely)


You can view stock tickers in a list. SELECT SQL is used to get all the stocks saved so far, and display them in a table using run-query and then query-result to get the query results.

// SPDX-License-Identifier: Apache-2.0
// Copyright 2018 DaSoftver LLC.

#include "vely.h"

void show_stock()
{
    out-header default
    @<html>
        @<body>
        // Show stock names and values
            @<table>
                @<tr>
                    @<td>Stock name</td>
                    @<td>Stock price</td>
                @</tr>
                run-query @db_stock = "select stock_name, stock_price from stock" output stock_name, stock_price
                    @<tr>
                        @<td>
                        query-result  stock_name
                        @</td>
                        @<td>
                        query-result  stock_price
                        @</td>
                    @</tr>
                end-query
            @</table>
        @</body>
    @</html>
}


See also:


Examples ( example_cookies   example_create_table   example_docker   example_file_manager   example_form   example_hello_world   example_json   example_multitenant_SaaS   examples   example_sendmail   example_shopping   example_stock   example_utility   example_write_report  )  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. Vely elephant logo (c) 2022 DaSoftver LLC. This web page is licensed under CC-BY-SA-4.0.