|
| 1 | +package journeymap_webmap; |
| 2 | + |
| 3 | +import io.javalin.Javalin; |
| 4 | +import io.javalin.http.staticfiles.Location; |
| 5 | +import journeymap.client.Constants; |
| 6 | +import journeymap.client.JourneymapClient; |
| 7 | +import journeymap.client.io.FileHandler; |
| 8 | +import journeymap_webmap.routes.Data; |
| 9 | +import journeymap_webmap.routes.Log; |
| 10 | +import journeymap_webmap.routes.Polygons; |
| 11 | +import journeymap_webmap.routes.Resources; |
| 12 | +import journeymap_webmap.routes.Skin; |
| 13 | +import journeymap_webmap.routes.Status; |
| 14 | +import journeymap_webmap.routes.Tiles; |
| 15 | +import journeymap_webmap.routes.Waypoints; |
| 16 | +import net.minecraft.resources.ResourceLocation; |
| 17 | +import org.slf4j.Logger; |
| 18 | +import org.slf4j.LoggerFactory; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.io.IOException; |
| 22 | +import java.net.ServerSocket; |
| 23 | + |
| 24 | +import static journeymap.common.Journeymap.MOD_ID; |
| 25 | + |
| 26 | +public class WebMap |
| 27 | +{ |
| 28 | + public static final Logger logger = LoggerFactory.getLogger("webmap"); |
| 29 | + private int port = 0; |
| 30 | + private boolean started = false; |
| 31 | + private Javalin app = null; |
| 32 | + private static WebMap INSTANCE; |
| 33 | + |
| 34 | + public static WebMap getInstance() |
| 35 | + { |
| 36 | + if (INSTANCE == null) |
| 37 | + { |
| 38 | + INSTANCE = new WebMap(); |
| 39 | + } |
| 40 | + return INSTANCE; |
| 41 | + } |
| 42 | + |
| 43 | + public void start() |
| 44 | + { |
| 45 | + if (!started) |
| 46 | + { |
| 47 | + findPort(true); |
| 48 | + initialise(); |
| 49 | + started = true; |
| 50 | + logger.info("WebMap is now listening on port {}", port); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private void initialise() |
| 55 | + { |
| 56 | + try |
| 57 | + { |
| 58 | + app = Javalin.create(config -> { |
| 59 | + String assetsRootProperty = System.getProperty("journeymap.webmap.assets_root", null); |
| 60 | + File testFile = new File("../src/main/resources" + FileHandler.ASSETS_WEBMAP); |
| 61 | + |
| 62 | + if (assetsRootProperty != null) |
| 63 | + { |
| 64 | + logger.info("Detected 'journeymap.webmap.assets_root' property, serving static files from: " + assetsRootProperty); |
| 65 | + config.staticFiles.add(assetsRootProperty, Location.EXTERNAL); |
| 66 | + } |
| 67 | + else if (testFile.exists()) |
| 68 | + { |
| 69 | + try |
| 70 | + { |
| 71 | + String assets = testFile.getCanonicalPath(); |
| 72 | + logger.info("Development environment detected, serving static files from the filesystem.: " + assets); |
| 73 | + config.staticFiles.add(testFile.getCanonicalPath(), Location.EXTERNAL); |
| 74 | + } |
| 75 | + catch (IOException e) |
| 76 | + { |
| 77 | + logger.error("WebMap error finding local assets path", e); |
| 78 | + } |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + File dir = new File(FileHandler.getMinecraftDirectory(), Constants.WEB_DIR); |
| 83 | + if (dir.exists()) |
| 84 | + { |
| 85 | + dir.delete(); |
| 86 | + } |
| 87 | + if (!dir.exists()) |
| 88 | + { |
| 89 | + logger.info("Attempting to copy web content to {}", new File(Constants.JOURNEYMAP_DIR, "web")); |
| 90 | + boolean created = FileHandler.copyResources(dir, ResourceLocation.fromNamespaceAndPath(MOD_ID, "web"), "", false); |
| 91 | + logger.info("Web content copied successfully: {}", created); |
| 92 | + } |
| 93 | + |
| 94 | + if (dir.exists()) |
| 95 | + { |
| 96 | + logger.info("Loading web content from local: {}", dir.getPath()); |
| 97 | + config.staticFiles.add(dir.getPath(), Location.EXTERNAL); |
| 98 | + } |
| 99 | + else |
| 100 | + { |
| 101 | + logger.info("Loading web content from jar: {}", FileHandler.ASSETS_WEBMAP); |
| 102 | + config.staticFiles.add(FileHandler.ASSETS_WEBMAP, Location.CLASSPATH); |
| 103 | + } |
| 104 | + } |
| 105 | + }) |
| 106 | + .before(ctx -> { |
| 107 | + ctx.header("Access-Control-Allow-Origin", "*"); |
| 108 | + ctx.header("Cache-Control", "no-cache"); |
| 109 | + }) |
| 110 | + .get("/waypoint/{id}/icon", Waypoints::iconGet) |
| 111 | + .get("/data/{type}", Data::dataGet) |
| 112 | + .get("/logs", Log::logGet) |
| 113 | + .get("/polygons", Polygons::polygonsGet) |
| 114 | + .get("/resources", Resources::resourcesGet) |
| 115 | + .get("/skin/{uuid}", Skin::skinGet) |
| 116 | + .get("/status", Status::statusGet) |
| 117 | + .get("/tiles/tile.png", Tiles::tilesGet); |
| 118 | + app.start(port); |
| 119 | + } |
| 120 | + catch (Exception e) |
| 121 | + { |
| 122 | + logger.error("Failed to start server: " + e); |
| 123 | + stop(); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + public void stop() |
| 128 | + { |
| 129 | + if (started) |
| 130 | + { |
| 131 | + if (app != null) |
| 132 | + { |
| 133 | + app.stop(); |
| 134 | + } |
| 135 | + started = false; |
| 136 | + logger.info("WebMap stopped."); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + private void findPort(boolean tryCurrentPort) |
| 141 | + { |
| 142 | + if (port == 0) |
| 143 | + { |
| 144 | + if (JourneymapClient.getInstance() == null || JourneymapClient.getInstance().getWebMapProperties() == null) |
| 145 | + { |
| 146 | + port = 8080; |
| 147 | + } |
| 148 | + else |
| 149 | + { |
| 150 | + var configuredPort = (String) JourneymapClient.getInstance().getWebMapProperties().port.get(); |
| 151 | + if (configuredPort == null) |
| 152 | + { |
| 153 | + port = 0; |
| 154 | + } |
| 155 | + else |
| 156 | + { |
| 157 | + this.port = Integer.parseInt(configuredPort); |
| 158 | + } |
| 159 | + logger.info("port found, set to " + port); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + if (tryCurrentPort) |
| 164 | + { |
| 165 | + try |
| 166 | + { |
| 167 | + ServerSocket socket = new ServerSocket(port); |
| 168 | + port = socket.getLocalPort(); |
| 169 | + socket.close(); |
| 170 | + } |
| 171 | + catch (IOException e) |
| 172 | + { |
| 173 | + logger.warn("Configured port " + port + " could not be bound: " + e); |
| 174 | + findPort(false); |
| 175 | + } |
| 176 | + |
| 177 | + logger.info("Configured port " + port + " is available."); |
| 178 | + } |
| 179 | + else |
| 180 | + { |
| 181 | + try |
| 182 | + { |
| 183 | + ServerSocket socket = new ServerSocket(0); |
| 184 | + port = socket.getLocalPort(); |
| 185 | + socket.close(); |
| 186 | + |
| 187 | + logger.info("New port " + port + " assigned by ServerSocket."); |
| 188 | + } |
| 189 | + catch (IOException e) |
| 190 | + { |
| 191 | + logger.error("Configured port {} could not be bound on second attempt, failing: ", port, e); |
| 192 | + stop(); |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + public int getPort() |
| 198 | + { |
| 199 | + return this.port; |
| 200 | + } |
| 201 | +} |
0 commit comments