Added automatic docker build on merge to master

This PR includes:
- Automatic build & push to docker hub when pushing to master, or when triggering a workflow dispatch on master
- Automatic docker build on pull requests to validate changes
- Updated the docker-compose.yaml to use the container images, rather than building locally
- Added support for defining backend settings using the container environment 
  -> All options defined in config.toml can now be loaded instead from the environment
  -> Order of precedence is environment definition, config.toml, and finally default inline configuration defined in config.ts
- Added support for defining frontend settings using the container environment
  -> Added a dynamic api route to load the container environment definitions on the server, and provide them to the client
  -> Added a library function to fetch from the newly created API, caching the response in the session storage
  -> Modified existing calls to `process.env` to use the new library function
  -> Left the initial statically compiled environment definitions in place as a backup definition, if no environment definitions are provided

Remaining tasks todo before able to merge to [ItzCrazyKns/Perplexica](https://github.com/ItzCrazyKns/Perplexica):
- Add secret definitions for `DOCKER_USERNAME` and `DOCKER_PASSWORD` to [ItzCrazyKns/Perplexica](https://github.com/ItzCrazyKns/Perplexica) to ensure push to dockerhub works on base branch
- Update documentation with information about changes
This commit is contained in:
Andrew Pennington 2024-08-21 00:40:34 +01:00 committed by GitHub
parent 9c1936ec2c
commit ec158c0cdf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 176 additions and 43 deletions

View file

@ -1,6 +1,7 @@
import fs from 'fs';
import path from 'path';
import toml from '@iarna/toml';
import process from 'process';
const configFileName = 'config.toml';
@ -24,25 +25,52 @@ type RecursivePartial<T> = {
[P in keyof T]?: RecursivePartial<T[P]>;
};
const loadConfig = () =>
toml.parse(
fs.readFileSync(path.join(__dirname, `../${configFileName}`), 'utf-8'),
) as any as Config;
const configFilePath = path.join(__dirname, `../${configFileName}`);
export const getPort = () => loadConfig().GENERAL.PORT;
const defaultConfig: Config = {
GENERAL: {
PORT: 3001,
SIMILARITY_MEASURE: "cosine"
},
API_KEYS: {
OPENAI: "",
GROQ: "",
ANTHROPIC: ""
},
API_ENDPOINTS: {
SEARXNG: "http://localhost:32768",
OLLAMA: ""
}
}
const loadConfig = () => {
if (fs.existsSync(configFilePath)) {
return toml.parse(fs.readFileSync(configFilePath, 'utf-8')) as any as Config;
} else {
return defaultConfig;
}
}
export const getPort = () =>
process.env.PORT ?? loadConfig().GENERAL.PORT;
export const getSimilarityMeasure = () =>
loadConfig().GENERAL.SIMILARITY_MEASURE;
process.env.SIMILARITY_MEASURE ?? loadConfig().GENERAL.SIMILARITY_MEASURE;
export const getOpenaiApiKey = () => loadConfig().API_KEYS.OPENAI;
export const getOpenaiApiKey = () =>
process.env.OPENAI_API_KEY ?? loadConfig().API_KEYS.OPENAI;
export const getGroqApiKey = () => loadConfig().API_KEYS.GROQ;
export const getGroqApiKey = () =>
process.env.GROQ_API_KEY ?? loadConfig().API_KEYS.GROQ;
export const getAnthropicApiKey = () => loadConfig().API_KEYS.ANTHROPIC;
export const getAnthropicApiKey = () =>
process.env.ANTHROPIC_API_KEY ?? loadConfig().API_KEYS.ANTHROPIC;
export const getSearxngApiEndpoint = () => loadConfig().API_ENDPOINTS.SEARXNG;
export const getSearxngApiEndpoint = () =>
process.env.SEARXNG_API_ENDPOINT ?? loadConfig().API_ENDPOINTS.SEARXNG;
export const getOllamaApiEndpoint = () => loadConfig().API_ENDPOINTS.OLLAMA;
export const getOllamaApiEndpoint = () =>
process.env.OLLAMA_API_ENDPOINT ?? loadConfig().API_ENDPOINTS.OLLAMA;
export const updateConfig = (config: RecursivePartial<Config>) => {
const currentConfig = loadConfig();
@ -65,8 +93,5 @@ export const updateConfig = (config: RecursivePartial<Config>) => {
}
}
fs.writeFileSync(
path.join(__dirname, `../${configFileName}`),
toml.stringify(config),
);
fs.writeFileSync(configFilePath, toml.stringify(config));
};