Skip to content

Commit

Permalink
feat(language-web): add health check endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
kris7t committed Dec 25, 2024
1 parent 269753f commit ec58e28
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public class CacheControlFilter implements Filter {
private static final Pattern CACHE_URI_PATTERN = Pattern.compile(".*\\.(css|gif|js|map|png|svg|woff2?)");

private static final Set<String> CACHE_URI_DENYLIST = Set.of("apple-touch-icon.png", "config.json", "favicon.png",
"favicon.svg", "favicon-96x96.png", "icon-any.svg", "icon-192x192.png", "icon-512x512.png", "mask-icon.svg",
"sw.js");
"favicon.svg", "favicon-96x96.png", "health", "icon-any.svg", "icon-192x192.png", "icon-512x512.png",
"mask-icon.svg", "sw.js");

private static final Duration EXPIRY = Duration.ofDays(365);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package tools.refinery.language.web;/*
* SPDX-FileCopyrightText: 2024 The Refinery Authors <https://refinery.tools/>
*
* SPDX-License-Identifier: EPL-2.0
*/

import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.Serial;

public class HealthCheckServlet extends HttpServlet {
@Serial
private static final long serialVersionUID = 5512947261931111041L;

private final transient Logger log = LoggerFactory.getLogger(getClass());

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
resp.setStatus(HttpServletResponse.SC_OK);
resp.setContentType("application/json");
try (var writer = resp.getWriter()) {
writer.println("{\"status\": \"up\"}");
writer.flush();
} catch (IOException e) {
log.error("Failed to write response", e);
if (!resp.isCommitted()) {
resp.reset();
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
package tools.refinery.language.web;

import jakarta.servlet.DispatcherType;
import jakarta.servlet.SessionTrackingMode;
import org.eclipse.jetty.ee10.servlet.DefaultServlet;
import org.eclipse.jetty.ee10.servlet.ServletContextHandler;
import org.eclipse.jetty.ee10.servlet.ServletHolder;
import org.eclipse.jetty.ee10.servlet.SessionHandler;
import org.eclipse.jetty.ee10.websocket.server.config.JettyWebSocketServletContainerInitializer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.resource.Resource;
Expand All @@ -30,7 +28,6 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.util.EnumSet;
import java.util.Set;

public class ServerLauncher {
public static final String DEFAULT_LISTEN_HOST = "localhost";
Expand All @@ -53,9 +50,9 @@ public ServerLauncher(InetSocketAddress bindAddress, String[] allowedOrigins, St
server = new Server(bindAddress);
((QueuedThreadPool) server.getThreadPool()).setName("jetty");
var handler = new ServletContextHandler();
addSessionHandler(handler);
addProblemServlet(handler, allowedOrigins);
addBackendConfigServlet(handler, webSocketUrl);
addHealthCheckServlet(handler);
var baseResource = getBaseResource();
if (baseResource != null) {
handler.setBaseResource(baseResource);
Expand All @@ -67,12 +64,6 @@ public ServerLauncher(InetSocketAddress bindAddress, String[] allowedOrigins, St
server.setHandler(handler);
}

private void addSessionHandler(ServletContextHandler handler) {
var sessionHandler = new SessionHandler();
sessionHandler.setSessionTrackingModes(Set.of(SessionTrackingMode.COOKIE));
handler.setSessionHandler(sessionHandler);
}

private void addProblemServlet(ServletContextHandler handler, String[] allowedOrigins) {
var problemServletHolder = new ServletHolder(ProblemWebSocketServlet.class);
if (allowedOrigins == null) {
Expand All @@ -93,6 +84,11 @@ private void addBackendConfigServlet(ServletContextHandler handler, String webSo
handler.addServlet(backendConfigServletHolder, "/config.json");
}

private void addHealthCheckServlet(ServletContextHandler handler) {
var healthCheckServletHolder = new ServletHolder(HealthCheckServlet.class);
handler.addServlet(healthCheckServletHolder, "/health");
}

private void addDefaultServlet(ServletContextHandler handler) {
var defaultServletHolder = new ServletHolder(DefaultServlet.class);
var isWindows = System.getProperty("os.name").toLowerCase().contains("win");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public void init(ServletConfig config) throws ServletException {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
resp.setStatus(HttpStatus.OK_200);
resp.setContentType("application/json");
try {
var writer = resp.getWriter();
try (var writer = resp.getWriter()) {
writer.write(serializedConfig);
writer.flush();
} catch (IOException e) {
Expand Down

0 comments on commit ec58e28

Please sign in to comment.