move public config to server

This commit is contained in:
Pierre Noël 2022-02-15 14:20:25 +01:00
parent 7576b81d1f
commit fb843f44f6
11 changed files with 6072 additions and 941 deletions

View File

@ -50,7 +50,7 @@ const LIFECYCLES = {
class Strapi { class Strapi {
constructor(opts = {}) { constructor(opts = {}) {
destroyOnSignal(this); destroyOnSignal(this);
this.dirs = utils.getDirs(opts.dir || process.cwd()); this.dirs = utils.getDirs(opts.dir || process.cwd(), { strapi: this });
const appConfig = loadConfiguration(this.dirs.root, opts); const appConfig = loadConfiguration(this.dirs.root, opts);
this.container = createContainer(this); this.container = createContainer(this);
this.container.register('config', createConfigProvider(appConfig)); this.container.register('config', createConfigProvider(appConfig));

View File

@ -6,6 +6,7 @@ const fs = require('fs-extra');
const chokidar = require('chokidar'); const chokidar = require('chokidar');
const execa = require('execa'); const execa = require('execa');
const { getOr } = require('lodash/fp'); const { getOr } = require('lodash/fp');
const urlJoin = require('url-join');
const { createLogger } = require('@strapi/logger'); const { createLogger } = require('@strapi/logger');
const loadConfiguration = require('../core/app-configuration'); const loadConfiguration = require('../core/app-configuration');
@ -131,6 +132,8 @@ function watchFileChanges({ dir, strapiInstance, watchIgnoreFiles, polling }) {
'**/index.html', '**/index.html',
'**/public', '**/public',
'**/public/**', '**/public/**',
strapiInstance.config.get('server.public.path'),
urlJoin(strapiInstance.config.get('server.public.path'), '**'),
'**/*.db*', '**/*.db*',
'**/exports/**', '**/exports/**',
...watchIgnoreFiles, ...watchIgnoreFiles,

View File

@ -21,6 +21,7 @@ const defaultConfig = {
proxy: false, proxy: false,
cron: { enabled: false }, cron: { enabled: false },
admin: { autoOpen: false }, admin: { autoOpen: false },
public: { path: './public' },
}, },
admin: {}, admin: {},
api: { api: {
@ -55,5 +56,9 @@ module.exports = (dir, initialConfig = {}) => {
const envDir = path.resolve(configDir, 'env', process.env.NODE_ENV); const envDir = path.resolve(configDir, 'env', process.env.NODE_ENV);
const envConfig = loadConfigDir(envDir); const envConfig = loadConfigDir(envDir);
return _.merge(rootConfig, defaultConfig, baseConfig, envConfig); const config = _.merge(rootConfig, defaultConfig, baseConfig, envConfig);
config.server.public.path = path.resolve(dir, config.server.public.path);
return config;
}; };

View File

@ -1,6 +1,7 @@
'use strict'; 'use strict';
const { getConfigUrls } = require('@strapi/utils'); const { getConfigUrls } = require('@strapi/utils');
const fse = require('fs-extra');
module.exports = function({ strapi }) { module.exports = function({ strapi }) {
strapi.config.port = strapi.config.get('server.port') || strapi.config.port; strapi.config.port = strapi.config.get('server.port') || strapi.config.port;
@ -22,4 +23,7 @@ module.exports = function({ strapi }) {
if (!shouldServeAdmin) { if (!shouldServeAdmin) {
strapi.config.serveAdminPanel = false; strapi.config.serveAdminPanel = false;
} }
// ensure public repository exists
fse.ensureDir(strapi.config.get('server.public.path'));
}; };

View File

@ -80,7 +80,7 @@ module.exports = (config, { strapi }) => {
{ {
method: 'GET', method: 'GET',
path: '/(.*)', path: '/(.*)',
handler: koaStatic(strapi.dirs.public, { handler: koaStatic(strapi.config.get('server.public.path'), {
maxage: maxAge, maxage: maxAge,
defer: true, defer: true,
}), }),

View File

@ -2,7 +2,7 @@
const { join } = require('path'); const { join } = require('path');
const getDirs = root => ({ const getDirs = (root, { strapi }) => ({
root, root,
src: join(root, 'src'), src: join(root, 'src'),
api: join(root, 'src', 'api'), api: join(root, 'src', 'api'),
@ -11,7 +11,13 @@ const getDirs = root => ({
policies: join(root, 'src', 'policies'), policies: join(root, 'src', 'policies'),
middlewares: join(root, 'src', 'middlewares'), middlewares: join(root, 'src', 'middlewares'),
config: join(root, 'config'), config: join(root, 'config'),
public: join(root, 'public'), get public() {
// TODO V5: to be removed
process.emitWarning(
`[Deprecated] strapi.dirs.public will not exist in future versions. Prefere using strapi.config.get('server.public.path') instead.`
);
return strapi.config.get('server.public.path');
},
}); });
module.exports = getDirs; module.exports = getDirs;

View File

@ -127,6 +127,7 @@
"resolve-cwd": "3.0.0", "resolve-cwd": "3.0.0",
"semver": "7.3.5", "semver": "7.3.5",
"statuses": "2.0.1", "statuses": "2.0.1",
"url-join": "4.0.1",
"uuid": "^3.3.2" "uuid": "^3.3.2"
}, },
"devDependencies": { "devDependencies": {

View File

@ -25,7 +25,10 @@ module.exports = ({ strapi }) => {
{ {
method: 'GET', method: 'GET',
path: '/uploads/(.*)', path: '/uploads/(.*)',
handler: [range, koaStatic(strapi.dirs.public, { defer: true, ...localServerConfig })], handler: [
range,
koaStatic(strapi.config.get('server.public.path'), { defer: true, ...localServerConfig }),
],
config: { auth: false }, config: { auth: false },
}, },
]); ]);

View File

@ -7,8 +7,11 @@
// Public node modules. // Public node modules.
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const fse = require('fs-extra');
const { PayloadTooLargeError } = require('@strapi/utils').errors; const { PayloadTooLargeError } = require('@strapi/utils').errors;
const UPLOADS_FOLDER_NAME = 'uploads';
module.exports = { module.exports = {
init({ sizeLimit = 1000000 } = {}) { init({ sizeLimit = 1000000 } = {}) {
const verifySize = file => { const verifySize = file => {
@ -17,7 +20,11 @@ module.exports = {
} }
}; };
const publicDir = strapi.dirs.public; const publicDir = strapi.config.get('server.public.path');
// Ensure uploads folder exists
const uploadPath = path.resolve(publicDir, UPLOADS_FOLDER_NAME);
fse.ensureDirSync(uploadPath);
return { return {
upload(file) { upload(file) {
@ -25,19 +32,15 @@ module.exports = {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// write file in public/assets folder // write file in public/assets folder
fs.writeFile( fs.writeFile(path.join(uploadPath, `${file.hash}${file.ext}`), file.buffer, err => {
path.join(publicDir, `/uploads/${file.hash}${file.ext}`), if (err) {
file.buffer, return reject(err);
err => {
if (err) {
return reject(err);
}
file.url = `/uploads/${file.hash}${file.ext}`;
resolve();
} }
);
file.url = `/${UPLOADS_FOLDER_NAME}/${file.hash}${file.ext}`;
resolve();
});
}); });
}, },
delete(file) { delete(file) {

View File

@ -35,7 +35,8 @@
"test": "echo \"no tests yet\"" "test": "echo \"no tests yet\""
}, },
"dependencies": { "dependencies": {
"@strapi/utils": "4.1.0" "@strapi/utils": "4.1.0",
"fs-extra": "10.0.0"
}, },
"engines": { "engines": {
"node": ">=12.22.0 <=16.x.x", "node": ">=12.22.0 <=16.x.x",

6947
yarn.lock

File diff suppressed because it is too large Load Diff