This commit is contained in:
Simon Cambier
2023-10-13 08:47:35 +02:00
parent fc6df5d36b
commit 9a218167d7
3 changed files with 70 additions and 51 deletions

View File

@@ -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<void> {
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
@@ -116,7 +122,7 @@ 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
}

View File

@@ -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 (<a href="https://publish.obsidian.md/omnisearch/Public+API+%26+URL+Scheme#HTTP+Server">more information</a>).`
})
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 (<a href="https://publish.obsidian.md/omnisearch/Public+API+%26+URL+Scheme#HTTP+Server">more information</a>).`
})
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

View File

@@ -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,8 +27,7 @@ export function getServer() {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify(results))
}
else {
} else {
res.end()
}
}
@@ -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()