package com.eshore.gringotts.gateway;

import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import java.util.function.Function;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.Buildable;
import org.springframework.cloud.gateway.route.builder.PredicateSpec;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;

/**
 * 启动类
 *
 * @author lanyuanxiaoyao
 * @date 2024-01-15
 */
@SpringBootApplication(scanBasePackages = "com.eshore.gringotts")
@EnableConfigurationProperties
@EnableEncryptableProperties
public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }

    private Function<PredicateSpec, Buildable<Route>> createRoute(String prefix, String target) {
        return p -> p
                .path(prefix + "/**")
                .filters(filter -> filter.rewritePath("^" + prefix, ""))
                .uri(target);
    }

    @Bean
    public RouteLocator routeLocator(RouteLocatorBuilder builder) {
        return builder.routes()
                .route("web", createRoute("/gringotts/web", "lb://gringotts-web"))
                .build();
    }
}
