|
|
|
|
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.kit.PropKit;
|
|
|
|
|
import com.jfinal.server.undertow.UndertowServer;
|
|
|
|
|
import com.jfinal.template.Engine;
|
|
|
|
|
import org.yaml.snakeyaml.Yaml;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
public class GwApplication extends JFinalConfig {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:获取是否为开发环境
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String getEnvPrefix() {
|
|
|
|
|
String myEnvVar = System.getenv("WORKING_ENV");
|
|
|
|
|
return myEnvVar == null ? "dev" : "pro";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
System.out.println("当前环境: " + getEnvPrefix());
|
|
|
|
|
String configFile = "undertow_{?}.properties".replace("{?}", getEnvPrefix());
|
|
|
|
|
UndertowServer.create(GwApplication.class, configFile).start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//路由表
|
|
|
|
|
public static Map<String, String> routeList = new HashMap<>();
|
|
|
|
|
//白名单
|
|
|
|
|
public static Set<String> whiteSet = new HashSet<>();
|
|
|
|
|
/**
|
|
|
|
|
* 配置常量
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void configConstant(Constants me) {
|
|
|
|
|
//使用LogBack
|
|
|
|
|
me.setLogFactory(new LogBackLogFactory());
|
|
|
|
|
//加载配置文件
|
|
|
|
|
String configFile = "application_{?}.yaml".replace("{?}", getEnvPrefix());
|
|
|
|
|
Prop PropKit = new YamlProp(configFile);
|
|
|
|
|
// 设置静态根目录为上传根目录
|
|
|
|
|
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) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 配置全局拦截器
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void configInterceptor(Interceptors me) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 配置处理器
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void configHandler(Handlers me) {
|
|
|
|
|
//路由处理器
|
|
|
|
|
me.add(new RouterHandler());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 在jfinal启动完成后马上执行
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void onStart() {
|
|
|
|
|
//打印 启动Logo
|
|
|
|
|
String path = Objects.requireNonNull(GwApplication.class.getClassLoader().getResource("logo.txt")).getPath();
|
|
|
|
|
File file = new File(path);
|
|
|
|
|
System.out.println(FileUtil.readUtf8String(file));
|
|
|
|
|
}
|
|
|
|
|
}
|