Merge pull request #152 from Kingsrook/feature/migrate-sample-app-to-new-javalin-server

Feature/migrate sample app to new javalin server
This commit is contained in:
2025-01-06 08:50:06 -06:00
committed by GitHub
5 changed files with 35 additions and 64 deletions

View File

@ -38,6 +38,7 @@ import com.kingsrook.qqq.middleware.javalin.specs.v1.MiddlewareVersionV1;
import io.javalin.Javalin;
import io.javalin.http.Context;
import org.apache.commons.lang.BooleanUtils;
import org.eclipse.jetty.util.resource.Resource;
/*******************************************************************************
@ -102,17 +103,23 @@ public class QApplicationJavalinServer
{
////////////////////////////////////////////////////////////////////////////////////////
// If you have any assets to add to the web server (e.g., logos, icons) place them at //
// src/main/resources/material-dashboard-overlay (or a directory of your choice //
// under src/main/resources) and use this line of code to tell javalin about it. //
// Make sure to add your app-specific directory to the javalin config before the core //
// material-dashboard directory, so in case the same file exists in both (e.g., //
// favicon.png), the app-specific one will be used. //
// src/main/resources/material-dashboard-overlay //
// we'll use the same check that javalin (jetty?) internally uses to see if this //
// directory exists - because if it doesn't, then it'll fail to start the server... //
// note that that Resource object is auto-closable, hence the try-with-resources //
////////////////////////////////////////////////////////////////////////////////////////
config.staticFiles.add("/material-dashboard-overlay");
try(Resource resource = Resource.newClassPathResource("/material-dashboard-overlay"))
{
if(resource !=null)
{
config.staticFiles.add("/material-dashboard-overlay");
}
}
/////////////////////////////////////////////////////////////////////
// tell javalin where to find material-dashboard static web assets //
/////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
// tell javalin where to find material-dashboard static web assets //
// in this case, this path is coming from the qqq-frontend-material-dashboard jar //
////////////////////////////////////////////////////////////////////////////////////
config.staticFiles.add("/material-dashboard");
////////////////////////////////////////////////////////////

View File

@ -23,10 +23,8 @@ package com.kingsrook.sampleapp;
import com.kingsrook.qqq.backend.core.logging.QLogger;
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
import com.kingsrook.qqq.backend.javalin.QJavalinImplementation;
import com.kingsrook.qqq.middleware.javalin.QApplicationJavalinServer;
import com.kingsrook.sampleapp.metadata.SampleMetaDataProvider;
import io.javalin.Javalin;
/*******************************************************************************
@ -36,12 +34,6 @@ public class SampleJavalinServer
{
private static final QLogger LOG = QLogger.getLogger(SampleJavalinServer.class);
private static final int PORT = 8000;
private QInstance qInstance;
private Javalin javalinService;
/*******************************************************************************
@ -49,7 +41,7 @@ public class SampleJavalinServer
*******************************************************************************/
public static void main(String[] args)
{
new SampleJavalinServer().startJavalinServer();
new SampleJavalinServer().start();
}
@ -57,38 +49,11 @@ public class SampleJavalinServer
/*******************************************************************************
**
*******************************************************************************/
public void startJavalinServer()
public void start()
{
try
{
qInstance = SampleMetaDataProvider.defineInstance();
QJavalinImplementation qJavalinImplementation = new QJavalinImplementation(qInstance);
javalinService = Javalin.create(config ->
{
config.router.apiBuilder(qJavalinImplementation.getRoutes());
// todo - not all?
config.bundledPlugins.enableCors(cors -> cors.addRule(corsRule -> corsRule.anyHost()));
}).start(PORT);
/////////////////////////////////////////////////////////////////
// set the server to hot-swap the q instance before all routes //
/////////////////////////////////////////////////////////////////
QJavalinImplementation.setQInstanceHotSwapSupplier(() ->
{
try
{
return (SampleMetaDataProvider.defineInstance());
}
catch(Exception e)
{
LOG.warn("Error hot-swapping meta data", e);
return (null);
}
});
javalinService.before(QJavalinImplementation::hotSwapQInstance);
javalinService.after(ctx -> ctx.res().setHeader("Access-Control-Allow-Origin", "http://localhost:3000"));
new QApplicationJavalinServer(new SampleMetaDataProvider()).start();
}
catch(Exception e)
{
@ -96,16 +61,4 @@ public class SampleJavalinServer
}
}
/*******************************************************************************
**
*******************************************************************************/
public void stopJavalinServer()
{
if(javalinService != null)
{
javalinService.stop();
}
}
}

View File

@ -31,6 +31,7 @@ import com.kingsrook.qqq.backend.core.actions.dashboard.widgets.QuickSightChartR
import com.kingsrook.qqq.backend.core.actions.processes.BackendStep;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.exceptions.QValueException;
import com.kingsrook.qqq.backend.core.instances.AbstractQQQApplication;
import com.kingsrook.qqq.backend.core.instances.QInstanceEnricher;
import com.kingsrook.qqq.backend.core.instances.QMetaDataVariableInterpreter;
import com.kingsrook.qqq.backend.core.model.actions.processes.RunBackendStepInput;
@ -78,7 +79,7 @@ import com.kingsrook.sampleapp.processes.clonepeople.ClonePeopleTransformStep;
/*******************************************************************************
**
*******************************************************************************/
public class SampleMetaDataProvider
public class SampleMetaDataProvider extends AbstractQQQApplication
{
public static boolean USE_MYSQL = true;
@ -108,6 +109,17 @@ public class SampleMetaDataProvider
/***************************************************************************
**
***************************************************************************/
@Override
public QInstance defineQInstance() throws QException
{
return (defineInstance());
}
/*******************************************************************************
**
*******************************************************************************/
@ -145,7 +157,7 @@ public class SampleMetaDataProvider
private static void defineBranding(QInstance qInstance)
{
qInstance.setBranding(new QBrandingMetaData()
.withLogo("/kr-logo.png")
.withLogo("/samples-logo.png")
.withIcon("/kr-icon.png"));
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 129 KiB

View File

@ -38,8 +38,7 @@ class SampleJavalinServerTest
void testStartStop()
{
SampleJavalinServer sampleJavalinServer = new SampleJavalinServer();
sampleJavalinServer.startJavalinServer();
sampleJavalinServer.stopJavalinServer();
sampleJavalinServer.start();
}
}