1
0
Fork 0
mirror of https://github.com/codex-team/codex.docs.git synced 2025-07-28 01:29:42 +02:00

Env vars config override and some fixes (#277)

* update config-loader and remove unnecessary packages

* implement default config && move password to auth section

* add 'v' to the start of image name

* fix dockerfile

* test

* test pipeline

* test again

* fixes

* remove test step

* remove console log

* fix default config
This commit is contained in:
Nikita Melnikov 2022-11-03 14:38:13 +04:00 committed by GitHub
parent b67717c8e5
commit ccbd79d6fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 53 additions and 31 deletions

View file

@ -22,7 +22,7 @@ router.get('/auth', csrfProtection, function (req: Request, res: Response) {
*/
router.post('/auth', parseForm, csrfProtection, async (req: Request, res: Response) => {
try {
if (!appConfig.password) {
if (!appConfig.auth.password) {
res.render('auth', {
title: 'Login page',
header: 'Password not set',
@ -32,7 +32,7 @@ router.post('/auth', parseForm, csrfProtection, async (req: Request, res: Respon
return;
}
if (req.body.password !== appConfig.password) {
if (req.body.password !== appConfig.auth.password) {
res.render('auth', {
title: 'Login page',
header: 'Wrong password',
@ -46,7 +46,7 @@ router.post('/auth', parseForm, csrfProtection, async (req: Request, res: Respon
iss: 'Codex Team',
sub: 'auth',
iat: Date.now(),
}, appConfig.password + appConfig.auth.secret);
}, appConfig.auth.password + appConfig.auth.secret);
res.cookie('authToken', token, {
httpOnly: true,

View file

@ -14,14 +14,14 @@ export default async function verifyToken(req: Request, res: Response, next: Nex
const token = req.cookies.authToken;
try {
if (!appConfig.password) {
if (!appConfig.auth.password) {
res.locals.isAuthorized = false;
next();
return;
}
const decodedToken = jwt.verify(token, appConfig.password + appConfig.auth.secret);
const decodedToken = jwt.verify(token, appConfig.auth.password + appConfig.auth.secret);
res.locals.isAuthorized = !!decodedToken;

View file

@ -34,6 +34,7 @@ function createApp(): express.Express {
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const cwd = process.cwd();
const app = express();
const localConfig = appConfig.frontend;
@ -85,7 +86,9 @@ function createApp(): express.Express {
app.use(express.static(path.join(__dirname, '../../public')));
if (appConfig.uploads.driver === 'local') {
app.use('/uploads', express.static(appConfig.uploads.local.path));
const uploadsPath = path.join(cwd, appConfig.uploads.local.path);
app.use('/uploads', express.static(uploadsPath));
}
app.use('/favicon', express.static(downloadedFaviconFolder));

View file

@ -65,6 +65,7 @@ const MongoDatabaseConfig = z.object({
*/
const AuthConfig = z.object({
secret: z.string(), // Secret for JWT
password: z.string(), // Password for admin panel
});
/**
@ -103,7 +104,6 @@ const AppConfig = z.object({
favicon: z.string().optional(), // Path or URL to favicon
uploads: z.union([LocalUploadsConfig, S3UploadsConfig]), // Uploads configuration
hawk: HawkConfig.optional().nullable(), // Hawk configuration
password: z.string(), // Password for admin panel
frontend: FrontendConfig, // Frontend configuration
auth: AuthConfig, // Auth configuration
database: z.union([LocalDatabaseConfig, MongoDatabaseConfig]), // Database configuration
@ -112,6 +112,38 @@ const AppConfig = z.object({
export type AppConfig = z.infer<typeof AppConfig>;
const defaultConfig: AppConfig = {
'port': 3000,
'host': 'localhost',
'uploads': {
'driver': 'local',
'local': {
'path': './uploads',
},
},
'frontend': {
'title': 'CodeX Docs',
'description': 'Free Docs app powered by Editor.js ecosystem',
'startPage': '',
'carbon': {
'serve': '',
'placement': '',
},
'menu': [],
},
'auth': {
'secret': 'supersecret',
'password': 'secretpassword',
},
'hawk': null,
'database': {
'driver': 'local',
'local': {
'path': './db',
},
},
};
const args = arg({ /* eslint-disable @typescript-eslint/naming-convention */
'--config': [ String ],
'-c': '--config',
@ -126,7 +158,7 @@ const paths = (args['--config'] || [ './docs-config.yaml' ]).map((configPath) =>
return path.join(cwd, configPath);
});
const loadedConfig = loadConfig<AppConfig>(...paths);
const loadedConfig = loadConfig(...[defaultConfig, ...paths]);
const appConfig = AppConfig.parse(loadedConfig);