65 lines
2.0 KiB
Java
65 lines
2.0 KiB
Java
package dev.dinauer.oidcproxy.startup;
|
|
|
|
import dev.dinauer.oidcproxy.startup.model.ConfigRoute;
|
|
import dev.dinauer.oidcproxy.startup.model.ConfigRules;
|
|
import io.quarkus.runtime.Startup;
|
|
import jakarta.enterprise.context.ApplicationScoped;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.Strings;
|
|
import org.apache.commons.lang3.tuple.Pair;
|
|
import tools.jackson.databind.ObjectMapper;
|
|
import tools.jackson.dataformat.yaml.YAMLFactory;
|
|
|
|
import java.io.File;
|
|
import java.util.LinkedList;
|
|
import java.util.List;
|
|
import java.util.Optional;
|
|
|
|
@ApplicationScoped
|
|
public class RouteService
|
|
{
|
|
private List<ProxyRoute> routes;
|
|
|
|
@Startup
|
|
void start()
|
|
{
|
|
ConfigRules rules = new ObjectMapper(new YAMLFactory()).readValue(new File("/home/andreas/Documents/dev/oidc-proxy/src/main/resources/routes.yaml"), ConfigRules.class);
|
|
|
|
List<ProxyRoute> result = new LinkedList<>();
|
|
for (ConfigRoute route : rules.routes())
|
|
{
|
|
if (StringUtils.isBlank(rules.root()))
|
|
{
|
|
result.add(new ProxyRoute(route.path(), route.target(), route.strategy()));
|
|
}
|
|
else
|
|
{
|
|
result.add(new ProxyRoute(StringUtils.join(rules.root(), route.path()), route.target(), route.strategy()));
|
|
}
|
|
}
|
|
this.routes = result;
|
|
}
|
|
|
|
public Optional<ProxyRoute> match(List<String> request)
|
|
{
|
|
Pair<Integer, ProxyRoute> best = null;
|
|
for (ProxyRoute proxyRoute : this.routes)
|
|
{
|
|
Optional<Integer> match = SegmentMatcher.match(proxyRoute.segments(), request);
|
|
if (match.isPresent())
|
|
{
|
|
int matchLength = match.get();
|
|
if (best == null || best.getLeft() < matchLength)
|
|
{
|
|
best = Pair.ofNonNull(matchLength, proxyRoute);
|
|
}
|
|
}
|
|
}
|
|
if (best != null)
|
|
{
|
|
return Optional.of(best.getRight());
|
|
}
|
|
return Optional.empty();
|
|
}
|
|
}
|