title: “GoWeb VARIABLE::CONFIG Function” author: “Pierre Laplante” date: 2025-08-30 version: “1.0.2” section: “1g”
variable::config return all configuration parameters
variable::config() or simply config()
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:
$HOME/.config/goweb
/www
.
(current directory)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"
source_name
follows the format:
username:password@tcp(ip:port)/database_name
exec_dir
specifies which directories can be executed by goweb.
Paths are relative to docroot
. All other paths are served statically.
If the URL points to a directory, index_name
values are appended in order to locate the file.
404
specifies the file returned when a resource is not found.
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"
}
load
and preload
load
specifies a filename to be loaded into memory, typically containing functions.The preload function must return one of the following values:
preload::CONT (0)
— Continue processing the request.preload::REWRITE (1)
— Output the function result and exit.preload::RETURN (3)
— Immediately return a status (often used with status functions).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;
}
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>.
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.
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
docroot
.PRELOAD::ROUTE
.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>
If a filename is specified for the error
parameter, that file will be executed whenever an error occurs.
Additionally:
errmsg
is set with the error message.Pierre Laplante laplante@plcb.ca