Done
This commit is contained in:
39
src/main/java/tech/nevets/ngxinstaller/Server.java
Normal file
39
src/main/java/tech/nevets/ngxinstaller/Server.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package tech.nevets.ngxinstaller;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
import static spark.Spark.*;
|
||||
|
||||
public class Server {
|
||||
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
|
||||
public static void main(String[] args) {
|
||||
port(8080);
|
||||
post("/ngx/create", (req, res) -> {
|
||||
JsonObject jsonObject = GSON.fromJson(req.body(), JsonObject.class);
|
||||
TextStorage ts = TextStorage.createPaste(jsonObject.get("content").getAsString());
|
||||
return "{ \"url\":\"https://api.nevets.tech/ngx/raw/" + ts.getId() + "\" }";
|
||||
});
|
||||
get("/ngx/raw/*", (req, res) -> {
|
||||
String id = req.splat()[0];
|
||||
TextStorage ts = TextStorage.getPaste(id);
|
||||
return GSON.toJson(ts);
|
||||
});
|
||||
|
||||
|
||||
Timer timer = new Timer();
|
||||
TimerTask tt = new GCTask();
|
||||
timer.schedule(tt, 0, 60000);
|
||||
}
|
||||
|
||||
static class GCTask extends TimerTask {
|
||||
@Override
|
||||
public void run() {
|
||||
TextStorage.garbageCollect();
|
||||
}
|
||||
}
|
||||
}
|
||||
52
src/main/java/tech/nevets/ngxinstaller/TextStorage.java
Normal file
52
src/main/java/tech/nevets/ngxinstaller/TextStorage.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package tech.nevets.ngxinstaller;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class TextStorage {
|
||||
private final String id;
|
||||
private final String text;
|
||||
private final long expiration;
|
||||
|
||||
private static final List<TextStorage> DB = new ArrayList<>();
|
||||
|
||||
private TextStorage(String id, String text, long expiration) {
|
||||
this.id = id;
|
||||
this.text = text;
|
||||
this.expiration = expiration;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public static TextStorage getPaste(String id) {
|
||||
for (TextStorage ts : DB) {
|
||||
if (ts.id.equals(id)) {
|
||||
return ts;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static TextStorage createPaste(String text) {
|
||||
TextStorage ts = new TextStorage(generateId(), text, System.currentTimeMillis() + 3600000);
|
||||
DB.add(ts);
|
||||
return ts;
|
||||
}
|
||||
|
||||
private static String generateId() {
|
||||
Random rand = ThreadLocalRandom.current();
|
||||
UUID uuid = UUID.randomUUID();
|
||||
String strUuid = uuid.toString().replace("-", "");
|
||||
int randInt = rand.nextInt(strUuid.length() - 4);
|
||||
return strUuid.substring(randInt, randInt + 4);
|
||||
}
|
||||
|
||||
public static void garbageCollect() {
|
||||
DB.removeIf(ts -> ts.expiration < System.currentTimeMillis());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user