Vely logo install | documentation | examples | changelog
16.10.0 released May 10, 2023
Example: cookies

A value is entered in the browser and saved as a cookie, then read back later. This example:
This is the basic mechanism often used in saving web application states and session information.

In a nutshell:  web browser; Apache; Unix sockets; 1 source files, 49 lines of code.
Screenshots of application
A web form is displayed where the user can enter a name. Clicking "Submit" sends it to the server:


A reply page when the server receives the information and sets the cookie in the browser:


A request sent to the server will now have the cookie, which the server code will read and display back:


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

Because it is used in this example, you will need to install Apache as a web server.

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/cookies.tar.gz
cd cookies

Setup application
The very first step is to create an application. The application will be named "cookies", 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) cookies

This will create a new application home (which is "/var/lib/vv/cookies") 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.
Build application
Use vv utility to make the application:
vv -q

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 cookies

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 cookies

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

If you want to stop your application server:
vf -m quit cookies

Setup web server
This shows how to connect your application listening on a Unix socket (started with vf) to Apache web server.

- Step 1:
To setup Apache as a reverse proxy and connect your application to it, you need to enable FastCGI proxy support, which generally means "proxy" and "proxy_fcgi" modules - this is done only once:
- Step 2:
Edit the Apache configuration file:
Add this to the end of file ("/cookies" is the application path (see request_URL) and "cookies" is your application name):
ProxyPass "/cookies" unix:///var/lib/vv/cookies/sock/sock|fcgi://localhost/cookies

- Step 3:
Finally, restart Apache. On Debian systems (like Ubuntu) or OpenSUSE:
sudo systemctl restart apache2

On Fedora systems (like RedHat) and Arch Linux:
sudo systemctl restart httpd

Note: you must not have any other URL resource that starts with "/cookies" (such as for example "/cookies.html" or "/cookies_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 "/cookies", 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 if different.
#Enter cookie

#Query entered cookie

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.
You are now done with the example! What follows are the source files in this project so you can examine how it works:
Working with cookies (cookies.vely)
Usage of cookies is simple - get-cookie is used to obtain the cookie from a web client (i.e. a browser), and set-cookie to set the cookie in the browser when the response is sent back.

Three subrequests (based on "action" input parameter) allow user to enter a value that is sent back to the server (see "enter_cookie" as the "action" parameter), which the server then sends back to the browser in response (see "save_cookie"), and finally with "query_cookie" this value can be obtained and displayed again. This demonstrates how server-side software uses cookies to manipulate and obtain the client state.
// SPDX-License-Identifier: EPL-2.0
// Copyright 2018 DaSoftver LLC.

#include "vely.h"

void cookies()

    input-param action

    if (!strcmp (action, "enter_cookie")) {

        // Display a form to get cookie value
        out-header default
        @<h2>Enter your name</h2>
        @<form action="<<p-path>>/cookies" method="POST">
        @    <input type="hidden" name="action" value="save_cookie">
        @    <label for="cookie_value">Your name:</label><br/>
        @    <input type="text" name="cookie_value" value=""><br/>
        @    <br/>
        @    <input type="submit" value="Submit">

    } else if (!strcmp (action, "save_cookie")) {

        // Submittal of form: save the cookie through response to the browser
        input-param cookie_value
        get-time to define cookie_expiration year 1 timezone "GMT"
        set-cookie "customer_name" = cookie_value  expires cookie_expiration  path "/"
        out-header default
        @Cookie sent to browser!

    } else if (!strcmp (action, "query_cookie")) {

        // Web request that delivers cookie value back here (to server); display it.
        get-cookie define name="customer_name"
        out-header default
        @Customer name is <<p-web name>>

    } else {
        out-header default
        @Unrecognized action<hr/>


See also
Examples ( example_cookies   example_create_table   example_docker   example_file_manager   example_form   example_hash   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) 2017-2023 Dasoftver LLC