|
|
package com.dsideal.resource;
|
|
|
|
|
|
import com.dsideal.resource.Base.Controller.BaseController;
|
|
|
import com.dsideal.resource.Index.IndexController;
|
|
|
import com.dsideal.resource.Interceptor.*;
|
|
|
import com.dsideal.resource.Menu.Controller.MenuController;
|
|
|
import com.dsideal.resource.Plugin.YamlProp;
|
|
|
import com.dsideal.resource.Util.FileUtil;
|
|
|
import com.dsideal.resource.Util.LogBackLogFactory;
|
|
|
import com.jfinal.config.*;
|
|
|
import com.jfinal.kit.Prop;
|
|
|
import com.jfinal.kit.StrKit;
|
|
|
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
|
|
|
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
|
|
|
import com.jfinal.plugin.hikaricp.HikariCpPlugin;
|
|
|
import com.jfinal.plugin.redis.RedisPlugin;
|
|
|
import com.jfinal.server.undertow.UndertowServer;
|
|
|
import com.jfinal.template.Engine;
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
|
public class ResApplication extends JFinalConfig {
|
|
|
|
|
|
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".replace("{?}", getEnvPrefix());
|
|
|
UndertowServer.create(ResApplication.class, configFile).start();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 配置常量
|
|
|
*/
|
|
|
public static Prop PropKit;
|
|
|
|
|
|
@Override
|
|
|
public void configConstant(Constants me) {
|
|
|
//使用LogBack
|
|
|
me.setLogFactory(new LogBackLogFactory());
|
|
|
//加载配置文件
|
|
|
String configFile = "application_{?}.yaml".replace("{?}", getEnvPrefix());
|
|
|
PropKit = new YamlProp(configFile);
|
|
|
|
|
|
// 设置静态根目录为上传根目录
|
|
|
me.setBaseUploadPath(PropKit.get("uploadTempPath"));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 配置路由
|
|
|
*/
|
|
|
@Override
|
|
|
public void configRoute(Routes me) {
|
|
|
//默认页面
|
|
|
me.add("/", IndexController.class);
|
|
|
//菜单
|
|
|
me.add("/menu", MenuController.class);
|
|
|
//资源基础管理
|
|
|
me.add("/base", BaseController.class);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void configEngine(Engine engine) {
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 配置插件
|
|
|
*/
|
|
|
private String connectionTestQuery = "select 1";
|
|
|
// 连接池中允许的最大连接数。缺省值:10;推荐的公式:((core_count * 2) + effective_spindle_count)
|
|
|
private int maxPoolSize = 10;
|
|
|
// 一个连接 idle 状态的最大时长(毫秒),超时则被释放(retired),缺省:10分钟
|
|
|
private long idleTimeoutMs = 600000;
|
|
|
private long maxLifetimeMs = 1800000;
|
|
|
// 等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生 SQLException, 缺省:30秒
|
|
|
private long connectionTimeoutMs = 30000;
|
|
|
|
|
|
@Override
|
|
|
public void configPlugin(Plugins me) {
|
|
|
HikariCpPlugin masterPlugin = new HikariCpPlugin(PropKit.get("mysql.jdbcUrl"), PropKit.get("mysql.user"),
|
|
|
PropKit.get("mysql.password").trim(), PropKit.get("mysql.driverClassName"));
|
|
|
masterPlugin.setConnectionTestQuery(connectionTestQuery);
|
|
|
masterPlugin.setConnectionTimeout(connectionTimeoutMs);
|
|
|
masterPlugin.setIdleTimeout(idleTimeoutMs);
|
|
|
masterPlugin.setMaxLifetime(maxLifetimeMs);
|
|
|
masterPlugin.setMaximumPoolSize(maxPoolSize);
|
|
|
|
|
|
String jdbcUrlSlave = PropKit.get("mysql.jdbcUrlSlave");
|
|
|
if (StrKit.isBlank(jdbcUrlSlave)) jdbcUrlSlave = PropKit.get("mysql.jdbcUrl");
|
|
|
HikariCpPlugin slavePlugin = new HikariCpPlugin(jdbcUrlSlave, PropKit.get("mysql.user"),
|
|
|
PropKit.get("mysql.password").trim(), PropKit.get("mysql.driverClassName"));
|
|
|
slavePlugin.setConnectionTestQuery(connectionTestQuery);
|
|
|
slavePlugin.setConnectionTimeout(connectionTimeoutMs);
|
|
|
slavePlugin.setIdleTimeout(idleTimeoutMs);
|
|
|
slavePlugin.setMaxLifetime(maxLifetimeMs);
|
|
|
slavePlugin.setMaximumPoolSize(maxPoolSize);
|
|
|
me.add(masterPlugin);
|
|
|
me.add(slavePlugin);
|
|
|
|
|
|
// 配置ActiveRecord插件
|
|
|
ActiveRecordPlugin masterArp = new ActiveRecordPlugin("master", masterPlugin);
|
|
|
masterArp.setDialect(new MysqlDialect());
|
|
|
ActiveRecordPlugin slaveArp = new ActiveRecordPlugin("slave", slavePlugin);
|
|
|
slaveArp.setDialect(new MysqlDialect());
|
|
|
|
|
|
//遍历sql目录下所有的sql文件
|
|
|
File sqlDir;
|
|
|
String basePath = ResApplication.class.getResource("/").getPath();
|
|
|
sqlDir = new File(basePath + "/Sql");
|
|
|
File[] sqlFiles = sqlDir.listFiles();
|
|
|
for (File sqlFile : sqlFiles != null ? sqlFiles : new File[0]) {
|
|
|
//只加载.sql文件
|
|
|
if (sqlFile.getName().indexOf(".sql") > 0) {
|
|
|
masterArp.addSqlTemplate("/Sql/" + sqlFile.getName());
|
|
|
slaveArp.addSqlTemplate("/Sql/" + sqlFile.getName());
|
|
|
}
|
|
|
}
|
|
|
//加载
|
|
|
me.add(masterArp);
|
|
|
me.add(slaveArp);
|
|
|
// 用于缓存模块的redis服务
|
|
|
RedisPlugin redis = new RedisPlugin("Redis", PropKit.get("redis.ip"), PropKit.getInt("redis.port"), 10 * 1000, PropKit.get("redis.password"));
|
|
|
//启动redis组件
|
|
|
me.add(redis);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 配置全局拦截器
|
|
|
*/
|
|
|
@Override
|
|
|
public void configInterceptor(Interceptors me) {
|
|
|
//注册参数数字检查器
|
|
|
me.add(new IsNumbericInterceptor());
|
|
|
|
|
|
//注册非空拦截器
|
|
|
me.add(new EmptyInterceptor());
|
|
|
|
|
|
//注册layui的分页检查器
|
|
|
me.add(new LayUiPageInfoInterceptor());
|
|
|
|
|
|
//注册Ids检查是不是数字的检查器
|
|
|
me.add(new CheckIdsInterceptor());
|
|
|
|
|
|
//注册一个GUID号的检查器
|
|
|
me.add(new IsGuidInterceptor());
|
|
|
|
|
|
//检查是不是系统管理员
|
|
|
me.add(new IsSysAdminInterceptor());
|
|
|
|
|
|
//注册一个检查输入文本长度的拦截器
|
|
|
me.add(new LengthInterceptor());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 配置处理器
|
|
|
*/
|
|
|
@Override
|
|
|
public void configHandler(Handlers me) {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 在jfinal启动完成后马上执行
|
|
|
*/
|
|
|
@Override
|
|
|
public void onStart() {
|
|
|
//打印 启动Logo
|
|
|
String path = ResApplication.class.getClassLoader().getResource("logo.txt").getPath();
|
|
|
File file = new File(path);
|
|
|
System.out.println(FileUtil.txt2String(file));
|
|
|
}
|
|
|
}
|