Fix merge issues

This commit is contained in:
2024-08-07 17:32:09 -04:00
3 changed files with 103 additions and 20 deletions

View File

@@ -5,6 +5,10 @@ import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import java.net.URLDecoder;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Timer;
import java.util.TimerTask;
@@ -13,6 +17,7 @@ import static spark.Spark.*;
public class Server {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
public static String cachedNginxSite = "";
public static void main(String[] args) {
if (args.length >= 1) {
port(Integer.parseInt(args[0]));
@@ -20,9 +25,53 @@ public class Server {
port(8080);
}
try {
cachedNginxSite = getHTML("https://nginx.org/download/");
} catch (Exception e) {
throw new RuntimeException(e);
}
port(8080);
options("/*",
(request, response) -> {
String accessControlRequestHeaders = request
.headers("Access-Control-Request-Headers");
if (accessControlRequestHeaders != null) {
response.header("Access-Control-Allow-Headers",
accessControlRequestHeaders);
}
String accessControlRequestMethod = request
.headers("Access-Control-Request-Method");
if (accessControlRequestMethod != null) {
response.header("Access-Control-Allow-Methods",
accessControlRequestMethod);
}
return "OK";
});
before((request, response) -> response.header("Access-Control-Allow-Origin", "*"));
post("/ngx/dhv", (req, res) -> {
System.out.println(req.body());
JsonObject jsonObject = GSON.fromJson(req.body(), JsonObject.class);
String version = jsonObject.get("version").getAsString();
System.out.println(version);
try {
if (cachedNginxSite.contains(version + ".tar.gz")) {
System.out.println("true");
return "{ \"url\":\"true\" }";
}
} catch (Exception ignore) {
System.out.println("false");
return "{ \"url\":\"false\" }";
}
System.out.println("false");
return "{ \"url\":\"false\" }";
});
post("/ngx/create", (req, res) -> {
JsonObject jsonObject = GSON.fromJson(req.body(), JsonObject.class);
TextStorage ts = TextStorage.createPaste(jsonObject.get("content").getAsString());
TextStorage ts = TextStorage.createPaste(java.net.URLDecoder.decode(jsonObject.get("content").getAsString(), StandardCharsets.UTF_8));
return "{ \"url\":\"https://api.nevets.tech/ngx/raw/" + ts.getId() + "\" }";
});
get("/ngx/raw/*", (req, res) -> {
@@ -43,7 +92,8 @@ public class Server {
res.status(404);
return "{\"error\":\"content not found\"}";
}
return GSON.toJson(ts);
res.type("raw");
return ts.getText();
});
@@ -52,6 +102,21 @@ public class Server {
timer.schedule(tt, 0, 60000);
}
public static String getHTML(String urlToRead) throws Exception {
StringBuilder result = new StringBuilder();
URL url = new URL(urlToRead);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(conn.getInputStream()))) {
for (String line; (line = reader.readLine()) != null; ) {
result.append(line);
}
}
return result.toString();
}
static class GCTask extends TimerTask {
@Override
public void run() {