Skip to content

Commit

Permalink
Allow calls to Khoj server from Obsidian mobile app to fix CORS issue
Browse files Browse the repository at this point in the history
- Obsidian mobile uses capacitor js. Requests from it have origin as
  http://localhost on Android and capacitor://localhost on iOS
- Allow those Obsidian mobile origins in CORS middleware of server
  • Loading branch information
debanjum committed Feb 5, 2024
1 parent 447904f commit 04a8bbf
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/interface/obsidian/src/chat_modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,13 @@ export class KhojChatModal extends Modal {
async getChatHistory(chatBodyEl: Element): Promise<boolean> {
// Get chat history from Khoj backend
let chatUrl = `${this.setting.khojUrl}/api/chat/history?client=obsidian`;
let headers = { "Authorization": `Bearer ${this.setting.khojApiKey}` };

try {
let response = await fetch(chatUrl, { method: "GET", headers: headers });
let response = await fetch(chatUrl, {
method: "GET",
headers: { "Authorization": `Bearer ${this.setting.khojApiKey}` },
});

let responseJson: any = await response.json();

if (responseJson.detail) {
Expand Down Expand Up @@ -361,7 +364,6 @@ export class KhojChatModal extends Modal {
let response = await fetch(chatUrl, {
method: "GET",
headers: {
"Access-Control-Allow-Origin": "*",
"Content-Type": "text/event-stream",
"Authorization": `Bearer ${this.setting.khojApiKey}`,
},
Expand Down Expand Up @@ -422,7 +424,7 @@ export class KhojChatModal extends Modal {
let chatBody = this.contentEl.getElementsByClassName("khoj-chat-body")[0];

let response = await request({
url: `${this.setting.khojUrl}/api/chat/history?client=web`,
url: `${this.setting.khojUrl}/api/chat/history?client=obsidian`,
method: "DELETE",
headers: { "Authorization": `Bearer ${this.setting.khojApiKey}` },
})
Expand Down
2 changes: 2 additions & 0 deletions src/khoj/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
CORSMiddleware,
allow_origins=[
"app://obsidian.md",
"http://localhost",
"capacitor://localhost",
"http://localhost:*",
"http://127.0.0.1:*",
f"https://{KHOJ_DOMAIN}",
Expand Down

0 comments on commit 04a8bbf

Please sign in to comment.