2020-03-10 23:46:09 +08:00
|
|
|
---
|
|
|
|
name: require
|
|
|
|
parameters:
|
|
|
|
- path
|
2023-01-13 05:59:42 +08:00
|
|
|
ts_ignore: true
|
2020-03-10 23:46:09 +08:00
|
|
|
---
|
|
|
|
|
2020-03-13 00:41:00 +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".
|
|
|
|
|
2020-03-13 00:41:00 +08:00
|
|
|
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
|
2020-03-13 00:41:00 +08:00
|
|
|
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
|
2020-03-13 00:41:00 +08:00
|
|
|
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:
|
|
|
|
|
2023-01-30 02:02:56 +08:00
|
|
|
{% code title="dnsconfig.js" %}
|
2023-01-20 20:56:20 +08:00
|
|
|
```javascript
|
2023-05-25 04:09:22 +08:00
|
|
|
require("kubernetes/clusters.js");
|
2020-03-10 23:46:09 +08:00
|
|
|
|
2023-06-18 11:35:13 +08:00
|
|
|
D("example.com", REG_MY_PROVIDER, DnsProvider(DSP_MY_PROVIDER),
|
2020-03-10 23:46:09 +08:00
|
|
|
IncludeKubernetes()
|
|
|
|
);
|
2022-02-18 01:22:31 +08:00
|
|
|
```
|
2023-01-30 02:02:56 +08:00
|
|
|
{% endcode %}
|
2020-03-10 23:46:09 +08:00
|
|
|
|
2023-01-30 02:02:56 +08:00
|
|
|
{% code title="kubernetes/clusters.js" %}
|
2023-01-20 20:56:20 +08:00
|
|
|
```javascript
|
2023-05-25 04:09:22 +08:00
|
|
|
require("./clusters/prod.js");
|
|
|
|
require("./clusters/dev.js");
|
2020-03-10 23:46:09 +08:00
|
|
|
|
|
|
|
function IncludeKubernetes() {
|
|
|
|
return [includeK8Sprod(), includeK8Sdev()];
|
|
|
|
}
|
2022-02-18 01:22:31 +08:00
|
|
|
```
|
2023-01-30 02:02:56 +08:00
|
|
|
{% endcode %}
|
2020-03-10 23:46:09 +08:00
|
|
|
|
2023-01-30 02:02:56 +08:00
|
|
|
{% code title="kubernetes/clusters/prod.js" %}
|
2023-01-20 20:56:20 +08:00
|
|
|
```javascript
|
2020-03-10 23:46:09 +08:00
|
|
|
function includeK8Sprod() {
|
2023-01-13 05:59:42 +08:00
|
|
|
return [
|
|
|
|
// ...
|
|
|
|
];
|
2020-03-10 23:46:09 +08:00
|
|
|
}
|
2022-02-18 01:22:31 +08:00
|
|
|
```
|
2023-01-30 02:02:56 +08:00
|
|
|
{% endcode %}
|
2020-03-10 23:46:09 +08:00
|
|
|
|
2023-01-30 02:02:56 +08:00
|
|
|
{% code title="kubernetes/clusters/dev.js" %}
|
2023-01-20 20:56:20 +08:00
|
|
|
```javascript
|
2020-03-10 23:46:09 +08:00
|
|
|
function includeK8Sdev() {
|
2023-01-13 05:59:42 +08:00
|
|
|
return [
|
|
|
|
// ...
|
|
|
|
];
|
2020-03-10 23:46:09 +08:00
|
|
|
}
|
2022-02-18 01:22:31 +08:00
|
|
|
```
|
2023-01-30 02:02:56 +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
|
|
|
|
2023-01-30 02:02:56 +08:00
|
|
|
{% code title="dnsconfig.js" %}
|
2023-01-20 20:56:20 +08:00
|
|
|
```javascript
|
2023-05-25 04:09:22 +08:00
|
|
|
var domains = require("./domain-ip-map.json")
|
2020-03-10 23:46:09 +08:00
|
|
|
|
|
|
|
for (var domain in domains) {
|
2023-05-25 04:09:22 +08:00
|
|
|
D(domain, REG_MY_PROVIDER, PROVIDER,
|
2020-03-10 23:46:09 +08:00
|
|
|
A("@", domains[domain])
|
|
|
|
);
|
|
|
|
}
|
2022-02-18 01:22:31 +08:00
|
|
|
```
|
2023-01-30 02:02:56 +08:00
|
|
|
{% endcode %}
|
2020-03-10 23:46:09 +08:00
|
|
|
|
2023-01-30 02:02:56 +08:00
|
|
|
{% code title="domain-ip-map.json" %}
|
2023-01-20 20:56:20 +08:00
|
|
|
```javascript
|
2020-03-10 23:46:09 +08:00
|
|
|
{
|
2023-06-17 20:58:17 +08:00
|
|
|
"example.com": "1.1.1.1",
|
|
|
|
"other-example.com``": "5.5.5.5"
|
2020-03-10 23:46:09 +08:00
|
|
|
}
|
2022-02-18 01:22:31 +08:00
|
|
|
```
|
2023-01-30 02:02:56 +08:00
|
|
|
{% endcode %}
|
2020-03-13 00:41:00 +08:00
|
|
|
|
2023-09-14 22:25:45 +08:00
|
|
|
# Notes
|
2020-03-13 00:41:00 +08:00
|
|
|
|
2023-09-14 22:25:45 +08:00
|
|
|
`require()` is *much* closer to PHP's `include()` function than it
|
|
|
|
is to node's `require()`.
|
2020-03-13 00:41:00 +08:00
|
|
|
|
2023-09-14 22:25:45 +08:00
|
|
|
Node's `require()` only includes a file once.
|
2023-01-30 02:13:51 +08:00
|
|
|
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.
|