-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moves to new config and setup system
- Loading branch information
Showing
15 changed files
with
184 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
aside: false | ||
--- | ||
|
||
# Config | ||
|
||
Configurations are used to set up the server and database. The configurations are stored in a JSON file called `config.json` in the root directory of the project. | ||
|
||
## Config variables | ||
|
||
| Key | Type | Default | Reqiured | Description | | ||
| ------------- | ------- | ------------------------------ | -------- | -------------------------------------------------------------------- | | ||
| cors | String | [] | false | Comma separated list of domains to allow CORS requests from | | ||
| database | Object | {"name": "lunalytics"} | false | Name of the database to use | | ||
| jwtSecret | String | Random UUID | false | Secret key used to sign/verify JWT tokens | | ||
| isDemo | boolean | false | false | Set to `enabled` to enable demo mode | | ||
| migrationType | String | "automatic" | false | Type of migration to run. Can be either "automatic" or "manual" | | ||
| port | Number | 2308 | false | Port to run the server on | | ||
| version | String | Current version of npm package | false | Version of Lunalytics, used to apply migrations scripts for database | | ||
|
||
### Example config.json | ||
|
||
```json | ||
{ | ||
"jwtSecret": "lunalyticsJwtSecretKeyHerePlease", | ||
"port": 2308, | ||
"database": { "name": "lunalytics" }, | ||
"isDemo": false, | ||
"cors": ["http://localhost:3000", "http://localhost:8080"], | ||
"version": "0.6.0" | ||
} | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// import dependencies | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
|
||
// import local files | ||
import logger from '../server/utils/logger.js'; | ||
import { loadJSON } from '../shared/parseJson.js'; | ||
const packageJson = loadJSON('../package.json'); | ||
|
||
const configExists = () => { | ||
const configPath = path.join(process.cwd(), 'config.json'); | ||
return fs.existsSync(configPath); | ||
}; | ||
|
||
if (configExists()) { | ||
logger.error('SETUP', { | ||
message: | ||
'Configuration file already exists. Please manually edit to overwrite or delete the file.', | ||
}); | ||
process.exit(0); | ||
} | ||
|
||
try { | ||
logger.info('SETUP', { message: 'Setting up application...' }); | ||
|
||
// write to config.json file | ||
const configPath = path.join(process.cwd(), 'config.json'); | ||
const config = { | ||
port: 2308, | ||
database: { name: 'lunalytics' }, | ||
jwtSecret: uuidv4(), | ||
migrationType: 'automatic', | ||
version: packageJson.version, | ||
}; | ||
|
||
fs.writeFileSync(configPath, JSON.stringify(config, null, 2)); | ||
|
||
logger.info('SETUP', { message: 'Application setup successfully.' }); | ||
|
||
process.exit(0); | ||
} catch (error) { | ||
logger.error('SETUP', { | ||
message: 'Unable to setup application. Please try again.', | ||
error: error.message, | ||
stack: error.stack, | ||
}); | ||
|
||
process.exit(1); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
import '../scripts/loadEnv.js'; | ||
|
||
// import dependencies | ||
import inquirer from 'inquirer'; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import fs from 'fs'; | ||
import logger from './logger.js'; | ||
|
||
class Config { | ||
constructor() { | ||
this.configPath = `${process.cwd()}/config.json`; | ||
this.config = {}; | ||
|
||
try { | ||
fs.watch(this.configPath, { persistent: false }, (eventType) => { | ||
if (eventType === 'change') { | ||
this.readConfigFile(); | ||
} | ||
}); | ||
} catch (error) { | ||
logger.error('CONFIG', { | ||
message: error?.message, | ||
stack: error?.stack, | ||
}); | ||
} | ||
|
||
logger.info('CONFIG', { message: 'Loading configuration...' }); | ||
|
||
this.readConfigFile(); | ||
} | ||
|
||
readConfigFile() { | ||
if (!fs.existsSync(this.configPath)) { | ||
logger.error('CONFIG', { | ||
message: | ||
'Configuration file not found. Please run "npm run setup" (or "yarn setup" or "pnpm setup") to create it.', | ||
}); | ||
process.exit(1); | ||
} | ||
|
||
const fileData = fs.readFileSync(this.configPath); | ||
|
||
try { | ||
this.config = JSON.parse(fileData); | ||
process.env.VITE_API_URL = `http://localhost:${this.config.port}`; | ||
|
||
if (process.env.NODE_ENV === 'test') { | ||
if (!this.config.database) this.config.database = {}; | ||
this.config.database.name = 'e2e-test'; | ||
|
||
logger.info('CONFIG', { | ||
message: 'Changed database name to "e2e-test" for testing purposes.', | ||
}); | ||
} | ||
|
||
logger.info('CONFIG', { | ||
message: 'Configuration has been setup successfully.', | ||
}); | ||
} catch (jsonError) { | ||
logger.error(`CONFIG`, { | ||
message: 'Unable to parse config file JSON', | ||
jsonError, | ||
}); | ||
} | ||
} | ||
|
||
get(key) { | ||
const value = this.config[key]; | ||
return value; | ||
} | ||
} | ||
|
||
const config = new Config(); | ||
|
||
export default config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters