2021-04-19 23:37:33 +08:00
|
|
|
|
package ink.wgink.gateway.handler.route;
|
|
|
|
|
|
|
|
|
|
import ink.wgink.gateway.dao.route.IRouteDao;
|
|
|
|
|
import ink.wgink.gateway.handler.BaseHandler;
|
2021-04-22 12:19:15 +08:00
|
|
|
|
import ink.wgink.gateway.pojo.result.SuccessResult;
|
2021-04-19 23:37:33 +08:00
|
|
|
|
import ink.wgink.gateway.pojo.route.Route;
|
2021-04-22 18:51:53 +08:00
|
|
|
|
import ink.wgink.gateway.util.UUIDUtil;
|
|
|
|
|
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
|
|
|
|
|
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
|
|
|
|
|
import org.springframework.cloud.gateway.route.RouteDefinition;
|
|
|
|
|
import org.springframework.context.ApplicationEvent;
|
|
|
|
|
import org.springframework.context.ApplicationEventPublisher;
|
|
|
|
|
import org.springframework.context.ApplicationEventPublisherAware;
|
2021-04-22 12:19:15 +08:00
|
|
|
|
import org.springframework.core.annotation.Order;
|
2021-04-20 12:32:15 +08:00
|
|
|
|
import org.springframework.data.domain.Example;
|
|
|
|
|
import org.springframework.data.domain.ExampleMatcher;
|
2021-04-22 12:19:15 +08:00
|
|
|
|
import org.springframework.data.domain.Page;
|
2021-04-19 23:37:33 +08:00
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
2021-04-22 12:19:15 +08:00
|
|
|
|
import org.springframework.stereotype.Service;
|
2021-04-19 23:37:33 +08:00
|
|
|
|
import org.springframework.web.reactive.function.server.ServerRequest;
|
|
|
|
|
import org.springframework.web.reactive.function.server.ServerResponse;
|
2021-04-22 18:51:53 +08:00
|
|
|
|
import org.springframework.web.util.UriComponentsBuilder;
|
2021-04-22 12:19:15 +08:00
|
|
|
|
import reactor.core.publisher.Flux;
|
2021-04-19 23:37:33 +08:00
|
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
|
2021-04-22 18:51:53 +08:00
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2021-04-19 23:37:33 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* When you feel like quitting. Think about why you started
|
|
|
|
|
* 当你想要放弃的时候,想想当初你为何开始
|
|
|
|
|
*
|
|
|
|
|
* @ClassName: RouteHandler
|
|
|
|
|
* @Description: 路由
|
|
|
|
|
* @Author: WangGeng
|
|
|
|
|
* @Date: 2021/4/19 21:40
|
|
|
|
|
* @Version: 1.0
|
|
|
|
|
**/
|
2021-04-22 12:19:15 +08:00
|
|
|
|
@Service
|
2021-04-22 18:51:53 +08:00
|
|
|
|
public class RouteHandler extends BaseHandler implements ApplicationEventPublisherAware {
|
2021-04-19 23:37:33 +08:00
|
|
|
|
|
|
|
|
|
private IRouteDao routeDao;
|
2021-04-22 18:51:53 +08:00
|
|
|
|
private ApplicationEventPublisher applicationEventPublisher;
|
2021-04-19 23:37:33 +08:00
|
|
|
|
|
|
|
|
|
public RouteHandler(IRouteDao routeDao) {
|
|
|
|
|
this.routeDao = routeDao;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 新增
|
|
|
|
|
*
|
|
|
|
|
* @param serverRequest
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public Mono<ServerResponse> save(ServerRequest serverRequest) {
|
|
|
|
|
Mono<Route> routeMono = serverRequest.bodyToMono(Route.class);
|
|
|
|
|
return routeMono.flatMap(route -> {
|
|
|
|
|
setSave(route);
|
|
|
|
|
return routeDao.save(route);
|
2021-04-22 18:51:53 +08:00
|
|
|
|
}).then(ServerResponse.ok().contentType(MediaType.APPLICATION_JSON_UTF8).body(Flux.just(new SuccessResult()), SuccessResult.class));
|
2021-04-19 23:37:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除
|
|
|
|
|
*
|
|
|
|
|
* @param serverRequest
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public Mono<ServerResponse> delete(ServerRequest serverRequest) {
|
|
|
|
|
String ids = serverRequest.pathVariable("ids");
|
|
|
|
|
List<String> idList = Arrays.asList(ids.split(","));
|
2021-04-20 12:32:15 +08:00
|
|
|
|
return routeDao.findAllById(idList).flatMap(
|
|
|
|
|
route -> routeDao.delete(route)
|
2021-04-22 18:51:53 +08:00
|
|
|
|
).then(ServerResponse.ok().contentType(MediaType.APPLICATION_JSON_UTF8).body(Mono.just(new SuccessResult()), SuccessResult.class));
|
2021-04-19 23:37:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 修改
|
|
|
|
|
*
|
|
|
|
|
* @param serverRequest
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public Mono<ServerResponse> update(ServerRequest serverRequest) {
|
|
|
|
|
String id = serverRequest.pathVariable("id");
|
|
|
|
|
Mono<Route> routeMono = serverRequest.bodyToMono(Route.class);
|
2021-04-20 12:32:15 +08:00
|
|
|
|
return routeDao.findById(id).flatMap(
|
|
|
|
|
route -> routeMono.flatMap(r -> {
|
2021-04-22 12:19:15 +08:00
|
|
|
|
route.setTitle(r.getTitle());
|
2021-04-20 12:32:15 +08:00
|
|
|
|
route.setSummary(r.getSummary());
|
|
|
|
|
route.setPath(r.getPath());
|
|
|
|
|
route.setUri(r.getUri());
|
|
|
|
|
setUpdate(route);
|
|
|
|
|
return routeDao.save(route);
|
2021-04-22 18:51:53 +08:00
|
|
|
|
}).then(ServerResponse.ok().contentType(MediaType.APPLICATION_JSON_UTF8).body(Flux.just(new SuccessResult()), SuccessResult.class))
|
2021-04-20 12:32:15 +08:00
|
|
|
|
).switchIfEmpty(ServerResponse.notFound().build());
|
2021-04-19 23:37:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 列表
|
|
|
|
|
*
|
|
|
|
|
* @param serverRequest
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public Mono<ServerResponse> list(ServerRequest serverRequest) {
|
2021-04-21 22:50:01 +08:00
|
|
|
|
Optional<String> keywords = serverRequest.queryParam("keywords");
|
|
|
|
|
|
2021-04-20 12:32:15 +08:00
|
|
|
|
Route route = new Route();
|
2021-04-21 22:50:01 +08:00
|
|
|
|
Example example = Example.of(route);
|
|
|
|
|
if (keywords.isPresent()) {
|
2021-04-22 12:19:15 +08:00
|
|
|
|
route.setTitle(keywords.get());
|
2021-04-21 22:50:01 +08:00
|
|
|
|
route.setSummary(keywords.get());
|
|
|
|
|
|
|
|
|
|
ExampleMatcher exampleMatcher = ExampleMatcher.matchingAny()
|
2021-04-22 12:19:15 +08:00
|
|
|
|
.withMatcher("title", ExampleMatcher.GenericPropertyMatcher::contains)
|
2021-04-21 22:50:01 +08:00
|
|
|
|
.withMatcher("summary", ExampleMatcher.GenericPropertyMatcher::contains)
|
|
|
|
|
.withIgnoreCase("id");
|
|
|
|
|
example = Example.of(route, exampleMatcher);
|
2021-04-20 12:32:15 +08:00
|
|
|
|
}
|
2021-04-22 18:51:53 +08:00
|
|
|
|
return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON_UTF8).body(routeDao.findAll(example), Route.class);
|
|
|
|
|
}
|
2021-04-20 12:32:15 +08:00
|
|
|
|
|
2021-04-19 23:37:33 +08:00
|
|
|
|
/**
|
|
|
|
|
* 列表
|
|
|
|
|
*
|
|
|
|
|
* @param serverRequest
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public Mono<ServerResponse> get(ServerRequest serverRequest) {
|
|
|
|
|
String id = serverRequest.pathVariable("id");
|
2021-04-20 12:32:15 +08:00
|
|
|
|
return routeDao.findById(id).flatMap(
|
2021-04-22 18:51:53 +08:00
|
|
|
|
route -> ServerResponse.ok().contentType(MediaType.APPLICATION_JSON_UTF8).body(Mono.just(route), Route.class)
|
2021-04-20 12:32:15 +08:00
|
|
|
|
).switchIfEmpty(ServerResponse.notFound().build());
|
2021-04-19 23:37:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 18:51:53 +08:00
|
|
|
|
/**
|
|
|
|
|
* 通知刷新路由
|
|
|
|
|
*
|
|
|
|
|
* @param serverRequest
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public Mono<ServerResponse> refresh(ServerRequest serverRequest) {
|
|
|
|
|
this.applicationEventPublisher.publishEvent(new RefreshRoutesEvent(this));
|
|
|
|
|
return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(Mono.just(new SuccessResult()), SuccessResult.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
|
|
|
|
|
this.applicationEventPublisher = applicationEventPublisher;
|
|
|
|
|
}
|
2021-04-19 23:37:33 +08:00
|
|
|
|
}
|