feat: enable buildtime API_ENDPOINT env var

This commit is contained in:
Timothy J. Baek 2023-10-21 16:14:12 -07:00 committed by AJ ONeal
parent f4f1283cd5
commit 86395a8c1f
No known key found for this signature in database
GPG key ID: 65118FF2A9DB590F
10 changed files with 692 additions and 64 deletions

View file

@ -1,7 +1,19 @@
import { browser, dev } from '$app/environment';
import { browser } from '$app/environment';
import { PUBLIC_API_ENDPOINT } from '$env/static/public';
export const API_ENDPOINT = browser
? `https://localhost/api`
: dev
? `http://localhost:11434/api`
: 'http://host.docker.internal:11434/api';
export const API_ENDPOINT =
PUBLIC_API_ENDPOINT === ''
? browser
? `http://${location.hostname}:11434/api`
: `http://localhost:11434/api`
: PUBLIC_API_ENDPOINT;
// Source: https://kit.svelte.dev/docs/modules#$env-static-public
// This feature, akin to $env/static/private, exclusively incorporates environment variables
// that are prefixed with config.kit.env.publicPrefix (usually set to PUBLIC_).
// Consequently, these variables can be securely exposed to client-side code.
// Example of the .env configuration:
// OLLAMA_API_ENDPOINT="http://localhost:11434/api"
// # Public
// PUBLIC_API_ENDPOINT=$OLLAMA_API_ENDPOINT

View file

@ -7,7 +7,7 @@ export const prerender = true;
// you have to set ssr to false.
// This is not the case (so set as true or comment the line)
// Documentation: https://kit.svelte.dev/docs/page-options#ssr
export const ssr = true;
// export const ssr = false;
// How to manage the trailing slashes in the URLs
// the URL for about page witll be /about with 'ignore' (default)

View file

@ -1,8 +0,0 @@
import type { PageServerLoad } from './$types';
export const load: PageServerLoad = () => {
const API_ENDPOINT = process.env.API_ENDPOINT;
return {
API_ENDPOINT
};
};

View file

@ -7,23 +7,13 @@
const { saveAs } = fileSaver;
import hljs from 'highlight.js';
import 'highlight.js/styles/dark.min.css';
import type { PageData } from './$types';
import { API_ENDPOINT as DEV_API_ENDPOINT } from '$lib/constants';
import { API_ENDPOINT } from '$lib/constants';
import { onMount, tick } from 'svelte';
import { page } from '$app/stores';
const suggestions = ''; // $page.url.searchParams.get('suggestions');
import Navbar from '$lib/components/layout/Navbar.svelte';
import SettingsModal from '$lib/components/chat/SettingsModal.svelte';
/* export let data: PageData; */
/* $: ({ API_ENDPOINT } = data); */
/* if (!API_ENDPOINT) { */
/* API_ENDPOINT = DEV_API_ENDPOINT; */
/* } */
/* console.log('API_ENDPOINT',API_ENDPOINT) */
/* console.log('DEV_API_ENDPOINT', DEV_API_ENDPOINT) */
let suggestions = ''; // $page.url.searchParams.get('suggestions');
let models = [];
let textareaElement;
@ -41,20 +31,24 @@
let messages = [];
onMount(async () => {
/* console.log('API_ENDPOINT 2', API_ENDPOINT) */
const resp = await fetch(`${DEV_API_ENDPOINT}/tags`, {
console.log(API_ENDPOINT);
const res = await fetch(`${API_ENDPOINT}/tags`, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
}
});
if (!resp.ok) {
let msg = await resp.text();
let err = new Error(msg);
throw err;
}
const data = await resp.json();
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((error) => {
console.log(error);
return { models: [] };
});
const data = res;
models = data.models;
let settings = localStorage.getItem('settings');