Skip to content

Commit

Permalink
O3-3438: Update SPA module to load the bundled frontend (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
wikumChamith authored Aug 16, 2024
1 parent 490ad71 commit 780f92e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
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 {

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

public static ServletContext servletContext;

@Override
public void started() {
log.info("SPA module started");
spaDirectoryResolver = new SpaDirectoryResolver();
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);
}
}
}
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

0 comments on commit 780f92e

Please sign in to comment.