Skip to content

Commit

Permalink
feat(editor): dev mode (#1432)
Browse files Browse the repository at this point in the history
  • Loading branch information
sr258 authored Apr 10, 2021
1 parent d5e31af commit d6035e5
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ microsoft.gpg
# Dependency directory
node_modules
/h5p

h5p_libs
25 changes: 12 additions & 13 deletions server/src/boot/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ const increaseMaxFileSize = async (config: H5P.IH5PConfig) => {

export default async (
serverConfig: IServerConfig,
browserWindow: electron.BrowserWindow
browserWindow: electron.BrowserWindow,
options?: {
devMode?: boolean;
libraryDir?: string;
}
) => {
const config = await new H5P.H5PConfig(
new H5P.fsImplementations.JsonStorage(serverConfig.configFile)
Expand All @@ -50,22 +54,17 @@ export default async (

const translationFunction = await boot_i18n(serverConfig);

// The H5PEditor object is central to all operations of @lumieducation/h5p-server
// if you want to user the editor component.
//
// To create the H5PEditor object, we call a helper function, which
// uses implementations of the storage classes with a local filesystem
// or a MongoDB/S3 backend, depending on the configuration values set
// in the environment variables.
// In your implementation, you will probably instantiate H5PEditor by
// calling new H5P.H5PEditor(...) or by using the convenience function
// H5P.fs(...).
// The H5PEditor object is central to all operations of
// @lumieducation/h5p-server if you want to user the editor component.
const h5pEditor: H5P.H5PEditor = await createH5PEditor(
config,
serverConfig.librariesPath, // the path on the local disc where libraries should be stored)
options?.libraryDir ?? serverConfig.librariesPath, // the path on the local disc where libraries should be stored)
serverConfig.workingCachePath, // the path on the local disc where content is stored. Only used / necessary if you use the local filesystem content storage class.
serverConfig.temporaryStoragePath, // the path on the local disc where temporary files (uploads) should be stored. Only used / necessary if you use the local filesystem temporary storage class.
(key, language) => translationFunction(key, { lng: language })
(key, language) => translationFunction(key, { lng: language }),
{
disableLibraryCache: options?.devMode
}
);

h5pEditor.setRenderer((model) => model);
Expand Down
15 changes: 11 additions & 4 deletions server/src/boot/createH5PEditor.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from 'path';
import * as H5P from '@lumieducation/h5p-server';

/**
Expand All @@ -20,16 +21,22 @@ export default async function createH5PEditor(
localLibraryPath: string,
localContentPath?: string,
localTemporaryPath?: string,
translationCallback?: H5P.ITranslationFunction
translationCallback?: H5P.ITranslationFunction,
options?: {
disableLibraryCache?: boolean;
}
): Promise<H5P.H5PEditor> {
const libStorage = new H5P.fsImplementations.FileLibraryStorage(
localLibraryPath
);
// Depending on the environment variables we use different implementations
// of the storage interfaces.
const h5pEditor = new H5P.H5PEditor(
new H5P.fsImplementations.InMemoryStorage(), // this is a general-purpose cache
config,
new H5P.cacheImplementations.CachedLibraryStorage(
new H5P.fsImplementations.FileLibraryStorage(localLibraryPath)
),
options?.disableLibraryCache
? libStorage
: new H5P.cacheImplementations.CachedLibraryStorage(libStorage),
new H5P.fsImplementations.FileContentStorage(localContentPath),
new H5P.fsImplementations.DirectoryTemporaryFileStorage(
localTemporaryPath
Expand Down
14 changes: 12 additions & 2 deletions server/src/httpServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,22 @@ const log = new Logger('boot');
let server: http.Server;

export { server };

/**
* options.devMode: true if the app should be used to develop libraries
* options.libraryDir: a directory in the filesystem at which libraries should
* be stored
*/
export default async (
serverConfig: IServerConfig,
browserWindow: electron.BrowserWindow
browserWindow: electron.BrowserWindow,
options?: {
devMode?: boolean;
libraryDir?: string;
}
) => {
await setup(serverConfig);
const app = appFactory(serverConfig, browserWindow);
const app = appFactory(serverConfig, browserWindow, options);
server = http.createServer(await app);
return server.listen(process.env.PORT || 0, () => {
log.info(`server booted on port ${(server.address() as any).port}`);
Expand Down
6 changes: 5 additions & 1 deletion server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@ app.on('ready', async () => {
log.info('app is ready');
const server = await httpServerFactory(
serverConfigFactory(process.env.USERDATA || app.getPath('userData')),
mainWindow
mainWindow,
{
devMode: app.commandLine.hasSwitch('dev'),
libraryDir: app.commandLine.getSwitchValue('libs')
}
);
log.info('server booted');

Expand Down

0 comments on commit d6035e5

Please sign in to comment.