use of io.micronaut.gcp.function.http.HttpFunction in project micronaut-gcp by micronaut-projects.
the class InvokerHttpServer method start.
@Override
public EmbeddedServer start() {
if (running.compareAndSet(false, true)) {
int retryCount = 0;
while (retryCount <= 3) {
try {
this.server = new Server(port);
ServletContextHandler servletContextHandler = new ServletContextHandler();
servletContextHandler.setContextPath("/");
server.setHandler(NotFoundHandler.forServlet(servletContextHandler));
HttpFunction httpFunction = new HttpFunction() {
@Override
protected ApplicationContext buildApplicationContext(@Nullable Object context) {
ApplicationContext ctx = InvokerHttpServer.this.getApplicationContext();
this.applicationContext = ctx;
return ctx;
}
};
HttpServlet servlet = new HttpServlet() {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException {
try {
httpFunction.service(new HttpRequestImpl(req), new HttpResponseImpl(resp));
} catch (Exception e) {
throw new ServletException(e);
}
}
};
ServletHolder servletHolder = new ServletHolder(servlet);
servletHolder.getRegistration().setMultipartConfig(new MultipartConfigElement(""));
servletContextHandler.addServlet(servletHolder, "/*");
server.start();
logServerInfo();
break;
} catch (BindException e) {
if (randomPort) {
this.port = SocketUtils.findAvailableTcpPort();
retryCount++;
} else {
throw new ServerStartupException(e.getMessage(), e);
}
} catch (Exception e) {
throw new ServerStartupException("Error starting Google Cloud Function server: " + e.getMessage(), e);
}
}
}
return this;
}
Aggregations