|
| 1 | +package com.slack.api.bolt.jakarta_jetty; |
| 2 | + |
| 3 | +import com.slack.api.bolt.App; |
| 4 | +import com.slack.api.bolt.WebEndpoint; |
| 5 | +import com.slack.api.bolt.handler.WebEndpointHandler; |
| 6 | +import com.slack.api.bolt.jakarta_servlet.SlackAppServlet; |
| 7 | +import com.slack.api.bolt.jakarta_servlet.SlackOAuthAppServlet; |
| 8 | +import com.slack.api.bolt.jakarta_servlet.WebEndpointServlet; |
| 9 | +import lombok.extern.slf4j.Slf4j; |
| 10 | +import org.eclipse.jetty.server.ConnectionFactory; |
| 11 | +import org.eclipse.jetty.server.Connector; |
| 12 | +import org.eclipse.jetty.server.HttpConnectionFactory; |
| 13 | +import org.eclipse.jetty.server.Server; |
| 14 | +import org.eclipse.jetty.server.handler.ErrorHandler; |
| 15 | +import org.eclipse.jetty.servlet.ServletContextHandler; |
| 16 | +import org.eclipse.jetty.servlet.ServletHolder; |
| 17 | + |
| 18 | +import jakarta.servlet.http.HttpServletRequest; |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.Writer; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +/** |
| 25 | + * An HTTP server backed by Jetty HTTP Server that runs {@link App} apps. |
| 26 | + * |
| 27 | + * @see <a href="https://www.eclipse.org/jetty/">Jetty HTTP Server</a> |
| 28 | + */ |
| 29 | +@Slf4j |
| 30 | +public class SlackAppServer { |
| 31 | + |
| 32 | + private final Server server; |
| 33 | + private final Map<String, App> pathToApp; |
| 34 | + private final boolean localDebug = System.getenv("SLACK_APP_LOCAL_DEBUG") != null; |
| 35 | + |
| 36 | + // This is intentionally mutable to allow developers to register their own one |
| 37 | + private ErrorHandler errorHandler = new ErrorHandler() { |
| 38 | + @Override |
| 39 | + protected void writeErrorPage( |
| 40 | + HttpServletRequest request, |
| 41 | + Writer writer, |
| 42 | + int code, |
| 43 | + String message, |
| 44 | + boolean showStacks) throws IOException { |
| 45 | + if (localDebug) { |
| 46 | + super.writeErrorPage(request, writer, code, message, showStacks); |
| 47 | + } else { |
| 48 | + writer.write("{\"status\":\"" + code + "\"}"); |
| 49 | + } |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + public SlackAppServer(App app) { |
| 54 | + this(app, "/slack/events", 3000); |
| 55 | + } |
| 56 | + |
| 57 | + public SlackAppServer(App app, String path) { |
| 58 | + this(app, path, 3000); |
| 59 | + } |
| 60 | + |
| 61 | + public SlackAppServer(App app, int port) { |
| 62 | + this(toApps(app, "/slack/events"), port); |
| 63 | + } |
| 64 | + |
| 65 | + public SlackAppServer(App app, String path, int port) { |
| 66 | + this(toApps(app, path), port); |
| 67 | + } |
| 68 | + |
| 69 | + public SlackAppServer(Map<String, App> pathToApp) { |
| 70 | + this(pathToApp, 3000); |
| 71 | + } |
| 72 | + |
| 73 | + public SlackAppServer(Map<String, App> pathToApp, int port) { |
| 74 | + this.pathToApp = pathToApp; |
| 75 | + server = new Server(port); |
| 76 | + removeServerHeader(server); |
| 77 | + |
| 78 | + ServletContextHandler handler = new ServletContextHandler(); |
| 79 | + Map<String, App> addedOnes = new HashMap<>(); |
| 80 | + for (Map.Entry<String, App> entry : this.pathToApp.entrySet()) { |
| 81 | + String appPath = entry.getKey(); |
| 82 | + App theApp = entry.getValue(); |
| 83 | + theApp.config().setAppPath(appPath); |
| 84 | + handler.addServlet(new ServletHolder(new SlackAppServlet(theApp)), appPath); |
| 85 | + |
| 86 | + if (theApp.config().isOAuthInstallPathEnabled() || theApp.config().isOAuthStartEnabled()) { |
| 87 | + if (theApp.config().isDistributedApp()) { |
| 88 | + // start |
| 89 | + String installPath = appPath + theApp.config().getOauthInstallPath(); |
| 90 | + App installPathApp = theApp.toOAuthInstallPathEnabledApp(); |
| 91 | + handler.addServlet(new ServletHolder(new SlackOAuthAppServlet(installPathApp)), installPath); |
| 92 | + addedOnes.put(installPath, installPathApp); |
| 93 | + } else { |
| 94 | + log.warn("The app is not ready for handling your Slack App installation URL. Make sure if you set all the necessary values in AppConfig."); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if (theApp.config().isOAuthRedirectUriPathEnabled() || theApp.config().isOAuthCallbackEnabled()) { |
| 99 | + if (theApp.config().isDistributedApp()) { |
| 100 | + // callback |
| 101 | + String redirectUriPath = appPath + theApp.config().getOauthRedirectUriPath(); |
| 102 | + App oAuthCallbackApp = theApp.toOAuthRedirectUriPathEnabledApp(); |
| 103 | + handler.addServlet(new ServletHolder(new SlackOAuthAppServlet(oAuthCallbackApp)), redirectUriPath); |
| 104 | + addedOnes.put(redirectUriPath, oAuthCallbackApp); |
| 105 | + } else { |
| 106 | + log.warn("The app is not ready for handling OAuth callback requests. Make sure if you set all the necessary values in AppConfig."); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // Register additional web endpoints |
| 111 | + if (theApp.getWebEndpointHandlers() != null && theApp.getWebEndpointHandlers().size() > 0) { |
| 112 | + for (Map.Entry<WebEndpoint, WebEndpointHandler> ee : theApp.getWebEndpointHandlers().entrySet()) { |
| 113 | + WebEndpoint endpoint = ee.getKey(); |
| 114 | + WebEndpointHandler endpointHandler = ee.getValue(); |
| 115 | + ServletHolder servletHolder = new ServletHolder( |
| 116 | + new WebEndpointServlet(endpoint, endpointHandler, theApp.config())); |
| 117 | + handler.addServlet(servletHolder, endpoint.getPath()); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + pathToApp.putAll(addedOnes); |
| 122 | + server.setHandler(handler); |
| 123 | + server.setErrorHandler(errorHandler); |
| 124 | + } |
| 125 | + |
| 126 | + public void start() throws Exception { |
| 127 | + for (App app : pathToApp.values()) { |
| 128 | + app.start(); |
| 129 | + } |
| 130 | + server.start(); |
| 131 | + log.info("⚡️ Bolt app is running!"); |
| 132 | + server.join(); |
| 133 | + } |
| 134 | + |
| 135 | + public void stop() throws Exception { |
| 136 | + for (App app : pathToApp.values()) { |
| 137 | + app.stop(); |
| 138 | + } |
| 139 | + log.info("⚡️ Your Bolt app has stopped..."); |
| 140 | + server.stop(); |
| 141 | + } |
| 142 | + |
| 143 | + public ErrorHandler getErrorHandler() { |
| 144 | + return errorHandler; |
| 145 | + } |
| 146 | + |
| 147 | + public void setErrorHandler(ErrorHandler errorHandler) { |
| 148 | + this.errorHandler = errorHandler; |
| 149 | + } |
| 150 | + |
| 151 | + // ---------------------------------------------------- |
| 152 | + // internal methods |
| 153 | + // ---------------------------------------------------- |
| 154 | + |
| 155 | + private static Map<String, App> toApps(App app, String path) { |
| 156 | + Map<String, App> apps = new HashMap<>(); |
| 157 | + apps.put(path, app); |
| 158 | + return apps; |
| 159 | + } |
| 160 | + |
| 161 | + private static void removeServerHeader(Server server) { |
| 162 | + // https://stackoverflow.com/a/15675075/840108 |
| 163 | + for (Connector y : server.getConnectors()) { |
| 164 | + for (ConnectionFactory x : y.getConnectionFactories()) { |
| 165 | + if (x instanceof HttpConnectionFactory) { |
| 166 | + ((HttpConnectionFactory) x).getHttpConfiguration().setSendServerVersion(false); |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | +} |
0 commit comments