Skip to content
This repository has been archived by the owner on Feb 24, 2023. It is now read-only.

Tidy up and minor fixes #20

Merged
merged 1 commit into from
Oct 4, 2021
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
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ yarn start:mongodb
Stop the MongoDB server: -

```bash
yarn start:mongodb
yarn stop:mongodb
```

Please note this does not run as a replica set and does not persist the data. We recommend setting a volume if you wish to have the data persist: -
Expand Down
7 changes: 7 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ services:
expose:
- "6080"
environment:
# To connect to a local version of MongoDB, use mongodb://host.docker.internal .
MONGO_CONNECT_STRING: "mongodb://mongodb"
# The API server - required for generating live previews. Here we refer to the 'api' server as defined below.
S8_API_SERVER: "http://api:8082"
# The root server refers to the entry point of the network. In this example, we are using the 'route' defined below.
S8_ROOT_SERVER: "http://127.0.0.1:8080"
# Proxy a platform's live build in. PROXY_FOR can be either be 'core' or the platform id associated with the platform.
# PROXY_FOR: "core"
# PROXY_LOCATION: "http://host.docker.internal:3124/main.js"

api:
image: scale8/api:latest
Expand All @@ -31,6 +37,7 @@ services:
expose:
- "8082"
environment:
# To connect to a local version of MongoDB, use mongodb://host.docker.internal .
MONGO_CONNECT_STRING: "mongodb://mongodb"

router:
Expand Down
4 changes: 3 additions & 1 deletion edge/build.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

./mvnw package
./mvnw clean install

./mvnw clean package

docker build -t scale8/edge:latest .
5 changes: 4 additions & 1 deletion edge/src/main/java/com/scale8/AppEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,11 @@ public String getCoreJs(ExtendedRequest request) throws Exception {
track(request, new JsonObject());
}

boolean isProxyMode =
env.PROXY_FOR.equals(appSettings.getCorePlatformId()) || env.PROXY_FOR.equals("core");

String js =
env.PROXY_FOR.equals(appSettings.getCorePlatformId())
isProxyMode
? doJsProxyRequest(env.PROXY_LOCATION)
: getPrimaryJsAsset(request, appSettings.getCorePlatformId());

Expand Down
2 changes: 1 addition & 1 deletion edge/src/main/java/com/scale8/Env.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private String getOrElse(String name, String orElse) {

public final String CONFIG_BUCKET = getOrElse("CONFIGS_BUCKET", "scale8_com_" + ENV + "_configs");

public final String PROXY_LOCATION = getOrElse("PROXY_ENDPOINT", "http://127.0.0.1:3123/main.js");
public final String PROXY_LOCATION = getOrElse("PROXY_LOCATION", "http://127.0.0.1:3123/main.js");

public final String PROXY_FOR = getOrElse("PROXY_FOR", "");

Expand Down
89 changes: 48 additions & 41 deletions edge/src/main/java/com/scale8/RootController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.scale8.asset.Loader;
import com.scale8.backends.storage.StorageInterface;
import com.scale8.extended.ExtendedRequest;
import com.scale8.mmdb.Geo;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.PathVariable;
Expand Down Expand Up @@ -39,8 +39,6 @@ public class RootController {

@Inject Geo geo;

@Inject StorageInterface storage;

private ExtendedRequest createExtendedRequest(
HttpRequest<String> request, String id, Map<String, String> extraParameters) {
return new ExtendedRequest(
Expand Down Expand Up @@ -79,53 +77,60 @@ public String ingestPostRequest(HttpRequest<String> request, @PathVariable Strin
return this.handleIngestRequest(request, id);
}

private String handleAnalyticsLoader(HttpRequest<String> request, String id) throws IOException {
private HttpResponse<String> handleAnalyticsLoader(HttpRequest<String> request, String id)
throws IOException {
String opts = request.getParameters().get("opts");
return loader
.getResourceAsString("analytics.js")
.replace("$spa", opts != null && opts.contains("spa") ? "!0" : "!1")
.replace("$hash", opts != null && opts.contains("hash") ? "!0" : "!1")
.replace("$getSvr", createExtendedRequest(request, id, null).getServer());
return HttpResponse.ok(
loader
.getResourceAsString("analytics.js")
.replace("$spa", opts != null && opts.contains("spa") ? "!0" : "!1")
.replace("$hash", opts != null && opts.contains("hash") ? "!0" : "!1")
.replace("$getSvr", createExtendedRequest(request, id, null).getServer()));
}

@Get(uri = "/analytics.js", produces = APPLICATION_JAVASCRIPT)
public String analyticsLoader(HttpRequest<String> request) throws IOException {
public HttpResponse<String> analyticsLoader(HttpRequest<String> request) throws IOException {
return this.handleAnalyticsLoader(request, null);
}

@Get(uri = "/edge/{id}/analytics.js", produces = APPLICATION_JAVASCRIPT)
public String analyticsLoader(HttpRequest<String> request, @PathVariable String id)
public HttpResponse<String> analyticsLoader(HttpRequest<String> request, @PathVariable String id)
throws IOException {
return this.handleAnalyticsLoader(request, id);
}

private String handleTmLoader(HttpRequest<String> request, String id) throws IOException {
return loader
.getResourceAsString("tm.js")
.replace("$getSvr", createExtendedRequest(request, id, null).getServer());
private HttpResponse<String> handleTmLoader(HttpRequest<String> request, String id)
throws IOException {
return HttpResponse.ok(
loader
.getResourceAsString("tm.js")
.replace("$getSvr", createExtendedRequest(request, id, null).getServer()));
}

@Get(uri = "/tm.js", produces = APPLICATION_JAVASCRIPT)
public String tmLoader(HttpRequest<String> request) throws IOException {
public HttpResponse<String> tmLoader(HttpRequest<String> request) throws IOException {
return handleTmLoader(request, null);
}

@Get(uri = "/edge/{id}/tm.js", produces = APPLICATION_JAVASCRIPT)
public String tmLoader(HttpRequest<String> request, @PathVariable String id) throws IOException {
public HttpResponse<String> tmLoader(HttpRequest<String> request, @PathVariable String id)
throws IOException {
return handleTmLoader(request, id);
}

private String handleTmCore(HttpRequest<String> request, String id) throws Exception {
return appEntity.getCoreJs(createExtendedRequest(request, id, null));
private HttpResponse<String> handleTmCore(HttpRequest<String> request, String id)
throws Exception {
return HttpResponse.ok(appEntity.getCoreJs(createExtendedRequest(request, id, null)));
}

@Get(uri = "/tm-core.js", produces = APPLICATION_JAVASCRIPT)
public String tmCore(HttpRequest<String> request) throws Exception {
public HttpResponse<String> tmCore(HttpRequest<String> request) throws Exception {
return handleTmCore(request, null);
}

@Get(uri = "/edge/{id}/tm-core.js", produces = APPLICATION_JAVASCRIPT)
public String tmCore(HttpRequest<String> request, @PathVariable String id) throws Exception {
public HttpResponse<String> tmCore(HttpRequest<String> request, @PathVariable String id)
throws Exception {
return handleTmCore(request, id);
}

Expand All @@ -148,37 +153,38 @@ public String trackEvent(
return handleTrackEvent(request, id, event);
}

private String handlePrimaryJsAsset(
private HttpResponse<String> handlePrimaryJsAsset(
HttpRequest<String> request, String id, String platformId, String revisionId)
throws Exception {
return appEntity.getPrimaryJsAsset(
createExtendedRequest(
request, id, revisionId == null ? null : Map.of("preview", revisionId)),
platformId);
return HttpResponse.ok(
appEntity.getPrimaryJsAsset(
createExtendedRequest(
request, id, revisionId == null ? null : Map.of("preview", revisionId)),
platformId));
}

@Get(uri = "/d/{platformId}", produces = APPLICATION_JAVASCRIPT)
public String primaryJsAsset(HttpRequest<String> request, @PathVariable String platformId)
throws Exception {
public HttpResponse<String> primaryJsAsset(
HttpRequest<String> request, @PathVariable String platformId) throws Exception {
return handlePrimaryJsAsset(request, null, platformId, null);
}

@Get(uri = "/edge/{id}/d/{platformId}", produces = APPLICATION_JAVASCRIPT)
public String primaryJsAsset(
public HttpResponse<String> primaryJsAsset(
HttpRequest<String> request, @PathVariable String id, @PathVariable String platformId)
throws Exception {
return handlePrimaryJsAsset(request, id, platformId, null);
}

@Get(uri = "/p/{platformId}/{revisionId}", produces = APPLICATION_JAVASCRIPT)
public String primaryJsAssetPreview(
public HttpResponse<String> primaryJsAssetPreview(
HttpRequest<String> request, @PathVariable String platformId, @PathVariable String revisionId)
throws Exception {
return handlePrimaryJsAsset(request, null, platformId, revisionId);
}

@Get(uri = "/edge/{id}/p/{platformId}/{revisionId}", produces = APPLICATION_JAVASCRIPT)
public String primaryJsAssetPreview(
public HttpResponse<String> primaryJsAssetPreview(
HttpRequest<String> request,
@PathVariable String platformId,
@PathVariable String id,
Expand All @@ -187,25 +193,26 @@ public String primaryJsAssetPreview(
return handlePrimaryJsAsset(request, id, platformId, revisionId);
}

private String handleJsAsset(
private HttpResponse<String> handleJsAsset(
HttpRequest<String> request, String id, String platformId, String revisionId, String asset)
throws Exception {
return appEntity.fetchJSAsset(
createExtendedRequest(
request, id, revisionId == null ? null : Map.of("preview", revisionId)),
platformId,
asset);
return HttpResponse.ok(
appEntity.fetchJSAsset(
createExtendedRequest(
request, id, revisionId == null ? null : Map.of("preview", revisionId)),
platformId,
asset));
}

@Get(uri = "/d/{platformId}/{asset}", produces = APPLICATION_JAVASCRIPT)
public String jsAsset(
public HttpResponse<String> jsAsset(
HttpRequest<String> request, @PathVariable String platformId, @PathVariable String asset)
throws Exception {
return handleJsAsset(request, null, platformId, null, asset);
}

@Get(uri = "/edge/{id}/d/{platformId}/{asset}", produces = APPLICATION_JAVASCRIPT)
public String jsAsset(
public HttpResponse<String> jsAsset(
HttpRequest<String> request,
@PathVariable String id,
@PathVariable String platformId,
Expand All @@ -215,7 +222,7 @@ public String jsAsset(
}

@Get(uri = "/p/{platformId}/{revisionId}/{asset}", produces = APPLICATION_JAVASCRIPT)
public String jsAssetPreview(
public HttpResponse<String> jsAssetPreview(
HttpRequest<String> request,
@PathVariable String platformId,
@PathVariable String revisionId,
Expand All @@ -225,7 +232,7 @@ public String jsAssetPreview(
}

@Get(uri = "/edge/{id}/p/{platformId}/{revisionId}/{asset}", produces = APPLICATION_JAVASCRIPT)
public String jsAssetPreview(
public HttpResponse<String> jsAssetPreview(
HttpRequest<String> request,
@PathVariable String id,
@PathVariable String platformId,
Expand Down
3 changes: 2 additions & 1 deletion edge/src/main/resources/tm.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
}, "").substr(1), e = document.createElement("script");
e.async = !0;
e.src = g + "/tm-core.js" + ("" === f ? "" : "?" + f);
e.crossOrigin = "";
a.document.getElementsByTagName("head")[0].appendChild(e);
})(window, "$getSvr", "s8prev");
})(window, "$getSvr", "s8prev");
4 changes: 2 additions & 2 deletions platforms/dev-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const middleware = require('webpack-dev-middleware');
const fs = require("fs");
const path = require("path");

const SERVER_PORT = 3123;
const PUBLIC_PATH = `http://127.0.0.1:${SERVER_PORT}`;
const SERVER_PORT = 3124;
const PUBLIC_PATH = `http://127.0.0.1:${SERVER_PORT}/`;

const providerString = process.argv.find((value => value.startsWith('--provider=')));

Expand Down