Skip to content

Commit

Permalink
Add api key handling
Browse files Browse the repository at this point in the history
  • Loading branch information
guerler committed Nov 1, 2024
1 parent c29dd2c commit e63db88
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 36 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"dist/galaxy-charts.umd.cjs"
],
"scripts": {
"dev": "vite",
"dev": "vite build && vite",
"build": "vite build",
"preview": "vite preview",
"prettier": "prettier --write 'package.json' '*.js' 'src/**/*.js' 'src/**/*.vue'",
Expand Down
2 changes: 1 addition & 1 deletion server.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default {
GALAXY_API: "http://127.0.0.1:8080/api",
GALAXY_ROOT: "http://127.0.0.1:8080",
};
3 changes: 2 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import { GalaxyCharts } from "galaxy-charts";
import Plugin from "@/Plugin.vue";
const config = {
dataset_url: "galaxy-charts.txt",
dataset_url: null,
dataset_id: "unavailable",
settings: {
setting_text: "My Test Setting",
setting_boolean: true,
Expand Down
7 changes: 3 additions & 4 deletions src/api/datasets.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import axios from "axios";
import { rethrowSimple } from "@/utilities/simpleError";
import { fetcher } from "@/utilities/fetcher";
import { useConfigStore } from "@/store/configStore";

export async function datasetsGet(id) {
const configStore = useConfigStore();
const url = `${configStore.getRoot()}api/datasets/${id}`;
try {
const { data } = await axios.get(url);
return data;
return await fetcher(url);
} catch (err) {
rethrowSimple(err);
}
Expand All @@ -22,7 +21,7 @@ export async function datasetsGetColumns(datasetId, columnList) {
provider: "dataset-column",
indeces: columnList.toString(),
}).toString();
const { data } = await axios.get(`${url}?${params}`);
const data = await fetcher(`${url}?${params}`);
const columnLength = columnList.length;
const results = new Array(columnLength);
for (let i = 0; i < results.length; i++) {
Expand Down
6 changes: 3 additions & 3 deletions src/components/ApiStatus.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async function checkVersion() {
version.value = await versionGet(props.root);
} catch (err) {
console.log(
"Unable to connect to Galaxy. Verify Galaxy is running and properly configured in `server.config.js`.",
`Unable to connect to Galaxy. Verify Galaxy is running and properly configured in 'server.config.js'.`,
);
version.value = "";
}
Expand All @@ -34,14 +34,14 @@ checkVersion();
<CheckCircleIcon class="text-green-600" />
</n-icon>
</template>
<span class="text-xs">Connected to Galaxy Version {{ version }}</span>
<span class="text-xs">Connected to Galaxy Version {{ version }}.</span>
</n-tooltip>
<n-tooltip v-else class="mx-1" trigger="hover">
<template #trigger>
<n-icon class="mx-1">
<ExclamationCircleIcon class="text-red-600" />
</n-icon>
</template>
<span class="text-xs">Galaxy is not running or not configured: `server.config.js`.</span>
<span class="text-xs">Galaxy is not accessible, review `server.config.js`.</span>
</n-tooltip>
</template>
11 changes: 0 additions & 11 deletions src/store/configStore.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
import { ref } from "vue";

const apiKey = ref("");
const root = ref("/");

export function useConfigStore() {
function getApiKey() {
return apiKey.value;
}

function getRoot() {
return root.value;
}

function setApiKey(newRoot) {
root.value = newRoot;
}

function setRoot(newRoot) {
root.value = newRoot;
}

return {
getApiKey,
getRoot,
setApiKey,
setRoot,
};
}
48 changes: 33 additions & 15 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,26 @@ import serverConfig from "./server.config";
import { libInjectCss } from "vite-plugin-lib-inject-css";
import { configDefaults } from "vitest/config";

// determine server route
let GALAXY_API = "";
if (process.env.GALAXY_API) {
GALAXY_API = process.env.GALAXY_API;
} else if (serverConfig.GALAXY_API) {
GALAXY_API = serverConfig.GALAXY_API;
// collect Galaxy server root
let GALAXY_ROOT = "";
if (process.env.GALAXY_ROOT) {
GALAXY_ROOT = process.env.GALAXY_ROOT;
} else if (serverConfig.GALAXY_ROOT) {
GALAXY_ROOT = serverConfig.GALAXY_ROOT;
} else {
console.warn("GALAXY_API not available. Please provide as environment variable or specify in 'server.config'.");
console.log("GALAXY_ROOT not available. Please provide as environment variable or specify in 'server.config'.");
}

// collect Galaxy API key
let GALAXY_KEY = "";
if (process.env.GALAXY_KEY) {
GALAXY_KEY = process.env.GALAXY_KEY;
} else {
console.log("GALAXY_KEY not available. Please provide as environment variable to access a remote Galaxy instance.");
}

// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue(), tailwindcss(), libInjectCss()],
resolve: {
alias: {
"@": path.resolve(__dirname, "src"),
},
},
build: {
lib: {
entry: path.resolve(__dirname, "lib/galaxy-charts.js"),
Expand All @@ -37,12 +39,28 @@ export default defineConfig({
},
},
},
define: {
'process.env.GALAXY_KEY': JSON.stringify(GALAXY_KEY),
},
plugins: [vue(), tailwindcss(), libInjectCss()],
resolve: {
alias: {
"@": path.resolve(__dirname, "src"),
},
},
server: {
proxy: {
"/api": {
target: GALAXY_API,
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, ""),
rewrite: (path) => {
if (GALAXY_KEY) {
const separator = path.includes("?") ? "&" : "?";
return `${path}${separator}key=${GALAXY_KEY}`;
} else {
return path;
}
},
target: GALAXY_ROOT,
},
},
},
Expand Down

0 comments on commit e63db88

Please sign in to comment.