|
|
package com.dsideal.base;
|
|
|
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
import com.cybermkd.mongo.plugin.MongoJFinalPlugin;
|
|
|
import com.dsideal.base.Admin.Controller.LoginController;
|
|
|
import com.dsideal.base.Index.Controller.IndexController;
|
|
|
import com.dsideal.base.Interceptor.*;
|
|
|
import com.dsideal.base.Util.LogBackLogFactory;
|
|
|
import com.jfinal.config.*;
|
|
|
import com.jfinal.kit.PathKit;
|
|
|
import com.jfinal.kit.PropKit;
|
|
|
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.server.undertow.UndertowServer;
|
|
|
import com.jfinal.template.Engine;
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
|
public class Start extends JFinalConfig {
|
|
|
|
|
|
//配置文件
|
|
|
public static void main(String[] args) {
|
|
|
//配置文件
|
|
|
String configFile = "application_dev.properties";
|
|
|
String myEnvVar = System.getenv("WORKING_ENV");
|
|
|
if (myEnvVar != null) {
|
|
|
configFile = configFile.replace("_dev", "_pro");
|
|
|
System.out.println("环境变量 WORKING_ENV 的值是: " + myEnvVar);
|
|
|
} else {
|
|
|
System.out.println("环境变量 WORKING_ENV 未设置。");
|
|
|
}
|
|
|
|
|
|
PropKit.use(configFile);
|
|
|
UndertowServer.create(Start.class, "undertow.properties").start();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 配置常量
|
|
|
*/
|
|
|
@Override
|
|
|
public void configConstant(Constants me) {
|
|
|
//使用LogBack
|
|
|
me.setLogFactory(new LogBackLogFactory());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 配置路由
|
|
|
*/
|
|
|
@Override
|
|
|
public void configRoute(Routes me) {
|
|
|
//默认页面
|
|
|
me.add("/", IndexController.class);
|
|
|
//登录路由
|
|
|
me.add("/login", LoginController.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 hpPlugin = new HikariCpPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"),
|
|
|
PropKit.get("password").trim(), PropKit.get("driverClassName"));
|
|
|
hpPlugin.setConnectionTestQuery(connectionTestQuery);
|
|
|
hpPlugin.setConnectionTimeout(connectionTimeoutMs);
|
|
|
hpPlugin.setIdleTimeout(idleTimeoutMs);
|
|
|
hpPlugin.setMaxLifetime(maxLifetimeMs);
|
|
|
hpPlugin.setMaximumPoolSize(maxPoolSize);
|
|
|
|
|
|
String jdbcUrlSlave = PropKit.get("jdbcUrlSlave");
|
|
|
if (StrKit.isBlank(jdbcUrlSlave)) jdbcUrlSlave = PropKit.get("jdbcUrl");
|
|
|
HikariCpPlugin slavePlugin = new HikariCpPlugin(jdbcUrlSlave, PropKit.get("user"),
|
|
|
PropKit.get("password").trim(), PropKit.get("driverClassName"));
|
|
|
slavePlugin.setConnectionTestQuery(connectionTestQuery);
|
|
|
slavePlugin.setConnectionTimeout(connectionTimeoutMs);
|
|
|
slavePlugin.setIdleTimeout(idleTimeoutMs);
|
|
|
slavePlugin.setMaxLifetime(maxLifetimeMs);
|
|
|
slavePlugin.setMaximumPoolSize(maxPoolSize);
|
|
|
me.add(hpPlugin);
|
|
|
me.add(slavePlugin);
|
|
|
|
|
|
// 配置ActiveRecord插件
|
|
|
ActiveRecordPlugin arp = new ActiveRecordPlugin(hpPlugin);
|
|
|
|
|
|
//遍历sql目录下所有的sql文件
|
|
|
File sqlDir;
|
|
|
String basePath = PathKit.getRootClassPath();
|
|
|
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) {
|
|
|
arp.addSqlTemplate("/Sql/" + sqlFile.getName());
|
|
|
}
|
|
|
}
|
|
|
arp.setShowSql(true);
|
|
|
arp.setDialect(new MysqlDialect());
|
|
|
//加载
|
|
|
me.add(arp);
|
|
|
|
|
|
//集成MongoDb
|
|
|
String mongodbUri = PropKit.get("mongodbUri");
|
|
|
MongoJFinalPlugin jFinalPlugin = new MongoJFinalPlugin();
|
|
|
|
|
|
// 验证连接字符串是否以预期的前缀开始
|
|
|
if (!mongodbUri.startsWith("mongodb://")) {
|
|
|
System.out.println("Invalid connection string format.");
|
|
|
return;
|
|
|
}
|
|
|
// 解析用户名和密码
|
|
|
int atIndex = mongodbUri.indexOf("@");
|
|
|
if (atIndex == -1) {
|
|
|
System.out.println("Username and password section is missing.");
|
|
|
return;
|
|
|
}
|
|
|
String userInfo = mongodbUri.substring("mongodb://".length(), atIndex);
|
|
|
String[] userInfoParts = userInfo.split(":");
|
|
|
String username = userInfoParts[0];
|
|
|
String password = userInfoParts.length > 1 ? userInfoParts[1] : "";
|
|
|
|
|
|
// 提取服务器地址列表
|
|
|
int slashIndex = mongodbUri.indexOf("/", atIndex + 1);
|
|
|
if (slashIndex == -1) {
|
|
|
System.out.println("No database name found.");
|
|
|
return;
|
|
|
}
|
|
|
String serverAddresses = mongodbUri.substring(atIndex + 1, slashIndex);
|
|
|
String[] servers = serverAddresses.split(",");
|
|
|
|
|
|
// 提取数据库名称,忽略查询参数
|
|
|
int questionMarkIndex = mongodbUri.indexOf("?", slashIndex);
|
|
|
String dbName = questionMarkIndex == -1
|
|
|
? mongodbUri.substring(slashIndex + 1)
|
|
|
: mongodbUri.substring(slashIndex + 1, questionMarkIndex);
|
|
|
|
|
|
|
|
|
jFinalPlugin.setDatabase(dbName);
|
|
|
jFinalPlugin.auth(username, password);
|
|
|
|
|
|
for (String server : servers) {
|
|
|
// 分割服务器地址以获取IP和端口
|
|
|
int portIndex = server.lastIndexOf(":");
|
|
|
String ipWithAt = server.substring(0, portIndex);
|
|
|
String ip = ipWithAt.substring(ipWithAt.indexOf("@") + 1);
|
|
|
String port = server.substring(portIndex + 1);
|
|
|
jFinalPlugin.add(ip, Integer.parseInt(port));
|
|
|
}
|
|
|
me.add(jFinalPlugin);
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 配置全局拦截器
|
|
|
*/
|
|
|
@Override
|
|
|
public void configInterceptor(Interceptors me) {
|
|
|
|
|
|
//注册参数数字检查器
|
|
|
me.add(new IsNumbericInterceptor());
|
|
|
|
|
|
//注册Ids检查是不是数字的检查器
|
|
|
me.add(new CheckIdsInterceptor());
|
|
|
|
|
|
//注册一个GUID号的检查器
|
|
|
me.add(new IsGuidInterceptor());
|
|
|
|
|
|
//注册一个检查输入文本长度的拦截器
|
|
|
me.add(new LengthInterceptor());
|
|
|
|
|
|
//JWT检查器
|
|
|
me.add(new JwtCheckInterceptor());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 配置处理器
|
|
|
*/
|
|
|
@Override
|
|
|
public void configHandler(Handlers me) {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 在jfinal启动完成后马上执行
|
|
|
*/
|
|
|
@Override
|
|
|
public void onStart() {
|
|
|
//打印 启动Logo
|
|
|
String path = Start.class.getClassLoader().getResource("logo.txt").getPath();
|
|
|
File file = new File(path);
|
|
|
System.out.println(FileUtil.readUtf8String(file));
|
|
|
}
|
|
|
}
|