diff --git a/src/main.ts b/src/main.ts index cba998d..a508a20 100644 --- a/src/main.ts +++ b/src/main.ts @@ -24,17 +24,23 @@ import { database, OmnisearchCache } from './database' import * as NotesIndex from './notes-index' import { searchEngine } from './search/omnisearch' import { cacheManager } from './cache-manager' -import getServer from './tools/api-server' export default class OmnisearchPlugin extends Plugin { private ribbonButton?: HTMLElement - public apiHttpServer = getServer() + // FIXME: fix the type + public apiHttpServer: null | any = null async onload(): Promise { await loadSettings(this) this.addSettingTab(new SettingsTab(this)) + if (!Platform.isMobile) { + import('./tools/api-server').then(m => + this.apiHttpServer = m.getServer() + ) + } + if (isPluginDisabled()) { console.log('Omnisearch - Plugin disabled') return @@ -115,8 +121,8 @@ export default class OmnisearchPlugin extends Plugin { this.executeFirstLaunchTasks() await this.populateIndex() - - if (settings.httpApiEnabled) { + + if (this.apiHttpServer && settings.httpApiEnabled) { this.apiHttpServer.listen(settings.httpApiPort) } }) @@ -267,4 +273,3 @@ function registerAPI(plugin: OmnisearchPlugin): void { // Deprecated ;(app as any).plugins.plugins.omnisearch.api = api } - diff --git a/src/settings.ts b/src/settings.ts index e8e4e36..760422c 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -1,5 +1,6 @@ import { Notice, + Platform, Plugin, PluginSettingTab, Setting, @@ -462,46 +463,48 @@ export class SettingsTab extends PluginSettingTab { //#region HTTP Server - const httpServerDesc = new DocumentFragment() - httpServerDesc.createSpan({}, span => { - span.innerHTML = `Omnisearch can be used through a simple HTTP server (more information).` - }) - new Setting(containerEl) - .setName('API Access Through HTTP') - .setHeading() - .setDesc(httpServerDesc) + if (!Platform.isMobile) { + const httpServerDesc = new DocumentFragment() + httpServerDesc.createSpan({}, span => { + span.innerHTML = `Omnisearch can be used through a simple HTTP server (more information).` + }) + new Setting(containerEl) + .setName('API Access Through HTTP') + .setHeading() + .setDesc(httpServerDesc) - new Setting(containerEl) - .setName('Enable the HTTP server') - .addToggle(toggle => - toggle.setValue(settings.httpApiEnabled).onChange(async v => { - settings.httpApiEnabled = v - if (v) { - this.plugin.apiHttpServer.listen(settings.httpApiPort) - } else { - this.plugin.apiHttpServer.close() - } - await saveSettings(this.plugin) - }) - ) + new Setting(containerEl) + .setName('Enable the HTTP server') + .addToggle(toggle => + toggle.setValue(settings.httpApiEnabled).onChange(async v => { + settings.httpApiEnabled = v + if (v) { + this.plugin.apiHttpServer.listen(settings.httpApiPort) + } else { + this.plugin.apiHttpServer.close() + } + await saveSettings(this.plugin) + }) + ) - new Setting(containerEl).setName('HTTP Port').addText(component => { - component - .setValue(settings.httpApiPort) - .setPlaceholder('51361') - .onChange(async v => { - if (parseInt(v) > 65535) { - v = settings.httpApiPort - component.setValue(settings.httpApiPort) - } - settings.httpApiPort = v - if (settings.httpApiEnabled) { - this.plugin.apiHttpServer.close() - this.plugin.apiHttpServer.listen(settings.httpApiPort) - } - await saveSettings(this.plugin) - }) - }) + new Setting(containerEl).setName('HTTP Port').addText(component => { + component + .setValue(settings.httpApiPort) + .setPlaceholder('51361') + .onChange(async v => { + if (parseInt(v) > 65535) { + v = settings.httpApiPort + component.setValue(settings.httpApiPort) + } + settings.httpApiPort = v + if (settings.httpApiEnabled) { + this.plugin.apiHttpServer.close() + this.plugin.apiHttpServer.listen(settings.httpApiPort) + } + await saveSettings(this.plugin) + }) + }) + } //#endregion HTTP Server diff --git a/src/tools/api-server.ts b/src/tools/api-server.ts index c88b61f..b4708b6 100644 --- a/src/tools/api-server.ts +++ b/src/tools/api-server.ts @@ -2,6 +2,7 @@ import * as http from 'http' import * as url from 'url' import api from './api' import { Notice } from 'obsidian' +import { saveSettings, settings } from 'src/settings' export function getServer() { const server = http.createServer(async function (req, res) { @@ -26,9 +27,8 @@ export function getServer() { res.statusCode = 200 res.setHeader('Content-Type', 'application/json') res.end(JSON.stringify(results)) - } - else { - res.end() + } else { + res.end() } } } catch (e) { @@ -40,12 +40,23 @@ export function getServer() { return { listen(port: string) { console.log(`Omnisearch - Starting HTTP server on port ${port}`) - server.listen({ - port: parseInt(port), - host: 'localhost', + server.listen( + { + port: parseInt(port), + host: 'localhost', + }, + () => { + console.log(`Omnisearch - Started HTTP server on port ${port}`) + new Notice(`Omnisearch - Started HTTP server on port ${port}`) + } + ) + + server.on('error', e => { + console.error(e) + new Notice( + `Omnisearch - Cannot start HTTP server on ${port}. See console for more details.` + ) }) - console.log(`Omnisearch - Started HTTP server on port ${port}`) - new Notice(`Omnisearch - Started HTTP server on port ${port}`) }, close() { server.close()