|
| 1 | +package pro.cloudnode.smp.smpcore.api; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.google.gson.GsonBuilder; |
| 5 | +import com.google.gson.JsonObject; |
| 6 | +import io.javalin.Javalin; |
| 7 | +import io.javalin.config.JavalinConfig; |
| 8 | +import io.javalin.http.Context; |
| 9 | +import io.javalin.json.JsonMapper; |
| 10 | +import org.jetbrains.annotations.NotNull; |
| 11 | +import org.jetbrains.annotations.Nullable; |
| 12 | +import pro.cloudnode.smp.smpcore.SMPCore; |
| 13 | +import pro.cloudnode.smp.smpcore.api.routes.Members; |
| 14 | +import pro.cloudnode.smp.smpcore.api.routes.Nations; |
| 15 | + |
| 16 | +import java.lang.reflect.Type; |
| 17 | + |
| 18 | +public class REST { |
| 19 | + private final @NotNull Javalin javalin; |
| 20 | + |
| 21 | + public REST(final int port) { |
| 22 | + javalin = Javalin.create(config -> { |
| 23 | + config.jsonMapper(new Mapper()); |
| 24 | + }).start(port); |
| 25 | + } |
| 26 | + |
| 27 | + private static void info(final @NotNull Context ctx) { |
| 28 | + final @NotNull JsonObject obj = new JsonObject(); |
| 29 | + obj.addProperty("version", SMPCore.getInstance().getPluginMeta().getVersion()); |
| 30 | + obj.addProperty("time", SMPCore.gameTime().getTime()); |
| 31 | + ctx.json(obj); |
| 32 | + } |
| 33 | + |
| 34 | + public void stop() { |
| 35 | + javalin.stop(); |
| 36 | + } |
| 37 | + |
| 38 | + private void configureRoutes(final @NotNull JavalinConfig config) { |
| 39 | + config.routes.before(ctx -> { |
| 40 | + final @Nullable String origin = ctx.header("Origin"); |
| 41 | + ctx.header("Access-Control-Allow-Origin", origin == null ? "*" : origin); |
| 42 | + ctx.header("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS"); |
| 43 | + ctx.header("Access-Control-Allow-Headers", "*"); |
| 44 | + ctx.header("Access-Control-Allow-Credentials", "true"); |
| 45 | + ctx.header("Access-Control-Max-Age", "3600"); |
| 46 | + }); |
| 47 | + |
| 48 | + |
| 49 | + config.routes.get("/", REST::info); |
| 50 | + |
| 51 | + final var members = new Members(this); |
| 52 | + config.routes.get("/members", members::list); |
| 53 | + config.routes.get("/members/{uuid}", members::get); |
| 54 | + |
| 55 | + final var nations = new Nations(this); |
| 56 | + config.routes.get("/nations", nations::list); |
| 57 | + config.routes.get("/nations/{id}", nations::get); |
| 58 | + } |
| 59 | + |
| 60 | + public void e404(final @NotNull Context ctx) { |
| 61 | + ctx.status(404); |
| 62 | + final @NotNull JsonObject obj = new JsonObject(); |
| 63 | + obj.addProperty("error", "not found"); |
| 64 | + ctx.json(obj); |
| 65 | + } |
| 66 | + |
| 67 | + public static final class Mapper implements JsonMapper { |
| 68 | + private final @NotNull Gson gson = new GsonBuilder().serializeNulls().create(); |
| 69 | + |
| 70 | + @Override |
| 71 | + public @NotNull String toJsonString(final @NotNull Object obj, final @NotNull Type type) { |
| 72 | + return gson.toJson(obj, type); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public <T> @NotNull T fromJsonString(final @NotNull String json, final @NotNull Type targetType) { |
| 77 | + return gson.fromJson(json, targetType); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments