Read ssl cert and key for https from .env file

This commit is contained in:
phil 2025-01-31 04:36:38 +01:00
parent d17ba9f735
commit df4e3a97c0
5 changed files with 20 additions and 11 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
node_modules/* node_modules/*
dist/* dist/*
settings.json settings.json
.env

View file

@ -9,7 +9,5 @@
"get:time", "get:time",
"get:bs" "get:bs"
], ],
"sslCertFile": "/path/to/cert.pem",
"sslKeyFile": "/path/to/key.pem",
"tokenSandbox": true "tokenSandbox": true
} }

View file

@ -12,8 +12,6 @@ interface Settings {
resourceServerUrl: string resourceServerUrl: string
resourceScopes: string[] resourceScopes: string[]
authProvider: string authProvider: string
sslCertFile: string
sslKeyFile: string
tokenSandbox: boolean tokenSandbox: boolean
} }
@ -22,6 +20,7 @@ export let settings: Settings
export let authServer: AxiosInstance export let authServer: AxiosInstance
export let resourceServer: AxiosInstance export let resourceServer: AxiosInstance
// The settings.json file is expected at the server's base url
axios.get("settings.json").then().then( axios.get("settings.json").then().then(
resp => { resp => {
settings = resp.data settings = resp.data

View file

@ -10,9 +10,11 @@
"compilerOptions": { "compilerOptions": {
"noEmit": true, "noEmit": true,
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo", "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",
"module": "ESNext", "module": "ESNext",
"moduleResolution": "Bundler", "moduleResolution": "Bundler",
"types": ["node"] "types": [
"node",
"vite/client"
]
} }
} }

View file

@ -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 { fileURLToPath, URL } from 'node:url'
import { defineConfig, UserConfig } from 'vite' import { defineConfig, UserConfig, loadEnv } from 'vite'
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
@ -7,7 +15,8 @@ import path from 'path';
import vue from '@vitejs/plugin-vue' import vue from '@vitejs/plugin-vue'
//import vueDevTools from 'vite-plugin-vue-devtools' //import vueDevTools from 'vite-plugin-vue-devtools'
import { settings } from "./settings" // Read the env file
const env = loadEnv("dev", ".")
let baseSettings: UserConfig = { let baseSettings: UserConfig = {
plugins: [ plugins: [
@ -21,11 +30,11 @@ let baseSettings: UserConfig = {
}, },
} }
if (settings.key && settings.cert) { if (env.VITE_SSL_CERT && env.VITE_SSL_KEY) {
baseSettings['server'] = { baseSettings['server'] = {
https: { https: {
key: fs.readFileSync(path.resolve(settings.key)), key: fs.readFileSync(path.resolve(env.VITE_SSL_KEY)),
cert: fs.readFileSync(path.resolve(settings.cert)) cert: fs.readFileSync(path.resolve(env.VITE_SSL_CERT))
} }
} }
} }