|
|
|
|
package com.dsideal.gw;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
|
|
import com.dsideal.gw.Contoller.IndexController;
|
|
|
|
|
import com.dsideal.gw.Handler.RouterHandler;
|
|
|
|
|
import com.dsideal.gw.Plugin.YamlProp;
|
|
|
|
|
import com.dsideal.gw.Util.LogBackLogFactory;
|
|
|
|
|
import com.jfinal.config.*;
|
|
|
|
|
import com.jfinal.kit.Prop;
|
|
|
|
|
import com.jfinal.plugin.redis.RedisPlugin;
|
|
|
|
|
import com.jfinal.server.undertow.UndertowServer;
|
|
|
|
|
import com.jfinal.template.Engine;
|
|
|
|
|
import lombok.SneakyThrows;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.yaml.snakeyaml.Yaml;
|
|
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
public class GwApplication extends JFinalConfig {
|
|
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(GwApplication.class);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:获取是否为开发环境
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String getEnvPrefix() {
|
|
|
|
|
String myEnvVar = System.getenv("WORKING_ENV");
|
|
|
|
|
if (myEnvVar == null) {
|
|
|
|
|
myEnvVar = "dev";
|
|
|
|
|
}
|
|
|
|
|
return myEnvVar;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
System.out.println("当前环境: " + getEnvPrefix());
|
|
|
|
|
String configFile = "undertow.properties";
|
|
|
|
|
UndertowServer.create(GwApplication.class, configFile).start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//路由表
|
|
|
|
|
public static Map<String, String> routeList = new HashMap<>();
|
|
|
|
|
//白名单
|
|
|
|
|
public static Set<String> whiteSet = new HashSet<>();
|
|
|
|
|
|
|
|
|
|
public static Prop PropKit;
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
//加载配置文件
|
|
|
|
|
String configFile = "application_{?}.yaml".replace("{?}", getEnvPrefix());
|
|
|
|
|
PropKit = new YamlProp(configFile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 配置常量
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void configConstant(Constants me) {
|
|
|
|
|
//使用LogBack
|
|
|
|
|
me.setLogFactory(new LogBackLogFactory());
|
|
|
|
|
|
|
|
|
|
// 设置静态根目录为上传根目录
|
|
|
|
|
me.setBaseUploadPath(PropKit.get("uploadTempPath"));
|
|
|
|
|
|
|
|
|
|
// 获取所有配置项,得到路由表
|
|
|
|
|
for (Map.Entry<Object, Object> entry : PropKit.getProperties().entrySet()) {
|
|
|
|
|
if (entry.getKey().toString().startsWith("route")) {
|
|
|
|
|
routeList.put(entry.getKey().toString().substring(6).replace(".url", ""), entry.getValue().toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 获取白名单链接列表
|
|
|
|
|
Yaml yaml = new Yaml();
|
|
|
|
|
List<String> whitelist = yaml.load(PropKit.get("whitelist"));
|
|
|
|
|
whiteSet.addAll(whitelist);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 配置路由
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void configRoute(Routes me) {
|
|
|
|
|
me.add("/", IndexController.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void configEngine(Engine engine) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void configPlugin(Plugins me) {
|
|
|
|
|
//加载Redis插件
|
|
|
|
|
RedisPlugin redis = new RedisPlugin("Redis", PropKit.get("redis.ip"),PropKit.getInt("redis.port"), 10000);
|
|
|
|
|
me.add(redis);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 配置全局拦截器
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void configInterceptor(Interceptors me) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 配置处理器
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void configHandler(Handlers me) {
|
|
|
|
|
//路由处理器
|
|
|
|
|
me.add(new RouterHandler());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 在jfinal启动完成后马上执行
|
|
|
|
|
*/
|
|
|
|
|
@SneakyThrows
|
|
|
|
|
@Override
|
|
|
|
|
public void onStart() {
|
|
|
|
|
//打印 启动Logo
|
|
|
|
|
System.out.println(FileUtil.readUtf8String("logo.txt"));
|
|
|
|
|
}
|
|
|
|
|
}
|