|
|
|
@ -1,51 +1,67 @@
|
|
|
|
|
package com.dsideal.gw.Controller;
|
|
|
|
|
package com.dsideal.gw.Interceptor;
|
|
|
|
|
|
|
|
|
|
import com.dsideal.gw.Start;
|
|
|
|
|
import com.dsideal.gw.gwApplication;
|
|
|
|
|
import com.jfinal.aop.Interceptor;
|
|
|
|
|
import com.jfinal.aop.Invocation;
|
|
|
|
|
import com.jfinal.core.Controller;
|
|
|
|
|
import okhttp3.MediaType;
|
|
|
|
|
import okhttp3.OkHttpClient;
|
|
|
|
|
import okhttp3.Request;
|
|
|
|
|
import okhttp3.RequestBody;
|
|
|
|
|
import okhttp3.Response;
|
|
|
|
|
import okhttp3.*;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
public class ApiController extends Controller {
|
|
|
|
|
|
|
|
|
|
public class RuteInterceptor implements Interceptor {
|
|
|
|
|
private final OkHttpClient client = new OkHttpClient();
|
|
|
|
|
private final List<Map<String, String>> routes;
|
|
|
|
|
|
|
|
|
|
public ApiController() {
|
|
|
|
|
Map<String, Object> jfinalConfig = (Map<String, Object>) Start.yamlConfig.get("jfinal");
|
|
|
|
|
public RuteInterceptor() {
|
|
|
|
|
Map<String, Object> jfinalConfig = (Map<String, Object>) gwApplication.yamlConfig.get("jfinal");
|
|
|
|
|
routes = (List<Map<String, String>>) jfinalConfig.get("routes");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:路由
|
|
|
|
|
*
|
|
|
|
|
* @param path
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private String route(String path) {
|
|
|
|
|
if (routes != null) {
|
|
|
|
|
for (Map<String, String> route : routes) {
|
|
|
|
|
String prefix = route.get("prefix");
|
|
|
|
|
if (path.startsWith(prefix)) {
|
|
|
|
|
return route.get("url") + path;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void index() {
|
|
|
|
|
String path = getRequest().getRequestURI(); // 获取请求路径
|
|
|
|
|
if (path.equals("/")) {
|
|
|
|
|
renderText("Welcome to JFinal Gateway.");
|
|
|
|
|
@Override
|
|
|
|
|
public void intercept(Invocation inv) {
|
|
|
|
|
Controller controller = inv.getController();
|
|
|
|
|
String path = controller.getRequest().getRequestURI();
|
|
|
|
|
if(path.equals("/")){
|
|
|
|
|
inv.invoke();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
String serviceUrl = route(path); // 根据路径决定转发到哪个微服务
|
|
|
|
|
|
|
|
|
|
if (serviceUrl == null) {
|
|
|
|
|
renderJson("error", "服务未找到");
|
|
|
|
|
controller.renderJson("error", "服务未找到");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
//方法类型
|
|
|
|
|
String method = getRequest().getMethod();
|
|
|
|
|
String method = controller.getRequest().getMethod();
|
|
|
|
|
//传递jwt token
|
|
|
|
|
Request.Builder requestBuilder = new Request.Builder()
|
|
|
|
|
.url(serviceUrl)
|
|
|
|
|
.header("Authorization", getHeader("Authorization"));
|
|
|
|
|
.header("Authorization", controller.getHeader("Authorization"));
|
|
|
|
|
|
|
|
|
|
if ("POST".equalsIgnoreCase(method)) {
|
|
|
|
|
String jsonPayload = getRawData(); // 获取请求体中的JSON数据
|
|
|
|
|
String jsonPayload = controller.getRawData(); // 获取请求体中的JSON数据
|
|
|
|
|
MediaType JSON = MediaType.get("application/json; charset=utf-8");
|
|
|
|
|
RequestBody body = RequestBody.create(JSON, jsonPayload);
|
|
|
|
|
requestBuilder.post(body);
|
|
|
|
@ -56,32 +72,15 @@ public class ApiController extends Controller {
|
|
|
|
|
|
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
|
if (response.body() != null) {
|
|
|
|
|
renderJson(response.body().string());
|
|
|
|
|
controller.renderJson(response.body().string());
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
renderJson("error", "服务请求失败");
|
|
|
|
|
controller.renderJson("error", "服务请求失败");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
renderJson("error", "发生异常: " + e.getMessage());
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:路由
|
|
|
|
|
*
|
|
|
|
|
* @param path
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private String route(String path) {
|
|
|
|
|
if (routes != null) {
|
|
|
|
|
for (Map<String, String> route : routes) {
|
|
|
|
|
String prefix = route.get("prefix");
|
|
|
|
|
if (path.startsWith(prefix)) {
|
|
|
|
|
return route.get("url") + path;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
inv.invoke();
|
|
|
|
|
}
|
|
|
|
|
}
|