#302 - Fixed
This commit is contained in:
13
src/main.ts
13
src/main.ts
@@ -24,17 +24,23 @@ import { database, OmnisearchCache } from './database'
|
|||||||
import * as NotesIndex from './notes-index'
|
import * as NotesIndex from './notes-index'
|
||||||
import { searchEngine } from './search/omnisearch'
|
import { searchEngine } from './search/omnisearch'
|
||||||
import { cacheManager } from './cache-manager'
|
import { cacheManager } from './cache-manager'
|
||||||
import getServer from './tools/api-server'
|
|
||||||
|
|
||||||
export default class OmnisearchPlugin extends Plugin {
|
export default class OmnisearchPlugin extends Plugin {
|
||||||
private ribbonButton?: HTMLElement
|
private ribbonButton?: HTMLElement
|
||||||
|
|
||||||
public apiHttpServer = getServer()
|
// FIXME: fix the type
|
||||||
|
public apiHttpServer: null | any = null
|
||||||
|
|
||||||
async onload(): Promise<void> {
|
async onload(): Promise<void> {
|
||||||
await loadSettings(this)
|
await loadSettings(this)
|
||||||
this.addSettingTab(new SettingsTab(this))
|
this.addSettingTab(new SettingsTab(this))
|
||||||
|
|
||||||
|
if (!Platform.isMobile) {
|
||||||
|
import('./tools/api-server').then(m =>
|
||||||
|
this.apiHttpServer = m.getServer()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
if (isPluginDisabled()) {
|
if (isPluginDisabled()) {
|
||||||
console.log('Omnisearch - Plugin disabled')
|
console.log('Omnisearch - Plugin disabled')
|
||||||
return
|
return
|
||||||
@@ -116,7 +122,7 @@ export default class OmnisearchPlugin extends Plugin {
|
|||||||
this.executeFirstLaunchTasks()
|
this.executeFirstLaunchTasks()
|
||||||
await this.populateIndex()
|
await this.populateIndex()
|
||||||
|
|
||||||
if (settings.httpApiEnabled) {
|
if (this.apiHttpServer && settings.httpApiEnabled) {
|
||||||
this.apiHttpServer.listen(settings.httpApiPort)
|
this.apiHttpServer.listen(settings.httpApiPort)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -267,4 +273,3 @@ function registerAPI(plugin: OmnisearchPlugin): void {
|
|||||||
// Deprecated
|
// Deprecated
|
||||||
;(app as any).plugins.plugins.omnisearch.api = api
|
;(app as any).plugins.plugins.omnisearch.api = api
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import {
|
import {
|
||||||
Notice,
|
Notice,
|
||||||
|
Platform,
|
||||||
Plugin,
|
Plugin,
|
||||||
PluginSettingTab,
|
PluginSettingTab,
|
||||||
Setting,
|
Setting,
|
||||||
@@ -462,6 +463,7 @@ export class SettingsTab extends PluginSettingTab {
|
|||||||
|
|
||||||
//#region HTTP Server
|
//#region HTTP Server
|
||||||
|
|
||||||
|
if (!Platform.isMobile) {
|
||||||
const httpServerDesc = new DocumentFragment()
|
const httpServerDesc = new DocumentFragment()
|
||||||
httpServerDesc.createSpan({}, span => {
|
httpServerDesc.createSpan({}, span => {
|
||||||
span.innerHTML = `Omnisearch can be used through a simple HTTP server (<a href="https://publish.obsidian.md/omnisearch/Public+API+%26+URL+Scheme#HTTP+Server">more information</a>).`
|
span.innerHTML = `Omnisearch can be used through a simple HTTP server (<a href="https://publish.obsidian.md/omnisearch/Public+API+%26+URL+Scheme#HTTP+Server">more information</a>).`
|
||||||
@@ -502,6 +504,7 @@ export class SettingsTab extends PluginSettingTab {
|
|||||||
await saveSettings(this.plugin)
|
await saveSettings(this.plugin)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
//#endregion HTTP Server
|
//#endregion HTTP Server
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import * as http from 'http'
|
|||||||
import * as url from 'url'
|
import * as url from 'url'
|
||||||
import api from './api'
|
import api from './api'
|
||||||
import { Notice } from 'obsidian'
|
import { Notice } from 'obsidian'
|
||||||
|
import { saveSettings, settings } from 'src/settings'
|
||||||
|
|
||||||
export function getServer() {
|
export function getServer() {
|
||||||
const server = http.createServer(async function (req, res) {
|
const server = http.createServer(async function (req, res) {
|
||||||
@@ -26,8 +27,7 @@ export function getServer() {
|
|||||||
res.statusCode = 200
|
res.statusCode = 200
|
||||||
res.setHeader('Content-Type', 'application/json')
|
res.setHeader('Content-Type', 'application/json')
|
||||||
res.end(JSON.stringify(results))
|
res.end(JSON.stringify(results))
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
res.end()
|
res.end()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -40,12 +40,23 @@ export function getServer() {
|
|||||||
return {
|
return {
|
||||||
listen(port: string) {
|
listen(port: string) {
|
||||||
console.log(`Omnisearch - Starting HTTP server on port ${port}`)
|
console.log(`Omnisearch - Starting HTTP server on port ${port}`)
|
||||||
server.listen({
|
server.listen(
|
||||||
|
{
|
||||||
port: parseInt(port),
|
port: parseInt(port),
|
||||||
host: 'localhost',
|
host: 'localhost',
|
||||||
})
|
},
|
||||||
|
() => {
|
||||||
console.log(`Omnisearch - Started HTTP server on port ${port}`)
|
console.log(`Omnisearch - Started HTTP server on port ${port}`)
|
||||||
new Notice(`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.`
|
||||||
|
)
|
||||||
|
})
|
||||||
},
|
},
|
||||||
close() {
|
close() {
|
||||||
server.close()
|
server.close()
|
||||||
|
|||||||
Reference in New Issue
Block a user