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

O3-3438: Update SPA module to load the bundled frontend #62

Merged
merged 1 commit into from
Aug 16, 2024

Conversation

wikumChamith
Copy link
Member

This enables the SPA module to serve the front end from a bundled .war file.

Ticket: https://openmrs.atlassian.net/browse/O3-3438

@wikumChamith wikumChamith requested a review from ibacher August 3, 2024 11:12
Copy link
Member

@ibacher ibacher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @wikumChamith! A few thoughts on how to proceed.

Comment on lines 95 to 96
File file = new File(openmrsRootPath, "WEB-INF" + File.separator + spaDirectory);
SpaDirectoryResolver.spaDirectory.set(Paths.get(file.toURI()).normalize().toAbsolutePath().toString());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we using both File and Paths here? Just use one or the other (Paths.get(openmrsRootPath, 'WEB-INF', spaDirectory).toAbsolutePath().toString())


public SpaDirectoryResolver(ServletContext servletContext) {
this.servletContext = servletContext;
if (isBundledFrontend()){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kind of forces things to use the bundled frontend if one exists. I think a better solution here would be:

  1. At module start, check to see if the local directory exists
  2. If it does, there's no work to do
  3. If it doesn't and there is a bundled frontend, extract it to the local directory

That way all the work is done in the Activator.

@wikumChamith
Copy link
Member Author

@ibacher I updated this PR.

@wikumChamith wikumChamith requested a review from ibacher August 7, 2024 06:08
@@ -75,6 +85,12 @@ private static void resolveDirectory(String spaDirectory) {
return;
}

if(spaDirectory.equals(BUNDLED_FRONTEND_DIRECTORY)){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why go through all of this when, in the Activator we can just set the GP to the resolved path to the bundled frontend directory?

@Override
public void started() {
log.info("SPA module started");
spaDirectoryResolver = new SpaDirectoryResolver();
File indexHtml = new File(OpenmrsUtil.getApplicationDataDirectory() + File.separator + SpaConstants.DEFAULT_FRONTEND_DIRECTORY, "index.html");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code should be moved to the contextRefreshed() hook instead of the started() hook, like in the OWA Activator.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, it seems like we could just do:

SpaDirectoryResolver directoryResolver = new SpaDirectoryResolver();
if (!Paths.get(directoryResolver.getSpaDirectory(), "index.html").toFile().exists()) {
  Path bundledFrontend = Paths.get(servletContext.getRealPath("/"), "WEB-INF", BUNDLED_FRONTEND_DIRECTORY);
  if (bundledFrontend.resolve("index.html").toFile().exist()) {
    // activator methods are always run in the daemon context, so it has all privileges
    Context.getAdministrationService().setGlobalProperty(GP_LOCAL_DIRECTORY, bundledFrontend.toAbsolutePath().toString());
  }
}

This would then only require the addition of the SpaActivator as a bean and all the changes to the SpaDirectoryResolver can be removed.

@wikumChamith wikumChamith force-pushed the O3-3438 branch 2 times, most recently from 50c573e to 1db7ddb Compare August 16, 2024 09:37
@wikumChamith
Copy link
Member Author

@ibacher I updated the PR.

@wikumChamith wikumChamith requested a review from ibacher August 16, 2024 09:38
Copy link
Member

@ibacher ibacher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @wikumChamith! Moving in the right direction here, but still looks like we need some changes. Have you tried this code out?

@Override
public void started() {
log.info("SPA module started");
spaDirectoryResolver = new SpaDirectoryResolver();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should still set this value. Otherwise, the call to add the GP listener won't work.

if (bundledFrontend.resolve("index.html").toFile().exists()) {
Context.getAdministrationService().setGlobalProperty(GP_LOCAL_DIRECTORY, bundledFrontend.toAbsolutePath().toString());
}
}
Context.getAdministrationService().addGlobalPropertyListener(spaDirectoryResolver);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code should probably have a guard if it's not in started():

if (spaDirectoryResolver != null) {
	Context.getAdministrationService().addGlobalPropertyListener(spaDirectoryResolver);
}

This is because contextRefreshed() is called before started() IIRC.

@wikumChamith
Copy link
Member Author

@ibacher, I made a mistake while testing the PR before and didn't notice an error. I updated the PR to fix it.

@wikumChamith wikumChamith requested a review from ibacher August 16, 2024 13:23
Copy link
Member

@ibacher ibacher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Thanks!

@ibacher ibacher merged commit 780f92e into openmrs:master Aug 16, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants