VARIABLE::CONFIG 1g 2024-06-04 laplante@plcb.ca GOWEB/variable


title: “GoWeb VARIABLE::CONFIG Function” author: “Pierre Laplante” date: 2025-08-30 version: “1.0.2” section: “1g”

module: “Goweb/Variable”

Name

variable::config return all configuration parameters

Synopsis

variable::config() or simply config()

Description

The config() function returns variable definitions found in the config.toml file.
The exact information returned depends on the file content.

By default, the system searches for the configuration file in the following locations, in order:

  1. $HOME/.config/goweb
  2. /www
  3. . (current directory)

Examples

Example 1: Using goweb.toml with goweb --config config.toml

When invoked, the file is searched in the current directory.
Here is an example configuration file (config.toml):

project = "extengo"
version = "0.0.0"
plugin = [ "example" ]
timeout = "10s"
load = "filename"
preload = "function"
redirect = "redirect"
error = "filename"
index_name = ["index", "index.html"]
exec_dir = [ "/extengo/table/secure/api", "/extengo/ide/" ]
pidfile = "/run/pidfile"
404 = "404.html"
routes = "route file"

[http_server]
port = ":8081"
docroot = "/home/laplante/go/src/goweb/cmd/goweb/www/"
htmldir = "html"
logdir = "log"
readtimeout = "10s"
writetimeout = "10s"
url = "https://goweb.pierrelaplante.ca"

[default_db]
source_name = "goweb:goweb@tcp(127.0.0.1:3306)/goweb"
driver_name = "mysql"

[lg]
# for Linux : fr = "fr_CA.utf8"
fr = "fr_CA.UTF-8"
en = "en_CA.utf8"

[handlers]
show_tables = "/show_tables"

[example]
file = "/usr/local/lib/example.so"
init = "Register_function"

Example call

config()

Result:

{
    "description.plugin":["example"],
    "description.version":"0.0.0",
    "description.timeout":"10s",
    "example.file":"/usr/local/lib/example.so",
    "example.init":"Register_function",
    "http_server.docroot":"/home/laplante/go/src/goweb/cmd/goweb/www/",
    "http_server.driver_name":"mysql",
    "http_server.htmldir":"html",
    "http_server.logdir":"log",
    "http_server.port":":8081",
    "http_server.readtimeout":"10s",
    "http_server.source_name":"xxx:---@tcp(127.0.0.1:3306)/xxx",
    "http_server.url":"https://goweb.pierrelaplante.ca",
    "http_server.writetimeout":"10s",
    "lg.en":"en_CA.utf8",
    "lg.fr":"fr_CA.UTF-8"
}

Example 2: load and preload

The preload function must return one of the following values:

Example:

func preload() {
    r := request();
    errorf("Executing preload on %!s(MISSING)\n", r.path);

    if r.path == "/tests/http1/test-009" {
        "rewrite to r.path="; r.path;
        return preload::REWRITE;
    }
    return preload::CONT;
}

Example 3: Redirect

Redirects are defined in the load file.

Example:

func redirect() {
    r := request();
    if r.path == "/redirect" {
        redirect("/index.html");
        return preload::REWRITE;
    }
    return preload::CONT;
}

Browser response:

HTTP/1.0 303 See Other
Content-Type: text/html; charset=utf-8
Location: /index.html
X-Goweb: 0.0.17
Date: Mon, 18 Mar 2024 16:44:34 GMT
Content-Length: 39

<a href="/index.html">See Other</a>.

Example 4: Handlers

Handlers allow execution of in-memory functions.

[handlers]
show_tables = "/show_tables"

Accessing /show_tables executes the show_tables function.
If the function is modified, the goweb server must be restarted.


Example 5: Routes

The routes parameter specifies a file with routing rules, for example:

GET /album/(.*) : file /album/list getMatch() to extract regex values
PUT /album/(.*) : func f to process request

Example:

func preload() {
    r := request();
    errorf("preload " + r.path);
    return preload::CONT;
}

func redirect() {
    r := request();
    errorf("Redirect " + r.path);
    
    if r.path == "/redirect" {
        redirect("/index.html");
        return preload::REWRITE;
    }
    
    return preload::CONT;
}

func testFuncRoute() {
    "In test func route\n";
    "match = "; getMatch();
    return preload::ROUTE;
}

To update the route file without restarting the server:

kill -USR1 <goweb_pid>

Example 6: Error Handling

If a filename is specified for the error parameter, that file will be executed whenever an error occurs.
Additionally:


Author

Pierre Laplante laplante@plcb.ca

See Also

Version