Skip to content

Commit 9519bba

Browse files
committed
switch to serving via javalin
1 parent 00d1d04 commit 9519bba

File tree

1 file changed

+20
-26
lines changed
  • photon-server/src/main/java/org/photonvision

1 file changed

+20
-26
lines changed

photon-server/src/main/java/org/photonvision/Main.java

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -189,41 +189,35 @@ public static void main(String[] args) {
189189

190190
try {
191191
int port = 5800;
192-
com.sun.net.httpserver.HttpServer server = null;
192+
io.javalin.Javalin app = null;
193193
try {
194-
server =
195-
com.sun.net.httpserver.HttpServer.create(new java.net.InetSocketAddress(port), 0);
194+
app = io.javalin.Javalin.create(cfg -> cfg.showJavalinBanner = false).start(port);
196195
} catch (Exception e) {
197196
logger.warn("Failed to bind to port 5800, exiting: " + e.getMessage());
198197
port = DEFAULT_WEBPORT;
199-
server =
200-
com.sun.net.httpserver.HttpServer.create(new java.net.InetSocketAddress(port), 0);
198+
app = io.javalin.Javalin.create(cfg -> cfg.showJavalinBanner = false).start(port);
201199
}
202200

203201
final int boundPort = port;
204-
server.createContext(
202+
final String html =
203+
"<!doctype html>"
204+
+ "<html><head><meta charset=\"utf-8\"><title>Unsupported platform</title></head><body>"
205+
+ "<p>Main Robot Controllers shouldn't run PhotonVision, but yours does! Please uninstall PhotonVision. "
206+
+ "If you choose to modify PhotonVision so that it functions on SystemCore, "
207+
+ "you do so entirely at your own risk and without any support. "
208+
+ "For more information, see <a href=\""
209+
+ docsLink
210+
+ "\" target=\"_blank\" rel=\"noopener noreferrer\">"
211+
+ docsLink
212+
+ "</a>.</p></body></html>";
213+
214+
app.get(
205215
"/",
206-
exchange -> {
207-
String html =
208-
"<!doctype html>"
209-
+ "<html><head><meta charset=\"utf-8\"><title>Unsupported platform</title></head><body>"
210-
+ "<p>Main Robot Controllers shouldn't run PhotonVision, but yours does! Please uninstall PhotonVision. "
211-
+ "If you choose to modify PhotonVision so that it functions on SystemCore, "
212-
+ "you do so entirely at your own risk and without any support. "
213-
+ "For more information, see <a href=\""
214-
+ docsLink
215-
+ "\" target=\"_blank\" rel=\"noopener noreferrer\">"
216-
+ docsLink
217-
+ "</a>.</p></body></html>";
218-
byte[] resp = html.getBytes(java.nio.charset.StandardCharsets.UTF_8);
219-
exchange.getResponseHeaders().set("Content-Type", "text/html; charset=utf-8");
220-
exchange.sendResponseHeaders(200, resp.length);
221-
try (java.io.OutputStream os = exchange.getResponseBody()) {
222-
os.write(resp);
223-
}
216+
ctx -> {
217+
ctx.contentType("text/html; charset=utf-8");
218+
ctx.result(html);
224219
});
225-
server.setExecutor(java.util.concurrent.Executors.newSingleThreadExecutor());
226-
server.start();
220+
227221
logger.info(
228222
"Served SystemCore warning page on port "
229223
+ boundPort

0 commit comments

Comments
 (0)