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
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
27 changes: 26 additions & 1 deletion omod/src/main/java/org/openmrs/module/spa/SpaActivator.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,49 @@
import org.openmrs.api.GlobalPropertyListener;
import org.openmrs.api.context.Context;
import org.openmrs.module.BaseModuleActivator;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;
import java.nio.file.Path;
import java.nio.file.Paths;

import static org.openmrs.module.spa.SpaConstants.BUNDLED_FRONTEND_DIRECTORY;
import static org.openmrs.module.spa.SpaConstants.GP_LOCAL_DIRECTORY;

@Slf4j
public class SpaActivator extends BaseModuleActivator {
public class SpaActivator extends BaseModuleActivator implements ServletContextAware {
ibacher marked this conversation as resolved.
Show resolved Hide resolved

// here so we can register the listener on load and de-register it when stopped
private GlobalPropertyListener spaDirectoryResolver = null;

public static ServletContext servletContext;
ibacher marked this conversation as resolved.
Show resolved Hide resolved

@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.

Context.getAdministrationService().addGlobalPropertyListener(spaDirectoryResolver);
}

@Override
public void contextRefreshed() {
if (!Paths.get(SpaDirectoryResolver.getSpaDirectory(), "index.html").toFile().exists()) {
Path bundledFrontend = Paths.get(servletContext.getRealPath("/"), "WEB-INF", BUNDLED_FRONTEND_DIRECTORY);
if (bundledFrontend.resolve("index.html").toFile().exists()) {
Context.getAdministrationService().setGlobalProperty(GP_LOCAL_DIRECTORY, bundledFrontend.toAbsolutePath().toString());
}
}
}

@Override
public void stopped() {
Context.getAdministrationService().removeGlobalPropertyListener(spaDirectoryResolver);
spaDirectoryResolver = null;
log.info("SPA module stopped");
}

@Override
public void setServletContext(ServletContext servletContext) {
SpaActivator.servletContext = servletContext;
}
}
2 changes: 2 additions & 0 deletions omod/src/main/java/org/openmrs/module/spa/SpaConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ private SpaConstants() {}

public static final String DEFAULT_FRONTEND_DIRECTORY = "frontend";

public static final String BUNDLED_FRONTEND_DIRECTORY = "bundledFrontend";

public static final String GP_LOCAL_DIRECTORY = "spa.local.directory";
}

Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ private static void resolveDirectory(String spaDirectory) {
SpaDirectoryResolver.spaDirectory.set(Paths.get(OpenmrsUtil.getApplicationDataDirectory(), DEFAULT_FRONTEND_DIRECTORY).normalize().toAbsolutePath().toString());
}
SpaDirectoryResolver.spaDirectory.set(spaDirectoryPath.normalize().toString());
} else {
SpaDirectoryResolver.spaDirectory.set(spaDirectory);
ibacher marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
1 change: 1 addition & 0 deletions omod/src/main/resources/webModuleApplicationContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

<context:annotation-config />
<bean id="org.openmrs.module.spa.SpaActivator" class="org.openmrs.module.spa.SpaActivator" />
<bean id="spaResourceLoader" class="org.openmrs.module.spa.SpaResourceLoader" />
<bean class="org.openmrs.module.spa.SpaController" />
<bean class="org.openmrs.module.spa.SpaResourceConverter" />
Expand Down
Loading