Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code cleanup + refactor #28

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ repositories {

dependencies {
implementation 'com.google.code.gson:gson:2.10.1'
implementation 'io.javalin:javalin:5.6.3'
implementation 'io.javalin:javalin:6.0.0-beta.3'
implementation 'org.slf4j:slf4j-simple:2.0.9'
implementation 'commons-io:commons-io:2.15.1'
implementation 'org.jetbrains:annotations:24.1.0'

testImplementation 'org.junit.jupiter:junit-jupiter:5.9.2'
testImplementation "org.junit.jupiter:junit-jupiter-params:5.9.2"
testImplementation 'io.javalin:javalin-testtools:5.6.3'
testImplementation 'io.javalin:javalin-testtools:6.0.0-beta.3'

testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
Expand Down
2 changes: 1 addition & 1 deletion checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@

<!--<module name="InvalidJavadocPosition"/>-->
<module name="JavadocParagraph"/>
<module name="JavadocStyle"/>
<!-- <module name="JavadocStyle"/>-->
<module name="AtclauseOrder">
<property name="tagOrder" value="@param,@return,@throws,@deprecated"/>
</module>
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/net/fabricmc/meta/FabricMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,19 @@

import net.fabricmc.meta.data.VersionDatabase;
import net.fabricmc.meta.utils.Reference;
import net.fabricmc.meta.web.CacheHandler;
import net.fabricmc.meta.web.WebServer;

public class FabricMeta {
// TODO remove all this static access
@Deprecated()
public static volatile VersionDatabase database;

private static final Logger LOGGER = LoggerFactory.getLogger(VersionDatabase.class);
private static final Map<String, String> config = new HashMap<>();
private static boolean configInitialized;
private static URL heartbeatUrl; // URL pinged with every successful update()
private static CacheHandler cacheHandler = new CacheHandler();

public static void main(String[] args) {
Path configFile = Paths.get("config.json");
Expand Down Expand Up @@ -77,12 +81,14 @@ 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, cacheHandler);
webServer.createServer().start(5555);
}

private static void update() {
try {
database = VersionDatabase.generate();
cacheHandler.invalidateCache();
updateHeartbeat();
} catch (Throwable t) {
if (database == null) {
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/net/fabricmc/meta/data/DataProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2023 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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);
}
}
24 changes: 7 additions & 17 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 All @@ -63,10 +63,8 @@ public static VersionDatabase generate() throws IOException, XMLStreamException
database.intermediary = INTERMEDIARY_PARSER.getMeta(MavenVersion::new, "net.fabricmc:intermediary:");
database.loader = LOADER_PARSER.getMeta(MavenBuildVersion::new, "net.fabricmc:fabric-loader:", list -> {
for (BaseVersion version : list) {
if (isPublicLoaderVersion(version)) {
version.setStable(true);
break;
}
version.setStable(true);
break;
}
});
database.installer = INSTALLER_PARSER.getMeta(MavenUrlVersion::new, "net.fabricmc:fabric-installer:");
Expand Down Expand Up @@ -114,14 +112,6 @@ private void loadMcData() throws IOException {
}

public List<MavenBuildVersion> getLoader() {
return loader.stream().filter(VersionDatabase::isPublicLoaderVersion).collect(Collectors.toList());
}

private static boolean isPublicLoaderVersion(BaseVersion version) {
return true;
}

public List<MavenBuildVersion> getAllLoader() {
return Collections.unmodifiableList(loader);
}
}
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
87 changes: 87 additions & 0 deletions src/main/java/net/fabricmc/meta/web/CacheHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright (c) 2023 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.meta.web;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import io.javalin.http.Handler;

public class CacheHandler {
private final Map<String, Response> cache = new ConcurrentHashMap<>();

public CacheHandler() {
}

public Handler before() {
return ctx -> {
Response response = cache.get(ctx.path());

if (response == null) {
return;
}

// Replay the response
ctx.status(response.status());
response.headers().forEach(ctx::header);
ctx.contentType(response.contentType());
ctx.result(response.body());

ctx.skipRemainingHandlers();
};
}

public Handler after() {
return ctx -> {
if (ctx.statusCode() != 200) {
return;
}

if (!ctx.queryParamMap().isEmpty()) {
// Don't cache any requests with query params to prevent the cache from growing too big.
// Maybe look into something better here
return;
}

cache.put(ctx.path(), new Response(
ctx.statusCode(),
readAllBytes(ctx.resultInputStream()),
ctx.headerMap(),
ctx.res().getContentType()));
};
}

private static byte[] readAllBytes(InputStream is) throws IOException {
is.reset();
byte[] bytes = is.readAllBytes();
is.reset();
return bytes;
}

public void invalidateCache() {
cache.clear();
}

private record Response(
int status,
byte[] body,
Map<String, String> headers,
String contentType) {
}
}
Loading
Loading