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/*
dist/*
settings.json
.env

View file

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

View file

@ -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

View file

@ -10,9 +10,11 @@
"compilerOptions": {
"noEmit": true,
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",
"module": "ESNext",
"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 { 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))
}
}
}