Search in sources :

Example 1 with HttpFunction

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;
}
Also used : Server(org.eclipse.jetty.server.Server) EmbeddedServer(io.micronaut.runtime.server.EmbeddedServer) HttpServlet(javax.servlet.http.HttpServlet) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletException(javax.servlet.ServletException) HttpServerException(io.micronaut.http.server.exceptions.HttpServerException) ServerStartupException(io.micronaut.http.server.exceptions.ServerStartupException) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ApplicationContext(io.micronaut.context.ApplicationContext) MultipartConfigElement(javax.servlet.MultipartConfigElement) ServerStartupException(io.micronaut.http.server.exceptions.ServerStartupException) HttpFunction(io.micronaut.gcp.function.http.HttpFunction) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Nullable(io.micronaut.core.annotation.Nullable)

Aggregations

ApplicationContext (io.micronaut.context.ApplicationContext)1 Nullable (io.micronaut.core.annotation.Nullable)1 HttpFunction (io.micronaut.gcp.function.http.HttpFunction)1 HttpServerException (io.micronaut.http.server.exceptions.HttpServerException)1 ServerStartupException (io.micronaut.http.server.exceptions.ServerStartupException)1 EmbeddedServer (io.micronaut.runtime.server.EmbeddedServer)1 IOException (java.io.IOException)1 MultipartConfigElement (javax.servlet.MultipartConfigElement)1 ServletException (javax.servlet.ServletException)1 HttpServlet (javax.servlet.http.HttpServlet)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Server (org.eclipse.jetty.server.Server)1 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)1 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)1