dnscontrol/documentation/functions/global/require.md

119 lines
2.7 KiB
Markdown
Raw Normal View History

2020-03-10 23:46:09 +08:00
---
name: require
parameters:
- path
ts_ignore: true
2020-03-10 23:46:09 +08:00
---
`require(...)` loads the specified JavaScript or JSON file, allowing
to split your configuration across multiple files.
2023-09-14 22:25:45 +08:00
A better name for this function might be "include".
If the supplied `path` string ends with `.js`, the file is interpreted
as JavaScript code, almost as though its contents had been included in
the currently-executing file. If the path string ends with `.json`,
`require()` returns the `JSON.parse()` of the file's contents.
2023-09-14 22:25:45 +08:00
If the path string begins with a `./`, it is interpreted relative to
the currently-loading file (which may not be the file where the
2023-09-14 22:25:45 +08:00
`require()` statement is, if called within a function). Otherwise it
is interpreted relative to the program's working directory at the time
of the call.
2020-03-10 23:46:09 +08:00
2023-09-14 22:25:45 +08:00
### Example 1: Simple
In this example, we separate our macros in one file, and put groups of domains
in 3 other files. The result is a cleaner separation of code vs. domains.
{% code title="dnsconfig.js" %}
```javascript
require("lib/macros.json");
require("domains/main.json");
require("domains/parked.json");
require("domains/otherstuff.json");
```
{% endcode %}
### Example 2: Complex
Here's a more complex example:
{% code title="dnsconfig.js" %}
```javascript
require("kubernetes/clusters.js");
2020-03-10 23:46:09 +08:00
D("example.com", REG_MY_PROVIDER, DnsProvider(DSP_MY_PROVIDER),
2020-03-10 23:46:09 +08:00
IncludeKubernetes()
);
```
{% endcode %}
2020-03-10 23:46:09 +08:00
{% code title="kubernetes/clusters.js" %}
```javascript
require("./clusters/prod.js");
require("./clusters/dev.js");
2020-03-10 23:46:09 +08:00
function IncludeKubernetes() {
return [includeK8Sprod(), includeK8Sdev()];
}
```
{% endcode %}
2020-03-10 23:46:09 +08:00
{% code title="kubernetes/clusters/prod.js" %}
```javascript
2020-03-10 23:46:09 +08:00
function includeK8Sprod() {
return [
// ...
];
2020-03-10 23:46:09 +08:00
}
```
{% endcode %}
2020-03-10 23:46:09 +08:00
{% code title="kubernetes/clusters/dev.js" %}
```javascript
2020-03-10 23:46:09 +08:00
function includeK8Sdev() {
return [
// ...
];
2020-03-10 23:46:09 +08:00
}
```
{% endcode %}
2020-03-10 23:46:09 +08:00
2023-09-14 22:25:45 +08:00
### Example 3: JSON
Requiring JSON files initializes variables:
2020-03-10 23:46:09 +08:00
{% code title="dnsconfig.js" %}
```javascript
var domains = require("./domain-ip-map.json")
2020-03-10 23:46:09 +08:00
for (var domain in domains) {
D(domain, REG_MY_PROVIDER, PROVIDER,
2020-03-10 23:46:09 +08:00
A("@", domains[domain])
);
}
```
{% endcode %}
2020-03-10 23:46:09 +08:00
{% code title="domain-ip-map.json" %}
```javascript
2020-03-10 23:46:09 +08:00
{
"example.com": "1.1.1.1",
"other-example.com``": "5.5.5.5"
2020-03-10 23:46:09 +08:00
}
```
{% endcode %}
2023-09-14 22:25:45 +08:00
# Notes
2023-09-14 22:25:45 +08:00
`require()` is *much* closer to PHP's `include()` function than it
is to node's `require()`.
2023-09-14 22:25:45 +08:00
Node's `require()` only includes a file once.
In contrast, DNSControl's `require()` is actually an imperative command to
2023-09-14 22:25:45 +08:00
load the file and execute the code or parse the data from it. For example if
two files both `require("./tools.js")`, then it will be
loaded twice, whereas in node.js it would only be loaded once.