Name

Web::VWF - Versatile Web Framework - easily build a website to support different languages and client platforms

Version

Version 0.004.1

Synopsis

A site's page script, cgi-bin/page.fcgi, is all of this:

    use FindBin;
    use Web::VWF;

    Web::VWF->new({ directory => "$FindBin::Bin/../conf" })->run();

Description

Web::VWF is VWF, the Versatile Web Framework, packaged as a CPAN distribution. It is an MVC framework for building responsive websites that adapt to the client's language, to the kind of client (web, mobile, robot, search engine or AI crawler) and to whichever database back end the site uses.

This module is the distribution's front door: it holds the version number, this overview, and the one thing a site builds by hand - "new", the site's configuration and the directory the server keeps it in. Working the framework does these modules:

example/ in this distribution is a small working site to copy and adapt: example/cgi-bin/page.fcgi is the page script, example/conf/default is the configuration (placeholders only - put real keys in your own copy), and example/templates/ holds the templates. Run it from the command line with CONFIG_DIR=$(pwd)/example/conf root_dir=$(pwd)/example perl -Ilib example/cgi-bin/page.fcgi page=index.

A site's templates sit under a path built from the page's class name, so the file to change when the page module is Web::VWF::Display::index is templates/web/Web/VWF/Display/index.tmpl - see Web::VWF::Display for the rest of the search order.

A site that used to carry a renamed copy of VWF of its own deletes that copy, installs this distribution, renames VWF:: to Web::VWF:: in its own code, and moves its templates from .../VWF/Display/ to .../Web/VWF/Display/.

Configuration

A site is configured by files, usually XML, in its conf/ directory (see Web::VWF::Config for where they are looked for, in which order, and how an environment variable of the same name as a top-level key overrides that key). The file named default is read first, then one named for the site's domain name, then $CONFIG_FILE. example/conf/default is a complete working example.

Every setting is optional. What a site does not say gets the default given below, which is what the framework did before the setting existed.

Environment Variables

Pages: Display

    <display>
            <directory>/path/to/modules</directory>
            <page>
                    <name>states</name>
                    <description>Displays information about the U.S. states</description>
                    <directory>/path/to/this/page</directory>
            </page>
    </display>

One page for each page that ?page= may name. The name is required and may only be word characters. The description is shown in the 300 Multiple Choices response, which lists the pages when no page is given. A page is the class Web::VWF::Display::<name> and its template.

directory says where the page modules are. In display it is the default for all of them, in a page it is that page's alone and wins. It is where the <name>.pm file is, not a directory of templates. A relative one is taken from the directory the page script is in. A page with no directory at all is found in @INC.

With no page at all, display's directory is searched and every .pm in it, except captcha.pm, is a page. With no display either, that is ../lib/Web/VWF/Display beside the script.

The old pages is read when there is no display, as is its old form of one string of names, <pages>index, meta_data</pages>.

Data: Databases and Data

    <databases>
            <directory>/path/to/files</directory>
    </databases>
    <data>
            <directory>/path/to/drivers</directory>
            <driver>
                    <name>states</name>
                    <description>List of data about the U.S. states</description>
                    <directory>/path/to/this/driver</directory>
            </driver>
    </data>

databases says where the data files are (CSV, XML, SQLite), and data where the drivers that read them are: the Database::Abstraction subclasses. They are separate so that the files may be kept apart from code.

databases's directory is the directory of the data files. Default: ../databases beside the script. Pages are told it as database_dir and databasedir. The old top-level database_dir is read when there is no directory in databases.

One driver in data for each driver a page may use. A page is given each as a variable named for the last part of its class, lower-cased: the module Web::VWF::Data::states is $states in the page. A name with no :: in it is a Web::VWF::Data:: module; one with ::, such as Bands::Data::bands, is used as written. The description is only logged.

directory is where the driver's .pm file is, named for the last part of the class: in the driver for that driver alone, in data for all of them. With neither, the drivers are looked for in the databases directory. A driver that is not in the data or databases directory is taken from @INC, which is how a site uses the distribution's own Web::VWF::Data::index. Only the directory of a driver is strict: a driver that is not in it is not loaded.

What is loaded, in order of preference:

A module that cannot be loaded is logged and skipped; the pages that do not need it still work.

Take care in naming a driver's own configuration: the drivers are given the configuration's top-level keys as their constructor arguments, so a key called data would replace the rows of every database. The framework discards those rows, but a driver of your own that is constructed elsewhere must do the same.

Other Settings

List-valued settings (supported_languages, allow_ips, rate_limit_trusted_ips, blacklist_countries) may be written as one string separated by commas or spaces, or as an element with one child for each value.

Recaptcha

    <recaptcha>
            <enabled>1</enabled>
            <site_key>...</site_key>
            <secret_key>...</secret_key>
    </recaptcha>

Google reCAPTCHA, from https://www.google.com/recaptcha/admin. When it is enabled a client past the soft rate limit is shown a challenge instead of being refused. When it is not, the hard limit refuses with a plain-text 429.

Security

Throttle

The per-address throttle used by Web::VWF::Allow and by Web::VWF::Display, which share one file.

Caches: disc_cache and memory_cache

    <disc_cache>
            <driver>File</driver>
            <root_dir>/var/tmp/cache</root_dir>
    </disc_cache>

The CHI caches. driver is one of Memory, BerkeleyDB, DBI, Redis, Memcached, SharedMem, File or Null. Default for disc_cache: BerkeleyDB, or File where that is not installed; for memory_cache: Memory. The other keys depend on the driver:

Subroutines/Methods

New

The configuration of a site, and the directory the server keeps it in.

Parameters:

Api Specification

Input
    {
            directory => { type => 'string', optional => 1 },
            info => { type => 'object', optional => 1 },
            logger => { type => 'object', optional => 1 },
            config_directory => { type => 'string', optional => 1 },
            config_file => { type => 'string', optional => 1 },
            config => { type => 'hashref', optional => 1 },
    }

new croaks with directory must be a string when directory is a reference.

Output
    { type => 'object', isa => 'Web::VWF' }

Run

Serve requests. It accepts FastCGI requests one after another and sends each one to the page named by ?page=, as described in "DESCRIPTION"; an error in one request is logged and does not end the worker.

The loop ends, and run returns, when the web server asks the worker to exit, after a TERM or USR1 signal, or when the page script has been changed since the worker started, so that the next request starts a worker with the new code.

When REMOTE_ADDR is not set, which is how the script is run from a command line, there is no web server: run handles one request, built from the command-line arguments such as page=index, turns caching off, takes the language from LANG and prints debug logging to STDOUT among the response.

run refuses to run again from inside itself, and to run while the script is still being compiled, which happens when something reads the page script as if it were a configuration file. It prints a message on STDERR and returns.

Api Specification

Input
    {}
Output
    { type => 'undef' }

_Directory

Internal: not part of the public interface. The directory the server keeps this site's configuration in: what new() was given, or /usr/lib/vwf. Web::VWF::Config adds the site's own name to it.

Api Specification

Input
    {}
Output
    { type => 'string' }

_Config

Internal: not part of the public interface. The site's configuration, read once and kept: a site that has no conf/ of its own beside cgi-bin/ is configured from the server's directory for it, and Web::VWF::Config describes the rest of the search.

Api Specification

Input
    {}
Output
    { type => 'object', isa => 'Web::VWF::Config' }

Sig_Handler

Internal: not part of the public interface, though it is a plain function, not a method. The handler for TERM and USR1. It sets the flag that makes run leave its loop and, if no request is being handled, flushes the response cache and statistics, detaches Log::WarnDie and exits at once. Mid-request it only sets the flag, so the response in flight is sent whole.

Api Specification

Input
    { signal => { type => 'string' } }
Output
    { type => 'undef' }

Choose

Internal: a plain function. Sends the 300 Multiple Choices response: plain text that lists ?page= and the description of each page the site serves, with a Last-Modified header from the page script. The list is left out for HEAD. It does nothing when the status of the request is already not 200, as for an unknown page, where the status line is sent elsewhere.

Api Specification

Input
    {}
Output
    { type => 'undef' }

Send_Error

Internal: a plain function. Sends a short plain-text error response, and records the status in the request's CGI::Info. Takes a hash or a hashref:

Api Specification

Input
    {
            status => { type => 'integer', min => 100, max => 599 },
            body => { type => 'string', optional => 1 },
            headers => { type => 'string', optional => 1 },
            buffered => { type => 'boolean', optional => 1 },
    }
Output
    { type => 'integer', value => 1 }

Duration_Seconds

Internal: a plain function. Converts a duration such as 60s, 2 minutes or 90 to whole seconds, with Time::Duration::Parse. A string of digits is already seconds. One that cannot be parsed is logged as a warning, and counts as 60 seconds.

Api Specification

Input
    { duration => { type => 'string' } }
Output
    { type => 'integer', min => 0 }

Blacklisted

Internal: a plain function. Takes the request's CGI::Info and says whether the request looks like an SQL injection attempt. The address that sent one is remembered by the worker, so its later requests are refused without scanning them again. It is only a filter for noise, see Web::VWF::Blacklist. A request of more than 8192 characters is not scanned. There is nothing to check without REMOTE_ADDR. When it returns true the status of the request has been set to 403.

Api Specification

Input
    { info => { type => 'object', isa => 'CGI::Info' } }
Output
    { type => 'boolean' }

Filter

Internal: a plain function, registered with Log::WarnDie. Called with a warning, returns false for the few harmless ones that are not worth logging (two missing optional modules under Net::OAuth and NetAddr::IP, and an S_IFFIFO message from Fcntl) and true for all the rest.

Api Specification

Input
    { message => { type => 'string' } }
Output
    { type => 'boolean' }

Author

Nigel Horne, <njh at nigelhorne.com>

Copyright 2010-2026 Nigel Horne.

Usage is subject to the GPL2 licence terms. If you use it, please let me know.