Skip to content

Commit

Permalink
remove s3 and basic sub-menu
Browse files Browse the repository at this point in the history
  • Loading branch information
eviltester committed Apr 11, 2024
1 parent 8f71d4b commit 492db22
Show file tree
Hide file tree
Showing 16 changed files with 234 additions and 73 deletions.
2 changes: 2 additions & 0 deletions challenger/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
<!-- todo: split into two apps, one aws enabled, one with local persistance only -->
<!-- 180 meg!
-->
<!--
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.825</version>
</dependency>
-->

<dependency>
<groupId>io.github.classgraph</groupId>
Expand Down
6 changes: 6 additions & 0 deletions challenger/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ You can also `GET http://localhost:4567/challenges` to get the list of challenge

## Version Tracking

20240411

- removed the S3 integration, so this is a breaking change for some people
- added the simple api in its own api path
- ui to surface all modes and sub info

v1.1 - 20240128

- in multiuser mode now all the user sessions are independent with a limit of 100 todos
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ public static void main(String[] args) {
}

thingifier.apiConfig().setApiToAllowRobotsIndexingResponses(false);
thingifier.apiConfig().setSupportsMultipleDatabases(true);

// setup routes required for challenges
challenger = new ChallengeRouteHandler(thingifier, app.getApiDefn(), config);
challenger.configureRoutes();


app.chooseThingifier();
// can set profile by adding more configs, or just
Expand All @@ -92,7 +93,7 @@ public static void main(String[] args) {
);

challenger.setupGui(app.getGuiManagement());

challenger.configureRoutes();

if(challenger.isSinglePlayerMode()){
logger.info("Running in Single User Mode");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public class ChallengeRouteHandler {
private boolean single_player_mode;
PersistenceLayer persistenceLayer;
private boolean guiStayAlive=false; // when set gui makes a call every 5 mins to keep session alive,
// not needed when storing data
private DefaultGUIHTML guiTemplates;
// not needed when storing data

public ChallengeRouteHandler(Thingifier thingifier, ThingifierApiDocumentationDefn apiDefn, ChallengerConfig config){

Expand Down Expand Up @@ -65,6 +66,8 @@ public ChallengeRouteHandler(Thingifier thingifier, ThingifierApiDocumentationDe
enableAdminApi();
}

this.guiTemplates = new DefaultGUIHTML();


}

Expand All @@ -83,9 +86,9 @@ public ChallengeRouteHandler configureRoutes() {
new MirrorRoutes().configure(mirrorModeDocumentationDefn);

// Simulation routes should not show
new SimulationRoutes().configure();
new SimulationRoutes(guiTemplates).configure();

new SimpleApiRoutes().configure();
new SimpleApiRoutes(guiTemplates).configure();

return this;
}
Expand All @@ -104,6 +107,7 @@ public void addHooks(final ThingifierHttpApiRoutings restServer) {
}

public void setupGui(DefaultGUIHTML guiManagement) {
this.guiTemplates = guiManagement;
new ChallengerWebGUI(guiManagement, guiStayAlive).setup(challengers, challengeDefinitions,
persistenceLayer, single_player_mode);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package uk.co.compendiumdev.challenge.challenges;

import org.joda.time.Chronology;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ChallengeDefinitionData {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,52 @@ public void setup(final Challengers challengers,
guiManagement.appendMenuItem("API documentation","/docs");
guiManagement.appendMenuItem("Learning", "/learning");


String actualMenu = """
<div class="container cssmenu">
<nav aria-label="Site menu">
<div class="css-menu">
<ul class="sub-menu">
<li class="rootdropdownmenuitem"><a class="brand-link" href="/">Home</a></li>
<li class="rootdropdownmenuitem"><a href="/simpleapi/docs">Simple API</a>
<ul class="dropdownmenu">
<li class="dropdownmenuitem"><a href="/simpleapi/gui/entities">Simple API Data Explorer</a></li>
</ul>
</li>
<li class="rootdropdownmenuitem"><a href="/gui/challenges">API Challenges</a>
<ul class="dropdownmenu">
<li class="dropdownmenuitem"><a href="/docs">Challenges API Docs</a></li>
<li class="dropdownmenuitem"><a href="/gui/entities">Challenges Data Explorer</a></li>
</ul>
</li>
<li class="rootdropdownmenuitem"><a href="/learning">Learning Zone</a>
</li>
<li class="rootdropdownmenuitem"><a href="/practice-modes/simulation">Simulator</a>
<ul class="dropdownmenu">
<li class="dropdownmenuitem"><a href="/sim/docs">Sim API Docs</a></li>
<li class="dropdownmenuitem"><a href="/practice-modes/simulation/swagger">Sim Open API File</a></li>
<li class="dropdownmenuitem"><a href="/practice-modes/simulation">About API Simulator</a></li>
</ul>
</li>
<li class="rootdropdownmenuitem"><a href="/practice-modes/mirror">Mirror</a>
<ul class="dropdownmenu">
<li class="dropdownmenuitem"><a href="/practice-modes/mirror/swagger">Mirror Open API File</a></li>
<li class="dropdownmenuitem"><a href="/practice-modes/mirror">About HTTP Mirror</a></li>
</ul>
</li>
</ul>
</div>
</nav>
</div>
""".stripIndent();

guiManagement.setActualMenuHtml(actualMenu);

// Add the Default GUI Endpoiints for entity exploration


Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package uk.co.compendiumdev.challenge.persistence;

import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
import com.google.gson.Gson;
//import com.amazonaws.regions.Regions;
//import com.amazonaws.services.s3.AmazonS3;
//import com.amazonaws.services.s3.AmazonS3ClientBuilder;
//import com.amazonaws.services.s3.model.GetObjectRequest;
//import com.amazonaws.services.s3.model.S3Object;
//import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.co.compendiumdev.challenge.ChallengerAuthData;
Expand All @@ -19,7 +19,7 @@ public class AwsS3Storage implements ChallengerPersistenceMechanism {
private final String bucketName;

Logger logger = LoggerFactory.getLogger(AwsS3Storage.class);
static AmazonS3 s3Client;
//static AmazonS3 s3Client;

// to work we need environment variables for
// AWS_ACCESS_KEY_ID
Expand All @@ -38,65 +38,70 @@ public AwsS3Storage(boolean allowSave, boolean allowLoad, String awsBucket){
@Override
public PersistenceResponse saveChallengerStatus(final ChallengerAuthData data) {

// by default will not save to aws - need to add environment variable
if(!allowSave){
return new PersistenceResponse().withSuccess(false).withErrorMessage("AWS Configuration does not allow saving challenger status");
}

if(data==null){
return new PersistenceResponse().withSuccess(false).withErrorMessage("no data provided");
}


try{
ensureClientExists();

final String dataString = new Gson().toJson(data);
// Upload a text string as a new object.
s3Client.putObject(bucketName, data.getXChallenger(), dataString);
return new PersistenceResponse().withSuccess(true);
} catch (Exception e) {

logger.error("Error storing data to bucket for guid: {}", data.getXChallenger(), e);
return new PersistenceResponse().withSuccess(false).withErrorMessage("Error storing data to S3");
}
return new PersistenceResponse().withSuccess(false).withErrorMessage("AWS not supported by current build");

// // by default will not save to aws - need to add environment variable
// if(!allowSave){
// return new PersistenceResponse().withSuccess(false).withErrorMessage("AWS Configuration does not allow saving challenger status");
// }
//
// if(data==null){
// return new PersistenceResponse().withSuccess(false).withErrorMessage("no data provided");
// }
//
//
// try{
// ensureClientExists();
//
// final String dataString = new Gson().toJson(data);
// // Upload a text string as a new object.
// //s3Client.putObject(bucketName, data.getXChallenger(), dataString);
// return new PersistenceResponse().withSuccess(true);
// } catch (Exception e) {
//
// logger.error("Error storing data to bucket for guid: {}", data.getXChallenger(), e);
// return new PersistenceResponse().withSuccess(false).withErrorMessage("Error storing data to S3");
// }
}

private void ensureClientExists() {
if(s3Client!=null){
return;
}

// requires environment variables for
// AWS_ACCESS_KEY_ID
// AWS_SECRET_ACCESS_KEY
s3Client = AmazonS3ClientBuilder.standard()
.withRegion(Regions.US_EAST_2)
.build();
// if(s3Client!=null){
// return;
// }
//
// // requires environment variables for
// // AWS_ACCESS_KEY_ID
// // AWS_SECRET_ACCESS_KEY
// s3Client = AmazonS3ClientBuilder.standard()
// .withRegion(Regions.US_EAST_2)
// .build();

}

@Override
public PersistenceResponse loadChallengerStatus(final String guid) {

// by default will not save from aws - need to add environment variable
if(!allowLoad){
return new PersistenceResponse().withSuccess(false).withErrorMessage("AWS Configuration does not allow loading challenger status");
}

try {
ensureClientExists();

final S3Object fullObject = s3Client.getObject(new GetObjectRequest(bucketName, guid));
String dataString = getObjectContent(fullObject.getObjectContent());
return new PersistenceResponse().withSuccess(true).withChallengerAuthData(
new Gson().fromJson(dataString, ChallengerAuthData.class));
} catch (Exception e) {
logger.error("Error Reading Challenge Status From S3: {}", guid, e);
return new PersistenceResponse().
withSuccess(false).
withErrorMessage("Error Reading Challenges Status from S3");
}
return new PersistenceResponse().withSuccess(false).withErrorMessage("AWS not supported by current build");

// // by default will not save from aws - need to add environment variable
// if(!allowLoad){
// return new PersistenceResponse().withSuccess(false).withErrorMessage("AWS Configuration does not allow loading challenger status");
// }
//
// try {
// ensureClientExists();
//
// final S3Object fullObject = s3Client.getObject(new GetObjectRequest(bucketName, guid));
// String dataString = getObjectContent(fullObject.getObjectContent());
// return new PersistenceResponse().withSuccess(true).withChallengerAuthData(
// new Gson().fromJson(dataString, ChallengerAuthData.class));
// } catch (Exception e) {
// logger.error("Error Reading Challenge Status From S3: {}", guid, e);
// return new PersistenceResponse().
// withSuccess(false).
// withErrorMessage("Error Reading Challenges Status from S3");
// }
}

private static String getObjectContent(InputStream input) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ public PersistenceLayer(StorageType storeWhere){

String allow_save = System.getenv("AWS_ALLOW_SAVE");
if(allow_save!=null && allow_save.toLowerCase().trim().equals("true")){
allowSaveToS3=true;
allowSaveToS3=false;
}

String allow_load = System.getenv("AWS_ALLOW_LOAD");
if(allow_load!=null && allow_load.toLowerCase().trim().equals("true")){
allowLoadFromS3=true;
allowLoadFromS3=false;
}

String bucketName = System.getenv("AWSBUCKET");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
*/
public class SimpleApiRoutes {

private final DefaultGUIHTML guiTemplates;
public Thingifier simplethings;
public EntityDefinition entityDefn;
private ThingifierApiDocumentationDefn apiDocDefn;
private ThingifierAutoDocGenRouting simpleApiDocsRouting;
private ThingifierHttpApiRoutings simpleApiHttpRouting;
private DefaultGuiRoutings simpleApiGuiRouting;

public SimpleApiRoutes(){
public SimpleApiRoutes(DefaultGUIHTML guiTemplates){
// fake the data storage
this.simplethings = new Thingifier();

Expand Down Expand Up @@ -78,11 +79,13 @@ public SimpleApiRoutes(){
// TODO: should probably have a support multiple databases config somewhere
simplethings.getERmodel().populateDatabase(EntityRelModel.DEFAULT_DATABASE_NAME);

this.guiTemplates = guiTemplates;

}

public void configure() {

DefaultGUIHTML gui = new DefaultGUIHTML();
DefaultGUIHTML gui = guiTemplates;

simpleApiGuiRouting = new DefaultGuiRoutings(simplethings, gui).
configureRoutes("/simpleapi/gui");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public class SimulationRoutes {

private ThingifierApiDocumentationDefn apiDocDefn;
private ThingifierAutoDocGenRouting simulatorDocsRouting;
private DefaultGUIHTML guiTemplates;

public SimulationRoutes(DefaultGUIHTML guiTemplates){
this.guiTemplates=guiTemplates;
}

public void setUpData(){
// fake the data storage
Expand Down Expand Up @@ -78,7 +83,7 @@ public void setUpData(){
simulatorDocsRouting = new ThingifierAutoDocGenRouting(
simulation,
apiDocDefn,
new DefaultGUIHTML()
guiTemplates
);
}

Expand Down
Loading

0 comments on commit 492db22

Please sign in to comment.