Skip to content

Commit

Permalink
Second pass
Browse files Browse the repository at this point in the history
  • Loading branch information
modmuss50 committed Dec 16, 2023
1 parent 421b746 commit 551e7ee
Show file tree
Hide file tree
Showing 24 changed files with 274 additions and 161 deletions.
3 changes: 2 additions & 1 deletion src/main/java/net/fabricmc/meta/FabricMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ public static void main(String[] args) {
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
executorService.scheduleAtFixedRate(FabricMeta::update, 1, 1, TimeUnit.MINUTES);

WebServer.start();
WebServer webServer = new WebServer(() -> database);
webServer.createServer().start(5555);
}

private static void update() {
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/net/fabricmc/meta/data/DataProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,37 @@

package net.fabricmc.meta.data;

import java.util.List;

import com.google.gson.JsonObject;

import net.fabricmc.meta.models.BaseVersion;
import net.fabricmc.meta.models.MavenBuildGameVersion;
import net.fabricmc.meta.models.MavenBuildVersion;
import net.fabricmc.meta.models.MavenVersion;
import net.fabricmc.meta.utils.LoaderMeta;

public interface DataProvider {
@Deprecated // TODO work to remove
VersionDatabase getVersionDatabase();

default List<BaseVersion> getGameVersions() {
return getVersionDatabase().game;
}

default List<MavenBuildGameVersion> getMappingVersions() {
return getVersionDatabase().mappings;
}

default List<MavenVersion> getIntermediaryVersions() {
return getVersionDatabase().intermediary;
}

default List<MavenBuildVersion> getLoaderVersions() {
return getVersionDatabase().getLoader();
}

default JsonObject getLoaderInstallerJson(String mavenNotation) {
return LoaderMeta.getMeta(mavenNotation);
}
}
10 changes: 5 additions & 5 deletions src/main/java/net/fabricmc/meta/data/VersionDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.fabricmc.meta.models.BaseVersion;
import net.fabricmc.meta.models.MavenBuildGameVersion;
import net.fabricmc.meta.models.MavenBuildVersion;
import net.fabricmc.meta.models.MavenUrlVersion;
import net.fabricmc.meta.models.MavenVersion;
import net.fabricmc.meta.utils.MinecraftLauncherMeta;
import net.fabricmc.meta.utils.PomParser;
import net.fabricmc.meta.web.models.BaseVersion;
import net.fabricmc.meta.web.models.MavenBuildGameVersion;
import net.fabricmc.meta.web.models.MavenBuildVersion;
import net.fabricmc.meta.web.models.MavenUrlVersion;
import net.fabricmc.meta.web.models.MavenVersion;

public class VersionDatabase {
public static final PomParser MAPPINGS_PARSER = new PomParser(LOCAL_FABRIC_MAVEN_URL + "net/fabricmc/yarn/maven-metadata.xml");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package net.fabricmc.meta.web.models;
package net.fabricmc.meta.models;

import java.util.function.Predicate;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package net.fabricmc.meta.web.models;
package net.fabricmc.meta.models;

public interface LoaderInfoBase {
MavenBuildVersion getLoader();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package net.fabricmc.meta.web.models;
package net.fabricmc.meta.models;

import com.google.gson.JsonObject;
import org.jetbrains.annotations.Nullable;
Expand All @@ -34,7 +34,7 @@ public LoaderInfoV1(MavenBuildVersion loader, MavenBuildGameVersion mappings) {
}

public LoaderInfoV1 populateMeta() {
launcherMeta = LoaderMeta.getMeta(this);
launcherMeta = LoaderMeta.getMeta(getLoader().getMaven());
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package net.fabricmc.meta.web.models;
package net.fabricmc.meta.models;

import com.google.gson.JsonObject;
import org.jetbrains.annotations.Nullable;
Expand All @@ -34,7 +34,7 @@ public LoaderInfoV2(MavenBuildVersion loader, MavenVersion intermediary) {
}

public LoaderInfoV2 populateMeta() {
launcherMeta = LoaderMeta.getMeta(this);
launcherMeta = LoaderMeta.getMeta(getLoader().getMaven());
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package net.fabricmc.meta.web.models;
package net.fabricmc.meta.models;

import net.fabricmc.meta.utils.YarnVersionParser;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package net.fabricmc.meta.web.models;
package net.fabricmc.meta.models;

public class MavenBuildVersion extends MavenVersion {
String separator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package net.fabricmc.meta.web.models;
package net.fabricmc.meta.models;

import net.fabricmc.meta.utils.Reference;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package net.fabricmc.meta.web.models;
package net.fabricmc.meta.models;

public class MavenVersion extends BaseVersion {
String maven;
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/net/fabricmc/meta/utils/LoaderMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@
import java.io.IOException;
import java.net.URL;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import org.apache.commons.io.FileUtils;

import net.fabricmc.meta.web.WebServer;
import net.fabricmc.meta.web.models.LoaderInfoBase;
import org.jetbrains.annotations.Nullable;

public class LoaderMeta {
public static final File BASE_DIR = new File("metadata");
private static final File BASE_DIR = new File("metadata");
private static final Gson GSON = new GsonBuilder().create();

public static JsonObject getMeta(LoaderInfoBase loaderInfo) {
String loaderMaven = loaderInfo.getLoader().getMaven();
@Nullable
public static JsonObject getMeta(String loaderMaven) {
String[] split = loaderMaven.split(":");
String path = String.format("%s/%s/%s", split[0].replaceAll("\\.", "/"), split[1], split[2]);
String filename = String.format("%s-%s.json", split[1], split[2]);
Expand All @@ -51,7 +52,7 @@ public static JsonObject getMeta(LoaderInfoBase loaderInfo) {
}

try {
JsonObject jsonObject = WebServer.GSON.fromJson(new FileReader(launcherMetaFile), JsonObject.class);
JsonObject jsonObject = GSON.fromJson(new FileReader(launcherMetaFile), JsonObject.class);
return jsonObject;
} catch (FileNotFoundException e) {
e.printStackTrace();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/fabricmc/meta/utils/PomParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import net.fabricmc.meta.web.models.BaseVersion;
import net.fabricmc.meta.models.BaseVersion;

public class PomParser {
public String path;
Expand Down
69 changes: 58 additions & 11 deletions src/main/java/net/fabricmc/meta/web/Endpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@

package net.fabricmc.meta.web;

import java.time.Duration;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.javalin.apibuilder.EndpointGroup;
import io.javalin.http.ContentType;
import io.javalin.http.Context;
import io.javalin.http.Handler;

import net.fabricmc.meta.data.DataProvider;

public abstract class Endpoint {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();

protected final DataProvider dataProvider;

protected Endpoint(DataProvider dataProvider) {
Expand All @@ -35,24 +39,67 @@ protected Endpoint(DataProvider dataProvider) {

public abstract EndpointGroup routes();

// Return a json list with no params
protected Handler result(Function<DataProvider, List<? extends JsonModel>> objectSupplier) {
protected Handler cache(Duration duration) {
return ctx -> {
List<? extends JsonModel> result = objectSupplier.apply(dataProvider);
ctx.header("Cache-Control", "public, max-age=" + duration.getSeconds());
};
}

ctx.contentType(ContentType.APPLICATION_JSON);
ctx.result(WebServer.GSON.toJson(result));
// Return a json list with no params
protected Handler result(JsonListHandler handler) {
return ctx -> {
List<? extends JsonModel> result = handler.apply(dataProvider);
jsonResult(ctx, result);
};
}

// Return a json list with one string param
protected Handler result(String key, BiFunction<DataProvider, String, List<? extends JsonModel>> objectSupplier) {
protected Handler result(String key, JsonListHandler1 handler) {
return ctx -> {
final String value = ctx.pathParamAsClass(key, String.class).get();
List<? extends JsonModel> result = handler.apply(dataProvider, value);
jsonResult(ctx, result);
};
}

List<? extends JsonModel> result = objectSupplier.apply(dataProvider, value);
ctx.contentType(ContentType.APPLICATION_JSON);
ctx.result(WebServer.GSON.toJson(result));
// Return a json list with two string params
protected Handler result(String key1, String key2, JsonListHandler2 handler) {
return ctx -> {
final String value1 = ctx.pathParamAsClass(key1, String.class).get();
final String value2 = ctx.pathParamAsClass(key2, String.class).get();
List<? extends JsonModel> result = handler.apply(dataProvider, value1, value2);
jsonResult(ctx, result);
};
}

// Return a json list with two string params
protected Handler result(String key1, String key2, JsonHandler2 handler) {
return ctx -> {
final String value1 = ctx.pathParamAsClass(key1, String.class).get();
final String value2 = ctx.pathParamAsClass(key2, String.class).get();
JsonModel result = handler.apply(dataProvider, value1, value2);
jsonResult(ctx, result);
};
}

protected void jsonResult(Context ctx, Object result) {
ctx.contentType(ContentType.APPLICATION_JSON);
ctx.result(GSON.toJson(result));
}

protected interface JsonListHandler {
List<? extends JsonModel> apply(DataProvider dataProvider);
}

protected interface JsonListHandler1 {
List<? extends JsonModel> apply(DataProvider dataProvider, String key1);
}

protected interface JsonListHandler2 {
List<? extends JsonModel> apply(DataProvider dataProvider, String key1, String key2);
}

protected interface JsonHandler2 {
JsonModel apply(DataProvider dataProvider, String key1, String key2);
}
}
3 changes: 2 additions & 1 deletion src/main/java/net/fabricmc/meta/web/ProfileHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@
import com.google.gson.JsonObject;
import org.apache.commons.io.IOUtils;

import net.fabricmc.meta.models.LoaderInfoV2;
import net.fabricmc.meta.utils.Reference;
import net.fabricmc.meta.web.models.LoaderInfoV2;
import net.fabricmc.meta.web.v2.EndpointsV2;

public class ProfileHandler {
private static final Executor EXECUTOR = Executors.newFixedThreadPool(2);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/fabricmc/meta/web/ServerBootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
import org.apache.commons.io.FileUtils;

import net.fabricmc.meta.FabricMeta;
import net.fabricmc.meta.models.BaseVersion;
import net.fabricmc.meta.utils.Reference;
import net.fabricmc.meta.web.models.BaseVersion;

public class ServerBootstrap {
private static final Path CACHE_DIR = Paths.get("metadata", "installer");
Expand Down
34 changes: 17 additions & 17 deletions src/main/java/net/fabricmc/meta/web/WebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,40 +28,40 @@
import io.javalin.http.Header;
import io.javalin.plugin.bundled.CorsPluginConfig;

import net.fabricmc.meta.FabricMeta;
import net.fabricmc.meta.data.DataProvider;
import net.fabricmc.meta.web.v1.EndpointsV1;
import net.fabricmc.meta.web.v2.EndpointsV2;

public class WebServer {
@Deprecated(forRemoval = true)
public static Javalin javalin;
public static Gson GSON = new GsonBuilder().setPrettyPrinting().create();

public static Javalin create() {
if (javalin != null) {
javalin.stop();
}
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();

private final DataProvider dataProvider;
private final EndpointsV1 endpointsV1;

javalin = Javalin.create(config -> {
public WebServer(DataProvider dataProvider) {
this.dataProvider = dataProvider;
endpointsV1 = new EndpointsV1(dataProvider);
}

public Javalin createServer() {
Javalin javalin = Javalin.create(config -> {
config.plugins.enableRouteOverview("/");
config.showJavalinBanner = false;
config.plugins.enableCors(cors -> cors.add(CorsPluginConfig::anyHost));
});

EndpointsV1 endpointsV1 = new EndpointsV1(() -> FabricMeta.database);

javalin.routes(() -> {
}).routes(() -> {
path("v1", endpointsV1.routes());
});

// TODO remove this
WebServer.javalin = javalin;
EndpointsV2.setup();

return javalin;
}

public static void start() {
assert javalin == null;
create().start(5555);
}

public static <T> void jsonGet(String route, Supplier<T> supplier) {
javalin.get(route, ctx -> {
T object = supplier.get();
Expand Down
Loading

0 comments on commit 551e7ee

Please sign in to comment.