NaviServer - programmable web server
4.99  5.0

[ Main Table Of Contents | Table Of Contents | Keyword Index ]

ns_config(n) 5.0.0a naviserver "NaviServer Built-In Commands"

Name

ns_config - Configuration parameters

Table Of Contents

Synopsis

Description

The NaviServer process reads a Tcl configuration file (specified with the -t command line option) during early start-up. After it has changed to the correct user id and group id, bound to port 80, and possibly performed a chroot, it evaluates the configuration file as a Tcl script.

The configuration file may contain standard Tcl and NaviServer commands, plus the ns_section and ns_param commands used to define a configuration structure. Parameters are key-value pairs and are grouped into sections. Section names must be unique -- parameter keys may be duplicates.

The configuration is global and read-only. Parameters may be retrieved at run-time using ns_config, although usually configuration parameters are used by Tcl library files at start-up.

COMMANDS

The following commands are available at run-time to retrieve information from the configuration file.

ns_config ?-all? ?-bool? ?-int? ?-min integer? ?-max integer? ?-exact? ?-set? ?--? section key ?default?

Returns the parameter value associated with the given section and key from the configuration file. If the parameter is not found, then the optional default is returned, otherwise the empty string is returned.

Parameter keys are matched case-insensitively. The first parameter is returned if duplicate keys are present.

-all

Specifies that the command should return all values provided from the underlying multi-set. If not specified, just the first value is returned. The command is similar to ns_set get.

-bool

Specifies that the parameter should be a valid boolean value, using any form understood by string is boolean. An error will be thrown if this is not the case, unless a valid default is given.

-int

Specifies that the parameter should be a valid integer value, using any form understood by string is integer. An error will be thrown is this is not the case, unless a valid default is given.

-min minint

Specifies the lower bound for an integer parameter value. An error will be thrown if the parameter < minint, unless a valid default value is given.

-max maxint

Specifies the upper bound for an integer parameter value. An error will be thrown if the parameter > maxint, unless a valid default value is given.

-exact

Specifies case-sensitive parameter key matching. Not recommended.

-set

Specifies that the parameter is set to the default value, when this parameter was not provided earlier. When this is done, the default is later retrievable via ns_config.

ns_configsection ?-filter unread|defaulted|defaults? ?--? section

Returns the ns_set which contains the actual values for all parameters defined in the specified section. If there is no matching section, an empty string is returned.

The -filter can be used to return different kind of information about the parameters defined in this section.

-filter unread

return the parameter, which were set during configuration (i.e., in the configuration file) but which were not read in the startup phase of the server. This option is useful to determine e.g. typographical errors of specified parameter names.

-filter defaulted

return the parameter, from which the default values were read (i.e., which were not explicitly set)

-filter defaults

return the defaults of the parameter. This is useful for contrasting the actual values with the default values, e.g. in a web based interface.

ns_configsections

Returns a list of ns_sets, one for every section in the configuration file. The sets contain the key-value pairs for the configuration section that the set represents. The ns_set name contains the section.

CONFIGURATION FILE COMMANDS

The following commands are available only within the Tcl configuration file, which is evaluated once at server start-up.

ns_section ?-update? ?--? name

Begins a new configuration section identified by the specified name. All subsequent calls to ns_param will add parameters to this section until another ns_section command is invoked with a different section name.

Multiple invocations of ns_section using the same name allow you to build the section incrementally. The following two snippets are equivalent:

Snippet 1:

 ns_section foo {
   ns_param x 1
   ns_param y 1
 }

Snippet 2:

 ns_section foo {
   ns_param x 1
 }
 ns_section foo {
   ns_param y 1
 }

All configuration data is stored internally in ns_set structures. Since ns_set is defined as a multi-map storage, multiple values can be associated with the same key. This powerful feature may lead to surprises: when a scalar value is requested and multiple values are stored for the same key, only the first value is returned (as with ns_set get). To retrieve all values for a key, use the -all option with ns_config or ns_set.

The -update flag can be used with ns_section to update an existing section by overwriting previous values instead of simply appending new ones.

 ns_section foo {
   ns_param x 1
 }
 
 # Later in the configuration file:
 ns_section -update foo {
   ns_param x 2
 }
 
 # Query the value of parameter 'x'
 ns_config foo x
 # Result: 2
ns_param key value

Sets the specified key to value in the currently active section. Keys are matched in a case-insensitive manner, and duplicate keys are allowed.

EXAMPLES

The following example demonstrates how to set parameter values for the foo module:

 ns_section "ns/server/server1/modules/foo" {
   ns_param   enabled   true
   ns_param   map       /some/url
   ns_param   map       /some-other/url
 }

Alternatively, the section content can be specified without braces (old style):

 ns_section "ns/server/server1/modules/foo"
 ns_param   enabled   true
 ns_param   map       /some/url
 ns_param   map       /some-other/url

The following example shows how to read configuration parameters for the foo module. In this case, the ns_config command checks for a boolean enabled parameter (defaulting to false). If enabled, it retrieves all values associated with the map key and registers a handler for GET requests on each corresponding URL:

 set path ns/server/[ns_info server]/modules/foo
 
 if {[ns_config -bool $path enabled false]} {
    foreach url [ns_config -all $path map] {
      ns_register_proc GET $url foo_module_handler
    }
 }

The following example prints all configuration parameters from every section of the configuration file. It retrieves each section's ns_set via ns_configsections and uses ns_set array to list all key/value pairs:

 ns_register_proc GET /config-print {
    set config ""
    foreach section [ns_configsections] {
        append config "section: [ns_set name $section]\n"
        foreach {key value} [ns_set array $section] {
            append config "  $key: $value\n"
        }
    }
    ns_return 200 text/plain $config
 }

See Also

ns_env, ns_info, ns_register, ns_set

Keywords

configuration, global built-in, interp, parameter, startup