From df4e3a97c00602c97eb09790d1c44026bee2b152 Mon Sep 17 00:00:00 2001 From: phil Date: Fri, 31 Jan 2025 04:36:38 +0100 Subject: [PATCH] Read ssl cert and key for https from .env file --- .gitignore | 1 + example_settings.json | 2 -- src/main.ts | 3 +-- tsconfig.node.json | 6 ++++-- vite.config.ts | 19 ++++++++++++++----- 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index c91899f..eff8b6d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules/* dist/* settings.json +.env diff --git a/example_settings.json b/example_settings.json index 4acce43..3c1f9f5 100644 --- a/example_settings.json +++ b/example_settings.json @@ -9,7 +9,5 @@ "get:time", "get:bs" ], - "sslCertFile": "/path/to/cert.pem", - "sslKeyFile": "/path/to/key.pem", "tokenSandbox": true } diff --git a/src/main.ts b/src/main.ts index f1f2fbf..427388e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -12,8 +12,6 @@ interface Settings { resourceServerUrl: string resourceScopes: string[] authProvider: string - sslCertFile: string - sslKeyFile: string tokenSandbox: boolean } @@ -22,6 +20,7 @@ export let settings: Settings export let authServer: AxiosInstance export let resourceServer: AxiosInstance +// The settings.json file is expected at the server's base url axios.get("settings.json").then().then( resp => { settings = resp.data diff --git a/tsconfig.node.json b/tsconfig.node.json index 4c399c2..50976ed 100644 --- a/tsconfig.node.json +++ b/tsconfig.node.json @@ -10,9 +10,11 @@ "compilerOptions": { "noEmit": true, "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo", - "module": "ESNext", "moduleResolution": "Bundler", - "types": ["node"] + "types": [ + "node", + "vite/client" + ] } } diff --git a/vite.config.ts b/vite.config.ts index 39eb7d7..b9462f0 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,5 +1,13 @@ +// +// Webcrypto is required for docoding the tokens, and thus https is compulsary. +// +// Read an environment file (.env) at the project's root, +// with 2 variables VITE_SSL_KEY and VITE_SSL_CERT defining the location +// of the ssl key and cert +// + import { fileURLToPath, URL } from 'node:url' -import { defineConfig, UserConfig } from 'vite' +import { defineConfig, UserConfig, loadEnv } from 'vite' import fs from 'fs'; import path from 'path'; @@ -7,7 +15,8 @@ import path from 'path'; import vue from '@vitejs/plugin-vue' //import vueDevTools from 'vite-plugin-vue-devtools' -import { settings } from "./settings" +// Read the env file +const env = loadEnv("dev", ".") let baseSettings: UserConfig = { plugins: [ @@ -21,11 +30,11 @@ let baseSettings: UserConfig = { }, } -if (settings.key && settings.cert) { +if (env.VITE_SSL_CERT && env.VITE_SSL_KEY) { baseSettings['server'] = { https: { - key: fs.readFileSync(path.resolve(settings.key)), - cert: fs.readFileSync(path.resolve(settings.cert)) + key: fs.readFileSync(path.resolve(env.VITE_SSL_KEY)), + cert: fs.readFileSync(path.resolve(env.VITE_SSL_CERT)) } } }