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

Commit

Permalink
invoke count
Browse files Browse the repository at this point in the history
  • Loading branch information
cubewhy committed Jan 18, 2024
1 parent 2b04278 commit cbbcce1
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class BackendApplication {
public static final File crashReportFolder = new File(configPath, "crash-reports");
public static final File artifactsFolder = new File(configPath, "artifacts");
public static final File applicationConfigFile = new File(configPath, "application.yml");

public static void main(String[] args) {
configPath.mkdirs();
new FileUtils().extractFile("application.yml", applicationConfigFile);
Expand Down
4 changes: 4 additions & 0 deletions backend/src/main/java/org/cubewhy/api/StartupRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import jakarta.annotation.Resource;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.cubewhy.api.files.InvokeCount;
import org.cubewhy.api.utils.AddonFileListener;
import org.cubewhy.api.utils.FileUtils;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.net.ssl.*;
import java.io.File;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

Expand All @@ -20,6 +22,8 @@ public class StartupRunner implements CommandLineRunner {
FileUtils utils;
FileAlterationMonitor monitor = new FileAlterationMonitor(10000L);

public static final InvokeCount counter = new InvokeCount(new File(configPath, "invoke-counter.json"));

/**
* Callback used to run the bean.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.UUID;

import static org.cubewhy.api.BackendApplication.*;
import static org.cubewhy.api.StartupRunner.counter;

@RestController
@RequestMapping("/launcher")
Expand Down Expand Up @@ -55,6 +56,9 @@ public void launch(@NotNull HttpServletRequest request, @NotNull HttpServletResp
String json = null;
// return download url
// Find at <configDir>/<branch>/<version>-<module>.json
// 拼接下载链接
counter.addCount("launcher/launch");

InputStream stream = fileUtils.getExternalFile(branchesFolder + String.format("/%s/%s-%s.json", info.getBranch(), info.getVersion(), info.getModule()));
if (stream != null) {
json = utils.readAll(stream);
Expand Down Expand Up @@ -138,4 +142,6 @@ public void getCrashReport(HttpServletResponse response, @RequestParam(required
response.getWriter().write(RestBean.success(JSONObject.parse(json)).toJson());
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.websocket.server.PathParam;
import org.cubewhy.api.entity.RestBean;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -17,6 +18,8 @@

import java.io.IOException;

import static org.cubewhy.api.StartupRunner.counter;

@RestController
@RequestMapping("/api/web")
public class WebController {
Expand Down Expand Up @@ -51,6 +54,12 @@ public void pay(HttpServletRequest request, @NotNull HttpServletResponse respons
response.sendRedirect(target);
}

@GetMapping("invoke")
public void invoke(HttpServletResponse response, @PathParam("api") String api) throws IOException {
response.setContentType("application/json");
response.getWriter().write(RestBean.success(counter.getCount(api)).toJson());
}

@GetMapping("latest")
public void latestVersion(HttpServletRequest request, HttpServletResponse response) throws Exception {
/*
Expand Down
2 changes: 2 additions & 0 deletions backend/src/main/java/org/cubewhy/api/entity/GameInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ public class GameInfo implements BaseData {
String version;
String branch;
String module;
String os;
String arch;
}
75 changes: 75 additions & 0 deletions backend/src/main/java/org/cubewhy/api/files/InvokeCount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.cubewhy.api.files;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.nio.charset.StandardCharsets;

@Slf4j
public class InvokeCount {
@Getter
private JSONObject config;
public final File file;

public InvokeCount(File file) {
this.file = file;
this.load();
}

public void addCount(String apiName) {
if (!config.containsKey(apiName)) {
config.put(apiName, 0);
}
config.put(apiName, config.getIntValue(apiName) + 1);
save();
}

public int getCount(String apiName) {
return config.getIntValue(apiName, 0);
}

public InvokeCount save() {
try {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(this.file));
bufferedWriter.write(config.toString());
bufferedWriter.flush();
bufferedWriter.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
return this;
}

public InvokeCount load() {
FileReader fileReader;
boolean successful = false;

while (!successful) {
try {
fileReader = new FileReader(this.file, StandardCharsets.UTF_8);
config = JSON.parseObject(fileReader);
if (config == null) {
config = new JSONObject();
}
successful = true;
fileReader.close();
} catch (FileNotFoundException e) {

try {
if (!this.file.getParentFile().exists()) {
this.file.getParentFile().mkdirs();
}
this.file.createNewFile();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,19 @@ public static void addFile(File file) throws IOException {
addons.add(json);
}

private static String getAddonType(File file) {
private static String getAddonType(@NotNull File file) {
return switch (file.getParentFile().getName()) {
case "mods" -> "weave";
case "cn" -> "cn";
case "fabric" -> "fabric";
case "javaagents" -> "Agent";
default ->
// Unreachable
file.getParentFile().getName();
};
}

private void walkDir(File dir) throws IOException {
private void walkDir(@NotNull File dir) throws IOException {
File[] files = dir.listFiles();
if (files != null) {
for (File file : files) {
Expand Down

0 comments on commit cbbcce1

Please sign in to comment.