Add support for config file

Closes: https://todo.sr.ht/~emersion/gamja/20
This commit is contained in:
Simon Ser 2021-05-25 12:33:22 +02:00
parent e0cde27d66
commit d261886199
3 changed files with 84 additions and 28 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
node_modules node_modules
/config.json

View file

@ -48,6 +48,21 @@ gamja settings can be overridden using URL query parameters:
- `server`: path or URL to the WebSocket server - `server`: path or URL to the WebSocket server
- `channels`: comma-separated list of channels to join - `channels`: comma-separated list of channels to join
## Configuration file
gamja default settings can be set using a `config.json` file at the root:
```js
{
"server": {
// WebSocket URL to connect to (string)
"url": "wss://irc.example.org",
// Channel(s) to auto-join (string or array of strings)
"autojoin": "#gamja"
}
}
```
## Contributing ## Contributing
Send patches on the [mailing list], report bugs on the [issue tracker]. Discuss Send patches on the [mailing list], report bugs on the [issue tracker]. Discuss

View file

@ -16,6 +16,21 @@ import { SERVER_BUFFER, BufferType, ReceiptType, NetworkStatus, Unread } from ".
import commands from "../commands.js"; import commands from "../commands.js";
import { setup as setupKeybindings } from "../keybindings.js"; import { setup as setupKeybindings } from "../keybindings.js";
const configPromise = fetch("../config.json")
.then((resp) => {
if (resp.ok) {
return resp.json();
}
if (resp.status !== 404) {
console.error("Failed to fetch config: HTTP error:", resp.status, resp.statusText);
}
return {};
})
.catch((err) => {
console.error("Failed to fetch config:", err);
return {};
});
const CHATHISTORY_MAX_SIZE = 4000; const CHATHISTORY_MAX_SIZE = 4000;
var messagesCount = 0; var messagesCount = 0;
@ -179,40 +194,63 @@ export default class App extends Component {
...connectParams, ...connectParams,
autoconnect: true, autoconnect: true,
}; };
} else {
var params = parseQueryString();
var host = window.location.host || "localhost:8080";
var proto = "wss:";
if (window.location.protocol != "https:") {
proto = "ws:";
}
var path = window.location.pathname || "/";
if (!window.location.host) {
path = "/";
}
var serverURL;
if (params.server) {
if (params.server.startsWith("/")) {
serverURL = proto + "//" + host + params.server;
} else {
serverURL = params.server;
}
} else {
serverURL = proto + "//" + host + path + "socket";
}
this.state.connectParams.serverURL = serverURL;
if (params.channels) {
this.state.connectParams.autojoin = params.channels.split(",");
}
} }
if (window.localStorage && localStorage.getItem("receipts")) { if (window.localStorage && localStorage.getItem("receipts")) {
var obj = JSON.parse(localStorage.getItem("receipts")); var obj = JSON.parse(localStorage.getItem("receipts"));
this.receipts = new Map(Object.entries(obj)); this.receipts = new Map(Object.entries(obj));
} }
configPromise.then((config) => {
this.handleConfig(config);
return config;
});
}
handleConfig(config) {
if (this.state.connectParams.autoconnect) {
// Connection params have already been loaded from local storage
return;
}
var host = window.location.host || "localhost:8080";
var proto = "wss:";
if (window.location.protocol != "https:") {
proto = "ws:";
}
var path = window.location.pathname || "/";
if (!window.location.host) {
path = "/";
}
var connectParams = {
serverURL: proto + "//" + host + path + "socket",
};
if (config.server) {
connectParams.serverURL = config.server.url;
if (Array.isArray(config.server.autojoin)) {
connectParams.autojoin = config.server.autojoin;
} else {
connectParams.autojoin = [config.server.autojoin];
}
}
var queryParams = parseQueryString();
if (queryParams.server) {
if (queryParams.server.startsWith("/")) {
connectParams.serverURL = proto + "//" + host + queryParams.server;
} else {
connectParams.serverURL = queryParams.server;
}
}
if (queryParams.channels) {
connectParams.autojoin = queryParams.channels.split(",");
}
this.setState((state) => {
return { connectParams: { ...state.connectParams, ...connectParams } };
});
} }
dismissError(event) { dismissError(event) {
@ -929,6 +967,7 @@ export default class App extends Component {
} }
if (!activeNetwork || (activeNetwork.status !== NetworkStatus.REGISTERED && !activeBuffer)) { if (!activeNetwork || (activeNetwork.status !== NetworkStatus.REGISTERED && !activeBuffer)) {
// TODO: using key=connectParams trashes the ConnectForm state on update
return html` return html`
<section id="connect"> <section id="connect">
<${ConnectForm} <${ConnectForm}
@ -936,6 +975,7 @@ export default class App extends Component {
params=${this.state.connectParams} params=${this.state.connectParams}
disabled=${activeNetwork} disabled=${activeNetwork}
onSubmit=${this.handleConnectSubmit} onSubmit=${this.handleConnectSubmit}
key=${this.state.connectParams}
/> />
</section> </section>
`; `;