package org.dominokit.domino.api.server.plugins;

import com.google.auto.service.AutoService;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.StaticHandler;
import org.dominokit.domino.api.server.DominoLoaderPlugin;
import org.dominokit.domino.api.server.PluginContext;

import java.nio.file.Paths;

import static java.util.Objects.nonNull;

@AutoService(DominoLoaderPlugin.class)
public class StaticResourcesConfiguratorPlugin extends BaseDominoLoaderPlugin {

    @Override
    public void applyPlugin(CompleteHandler completeHandler) {
        StaticHandler staticHandler = StaticHandler.create();
        if (nonNull(System.getProperty("domino.webroot.location"))) {
            staticHandler.setAllowRootFileSystemAccess(true);
            staticHandler.setWebRoot(systemWebRoot());
        } else {
            staticHandler.setWebRoot(context.getWebRoot());
        }

        context.getRouter().route("/").order(Integer.MAX_VALUE - 3)
                .handler(this::serveIndexPage);

        context.getRouter().route("/static/*").order(Integer.MAX_VALUE - 2)
                .handler(staticHandler)
                .failureHandler(this::serveResource);

        context.getRouter().route("/*").order(Integer.MAX_VALUE)
                .handler(staticHandler)
                .handler(this::resourceNotFound)
                .failureHandler(this::serveIndexPage);

        completeHandler.onCompleted();
    }

    private HttpServerResponse resourceNotFound(RoutingContext routingContext) {
        return routingContext.response()
                .putHeader("Content-type", "text/html")
                .setStatusCode(404)
                .sendFile(context.getWebRoot() + "/index.html");
    }

    private String systemWebRoot() {
        return Paths.get(System.getProperty("domino.webroot.location")).toAbsolutePath().toString();
    }

    private HttpServerResponse serveIndexPage(RoutingContext event) {
        return event.response().putHeader("Content-type", "text/html")
                .sendFile(context.getWebRoot() + "/index.html");
    }

    private void serveResource(RoutingContext routingContext) {
        if (!routingContext.response().ended()) {
            routingContext.response()
                    .sendFile(context.getWebRoot() + routingContext.request().path().replace("/static", ""), event -> {
                        if (event.failed())
                            serveIndexPage(routingContext);
                    });
        }
    }

    @Override
    public int order() {
        return PluginContext.STATIC_RESOURCES_ORDER;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}

@@ -133,16 +133,47 @@
 sponse;%0A
+import io.vertx.ext.web.Route;%0A
 import i
@@ -645,202 +645,125 @@
     
-StaticHandler staticHandler = StaticHandler.create();%0A        if (nonNull(System.getProperty(%22domino.webroot.location%22))) %7B%0A            staticHandler.setAllowRootFileSystemAccess(tru
+context.getRouter().route(%22/%22).order(Integer.MAX_VALUE - 2)%0A                .handler(this::serveIndexPag
 e);%0A
+%0A
         
     
@@ -762,131 +762,70 @@
     
-    staticHandler.setWebRoot(systemWebRoot());%0A        %7D else %7B%0A            staticHandler.setWebRoot(context.getWebRoot());
+Route route = context.getRouter()%0A                .route(%22/*%22)
 %0A   
@@ -833,19 +833,16 @@
     
-%7D%0A%0A
         
 cont
@@ -841,38 +841,8 @@
     
-context.getRouter().route(%22/%22)
 .ord
@@ -865,12 +865,8 @@
 ALUE
- - 3
 )%0A  
@@ -892,40 +892,74 @@
 ler(
-this::serveIndexPage
+StaticHandler.create().setWebRoot(context.getWebRoot())
 );%0A
-%0A
         
 cont
@@ -958,102 +958,120 @@
     
-context.getRouter().route(%22/static/*%22).order(Integer.MAX_VALUE - 2)%0A                .h
+if (nonNull(System.getProperty(%22domino.webroot.location%22))) %7B%0A            StaticHandler webRootStaticH
 andler
-(s
+ = S
 tati
@@ -1070,33 +1070,42 @@
  = StaticHandler
-)
+.create();
 %0A               
@@ -1105,115 +1105,126 @@
     
-    .failureHandler(this::serveResource);%0A%0A        context.getRouter().route(%22/*%22).order(Integer.MAX_VALUE)
+webRootStaticHandler.setAllowRootFileSystemAccess(true);%0A            webRootStaticHandler.setWebRoot(systemWebRoot());
 %0A   
@@ -1224,36 +1224,37 @@
 ));%0A            
-    
+route
 .handler(staticH
@@ -1246,17 +1246,24 @@
 handler(
-s
+webRootS
 taticHan
@@ -1267,16 +1267,41 @@
 Handler)
+;%0A        %7D%0A        route
 %0A       
@@ -1380,34 +1380,35 @@
 r(this::
-serveIndexPage
+resourceNotFound
 );%0A
-%0A
         
@@ -2093,425 +2093,8 @@
  %7D%0A%0A
-    private void serveResource(RoutingContext routingContext) %7B%0A        if (!routingContext.response().ended()) %7B%0A            routingContext.response()%0A                    .sendFile(context.getWebRoot() + routingContext.request().path().replace(%22/static%22, %22%22), event -%3E %7B%0A                        if (event.failed())%0A                            serveIndexPage(routingContext);%0A                    %7D);%0A        %7D%0A    %7D%0A%0A
     

fix path traversal