Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Env vars config override and some fixes #277

Merged
merged 11 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ env:

jobs:
build:
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04

steps:
- name: Checkout repository
Expand All @@ -35,8 +35,8 @@ jobs:
type=ref,event=branch
type=ref,event=pr
type=raw,value={{branch}}-{{sha}}-{{date 'X'}},enable=${{ startsWith(github.ref, 'refs/heads') }}
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern=v{{version}}
type=semver,pattern=v{{major}}.{{minor}}
- name: Build and push image
uses: docker/build-push-action@v3
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile.prod
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ COPY --from=build /usr/src/app/public ./public

ENV NODE_ENV=production

CMD ["node", "dist/backend/server.js"]
CMD ["node", "dist/backend/app.js"]
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,16 @@
},
"dependencies": {
"@aws-sdk/client-s3": "^3.181.0",
"@codex-team/config-loader": "0.0.1-alpha.2",
"@codex-team/config-loader": "0.1.0-rc1",
"@codexteam/shortcuts": "^1.2.0",
"@hawk.so/javascript": "^3.0.1",
"@hawk.so/nodejs": "^3.1.4",
"@types/multer-s3": "^3.0.0",
"@types/yargs": "^17.0.13",
"arg": "^5.0.2",
"config": "^3.3.6",
"cookie-parser": "^1.4.5",
"csurf": "^1.11.0",
"debug": "^4.3.2",
"dotenv": "^16.0.0",
"express": "^4.17.1",
"file-type": "^16.5.4",
"fs-extra": "^10.1.0",
Expand Down
6 changes: 3 additions & 3 deletions src/backend/routes/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/backend/routes/middlewares/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
5 changes: 4 additions & 1 deletion src/backend/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
36 changes: 34 additions & 2 deletions src/backend/utils/appConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
});

/**
Expand Down Expand Up @@ -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
Expand All @@ -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',
Expand All @@ -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);

Expand Down
21 changes: 5 additions & 16 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1809,12 +1809,11 @@
"@babel/helper-validator-identifier" "^7.19.1"
to-fast-properties "^2.0.0"

"@codex-team/config-loader@0.0.1-alpha.2":
version "0.0.1-alpha.2"
resolved "https://registry.yarnpkg.com/@codex-team/config-loader/-/config-loader-0.0.1-alpha.2.tgz#1852feef1cb7bea2bd530fd55a0a1a7d8ab75cef"
integrity sha512-RNf53ttDwOUCKaMfJM/X4y7+gYtzHmmIQ0np9W2chpFxhFdNJxZB6SPF3y3Et7Xe6damOrGl5P46+W5R/5iToA==
"@codex-team/config-loader@0.1.0-rc1":
version "0.1.0-rc1"
resolved "https://registry.yarnpkg.com/@codex-team/config-loader/-/config-loader-0.1.0-rc1.tgz#f4adf2553e97933b029982622ed29ef667cded3f"
integrity sha512-dHII0e2L3QsSs77zn1KLz+PIuVCYTqSUPAPgk4UiT5MUA1lNi/6smJ5A7+QEcbBnKaHVmRtvhHGR9ahfJ5ZhIQ==
dependencies:
eslint-plugin-n "^15.2.5"
js-yaml "^4.1.0"
lodash.isarray "^4.0.0"
lodash.merge "^4.6.2"
Expand Down Expand Up @@ -3299,12 +3298,6 @@ concurrently@^7.1.0:
tree-kill "^1.2.2"
yargs "^17.3.1"

config@^3.3.6:
version "3.3.7"
resolved "https://registry.yarnpkg.com/config/-/config-3.3.7.tgz#4310410dc2bf4e0effdca21a12a4035860a24ee4"
dependencies:
json5 "^2.1.1"

[email protected]:
version "0.5.4"
resolved "https://registry.yarnpkg.com/content-disposition/-/content-disposition-0.5.4.tgz#8b82b4efac82512a02bb0b1dcec9d2c5e8eb5bfe"
Expand Down Expand Up @@ -3718,10 +3711,6 @@ domutils@^3.0.1:
domelementtype "^2.3.0"
domhandler "^5.0.1"

dotenv@^16.0.0:
version "16.0.1"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.0.1.tgz#8f8f9d94876c35dac989876a5d3a82a267fdce1d"

[email protected]:
version "1.0.11"
resolved "https://registry.yarnpkg.com/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz#ae0f0fa2d85045ef14a817daa3ce9acd0489e5bf"
Expand Down Expand Up @@ -4969,7 +4958,7 @@ json5@^1.0.1:
dependencies:
minimist "^1.2.0"

json5@^2.1.1, json5@^2.1.2, json5@^2.2.1:
json5@^2.1.2, json5@^2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.1.tgz#655d50ed1e6f95ad1a3caababd2b0efda10b395c"

Expand Down