19.0.0 released Nov 08, 2023
|
Hashed key/value server
You will create your own hash server in this example. A REST API will enable end-user to add key/data pairs, query and delete them. The data is in memory-only; a more involved example could be constructed to persist the data in some form (such as with SQLite). This is an extremely fast, single-process hash server.
In a nutshell: web browser; Apache; REST API; Unix sockets; 2 source files, 47 lines of code.
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:
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/hash-server.tar.gz
cd hash-server
The very first step is to create an application. The application will be named "hash-server", 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) hash-server
This will create a new application home (which is "/var/lib/vv/hash-server") 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.
Use vv utility to make the application:
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):
This will start 1 daemon process to serve the incoming requests.
See vf for more options to help you achieve best performance.
If you want to stop your application 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:
- For Debian (like Ubuntu) and OpenSUSE systems you need to enable proxy and proxy_fcgi modules:
sudo a2enmod proxy
sudo a2enmod proxy_fcgi
- For Fedora systems (or others like Archlinux) enable proxy and proxy_fcgi modules by adding (or uncommenting) LoadModule directives in the Apache configuration file - the default location of this file on Linux depends on the distribution. For Fedora (such as RedHat), Archlinux:
sudo vi /etc/httpd/conf/httpd.conf
For OpenSUSE:
sudo vi /etc/apache2/httpd.conf
Add this to the end of the file:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
- Step 2:
Edit the Apache configuration file:
- For Debian (such as Ubuntu):
sudo vi /etc/apache2/apache2.conf
- for Fedora (such as RedHat), Archlinux:
sudo vi /etc/httpd/conf/httpd.conf
- and for OpenSUSE:
sudo vi /etc/apache2/httpd.conf
Add this to the end of file ("/hash-server" is the application path (see request-URL) and "hash-server" is your application name):
ProxyPass "/hash-server" unix:///var/lib/vv/hash-server/sock/sock|fcgi://localhost/hash-server
- 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 "/hash-server" (such as for example "/hash-server.html" or "/hash-server_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 "/hash-server", 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 127.0.0.1 if different.
# Add key "key_1" with data "data_1"
http://127.0.0.1/hash-server/server/op/add/key/key_1/data/data_1
# Query key "key_1"
http://127.0.0.1/hash-server/server/op/query/key/key_1
# Delete key "key_1"
http://127.0.0.1/hash-server/server/op/delete/key/key_1
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.
- Test hash server with a bash script
To test the hash server, you can use "test_hash" bash script (see source code):
This will insert 1000 key/data value pairs, query them, delete and then query again. The result is:
Keys added
Keys queried
Keys deleted
Keys queried
Hash server test successful
- Use REST API from command line
Here is the REST API for your application.
Substitute the loopback "localhost" with the IP or web address of your server.
Note that in these REST calls, the application path is "/hash-server", and the request name is "/server", followed by URL payload (see request-URL).
The request method used is based on REST methodology, i.e. POST to create resource, GET to obtain, DELETE to delete it, etc. This is how you can use the API:
- Here is an URL payload to add a key/data pair; use the "add" operation (i.e. "add" is the value for "op" parameter, written as path segment "/op/add"), and (as an example) specify "key_1" for a key (written as path segment "/key/key_1") and "data_1" for data (written as path segment "/data/data_1"). The entire URL is:
The result:
- To query the hash server, use the "query" operation (i.e. "query" is the value for "op" parameter, written as path segment "/op/query"), and (as an example) specify "key_1" for a key (written as path segment "/key/key_1"):
The result:
- To delete a key/data pair, use the "delete" operation (i.e. "delete" is the value for "op" parameter, written as path segment "/op/delete"), and (as an example) specify "key_1" for a key (written as path segment "/key/key_1"):
The result:
The following are the source files in this application:
- Hash server (server.vely)
This is the hash server. Because the data kept in hash needs to exist beyond a single request, you'd use "process-scope" clause (see new-hash). This way hash data stays allocated and available for the life of the server process. The creation of a new hash is done within do-once statement, so it executes only once for the life of the process.
Next, you get the input parameters (see input-param); in this case task-param "op" (operation requested), as well as "key" (value of a key) and "data" (value of data). Then, depending on the operation requested, the data is added, deleted and retrieved (i.e. queried). Note that because input parameter values are not allocated memory, they are copied into new memory before being stored in the hash; this way they survive for the life of the process - this is done automatically due to "process-scope" clause when hash was created.
%% /server
out-header default
do-once
new-hash define h size 1024 process-scope
end-do-once
task-param op
input-param key
input-param data
if-task "add"
copy-string key to define c_key
copy-string data to define c_data
write-hash h key c_key value c_data \
old-value define old_data old-key define old_key \
status define st
if (st == VV_ERR_EXIST) {
delete-mem old_key
delete-mem old_data
}
@Added [<<p-out key>>]
else-task "delete"
read-hash h key (key) value define val \
old-key define okey \
delete \
status define st
if (st == VV_ERR_EXIST) {
@Not found [<<p-out key>>]
} else {
@Deleted [<<p-out val>>]
delete-mem val
delete-mem okey
}
else-task "query"
read-hash h key (key) value define val status define st
if (st == VV_ERR_EXIST) {
@Not found, queried [<<p-out key>>]
} else {
@Value [<<p-out val>>]
}
end-task
%%
- bash testing script (test_hash)
Test hash server by adding 1000 key/data pairs, querying to make sure the data is correct, deleting all of them and then querying to make sure they were all deleted.
#Restart hash-server server for this test
vf -m restart hash-server
#Add 1000 key/data pairs. Key value is 0,1,2... and data values are "data_0", "data_1", "data_2" etc.
for i in {1..1000}; do
if [ "$(curl -s "http:
echo "Error adding key [$i]"
exit -1
fi
done
echo "Keys added"
#Query all 1000 keys and check that values retrieved are the correct ones.
for i in {1..1000}; do
if [ "$(curl -s "http:
echo "Error querying key [$i]"
exit -1
fi
done
echo "Keys queried"
#Delete all 1000 keys
ERR="0"
for i in {1..1000}; do
if [ "$(curl -s "http:
echo "Error deleting key [$i]"
exit -1
fi
done
echo "Keys deleted"
#Query all 1000 keys and check that values retrieved are the correct ones.
for i in {1..1000}; do
if [ "$(curl -s "http:
echo "Error querying key [$i] after deletion."
exit -1
fi
done
echo "Keys queried"
echo "Hash server test successful"
Examples
example-client-API
example-cookies
example-create-table
example-develop-web-applications-in-C-programming-language
example-distributed-servers
example-docker
example-encryption
example-file-manager
example-form
example-hash-server
example-hello-world
example-how-to-design-application
example-how-to-use-regex
example-json
example-multitenant-SaaS
example-postgres-transactions
examples
example-sendmail
example-shopping
example-stock
example-uploading-files
example-using-mariadb-mysql
example-using-trees-for-in-memory-queries
example-utility
example-write-report
See all
documentation
You are free to copy, redistribute and adapt this web page (even commercially), as long as you give credit and provide a dofollow link back to this page - see full license at
CC-BY-4.0. Copyright (c) 2019-2023 Dasoftver LLC. Vely and elephant logo are trademarks of Dasoftver LLC. The software and information on this web site are provided "AS IS" and without any warranties or guarantees of any kind. Icons from
table-icons.io copyright Paweł Kuna, licensed under
MIT license.