From 0368004736f84d57bc2dfc8fe4e83a5f5bff2320 Mon Sep 17 00:00:00 2001
From: aoshiguchen <1052045476@qq.com>
Date: Sun, 31 Jul 2022 18:21:30 +0800
Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96db=E6=95=B0=E6=8D=AE?=
=?UTF-8?q?=E9=80=BB=E8=BE=91=E5=AE=8C=E5=96=84.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
neutrino-proxy-server/pom.xml | 5 ++
.../proxy/server/base/rest/DBInitialize.java | 76 ++++++++++++++-----
.../proxy/server/base/rest/SqliteConfig.java | 48 ++++++++++++
.../src/main/resources/application.yml | 4 +
.../src/main/resources/init-structure.sql | 19 -----
.../src/main/resources/sql/init-structure.sql | 63 +++++++++++++++
.../src/main/resources/sql/user.data.sql | 0
7 files changed, 179 insertions(+), 36 deletions(-)
create mode 100644 neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/SqliteConfig.java
delete mode 100644 neutrino-proxy-server/src/main/resources/init-structure.sql
create mode 100644 neutrino-proxy-server/src/main/resources/sql/init-structure.sql
create mode 100644 neutrino-proxy-server/src/main/resources/sql/user.data.sql
diff --git a/neutrino-proxy-server/pom.xml b/neutrino-proxy-server/pom.xml
index b168a2a8..94b30f63 100644
--- a/neutrino-proxy-server/pom.xml
+++ b/neutrino-proxy-server/pom.xml
@@ -25,6 +25,11 @@
sqlite-jdbc
3.7.2
+
+ com.alibaba
+ druid
+ 1.1.24
+
diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/DBInitialize.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/DBInitialize.java
index a2a37725..0851a9f5 100644
--- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/DBInitialize.java
+++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/DBInitialize.java
@@ -21,15 +21,14 @@
*/
package fun.asgc.neutrino.proxy.server.base.rest;
+import com.alibaba.druid.pool.DruidDataSource;
+import com.google.common.collect.Lists;
import fun.asgc.neutrino.core.annotation.PreLoad;
-import fun.asgc.neutrino.core.util.CollectionUtil;
-import fun.asgc.neutrino.core.util.FileUtil;
-import fun.asgc.neutrino.core.util.LockUtil;
-import fun.asgc.neutrino.core.util.StringUtil;
+import fun.asgc.neutrino.core.db.template.JdbcTemplate;
+import fun.asgc.neutrino.core.util.*;
+import lombok.extern.slf4j.Slf4j;
-import java.sql.Connection;
import java.sql.DriverManager;
-import java.sql.Statement;
import java.util.List;
/**
@@ -37,21 +36,25 @@ import java.util.List;
* @author: 初始化数据库
* @date: 2022/7/31
*/
+@Slf4j
@PreLoad("init")
public class DBInitialize {
-
- private static Connection conn;
+ private static List initDataTableNameList = Lists.newArrayList("user");
+ private static SqliteConfig sqliteConfig;
+ private static JdbcTemplate jdbcTemplate;
public static void init() throws Exception {
+ sqliteConfig = ConfigUtil.getYmlConfig(SqliteConfig.class);
+ jdbcTemplate = getJdbcTemplate();
initDBStructure();
+ initDBData();
}
/**
* 初始化数据库结构
*/
private static void initDBStructure() throws Exception {
- Statement stat = getOrNewConnection().createStatement();
- List lines = FileUtil.readContentAsStringList("classpath:/init-structure.sql");
+ List lines = FileUtil.readContentAsStringList("classpath:/sql/init-structure.sql");
if (CollectionUtil.isEmpty(lines)) {
return;
}
@@ -62,27 +65,66 @@ public class DBInitialize {
}
sql += "\r\n" + line.trim();
if (sql.endsWith(";")) {
- stat.executeUpdate(sql);
+ log.debug("初始化数据库表 sql:{}", sql);
+ jdbcTemplate.update(sql);
sql = "";
}
}
}
/**
- * 获取一个数据库连接,如果数据库不存在,则会创建一个空数据库
+ * 初始化数据
+ * @throws Exception
+ */
+ private static void initDBData() throws Exception {
+ if (CollectionUtil.isEmpty(initDataTableNameList)) {
+ return;
+ }
+ for (String tableName : initDataTableNameList) {
+ // 表里没有数据的时候,才进行初始化操作
+ int count = jdbcTemplate.queryForInt(String.format("select count(1) from `%s`", tableName));
+ if (count > 0) {
+ continue;
+ }
+ List lines = FileUtil.readContentAsStringList(String.format("classpath:/sql/%s.data.sql", tableName));
+ if (CollectionUtil.isEmpty(lines)) {
+ return;
+ }
+ String sql = "";
+ for (String line : lines) {
+ if (StringUtil.isEmpty(line) || StringUtil.isEmpty(line.trim()) || line.trim().startsWith("#")) {
+ continue;
+ }
+ sql += "\r\n" + line.trim();
+ if (sql.endsWith(";")) {
+ jdbcTemplate.update(sql);
+ sql = "";
+ }
+ }
+ }
+ }
+
+ /**
+ * 获取jdbcTemplate实例
* @return
* @throws Exception
*/
- public static Connection getOrNewConnection() throws Exception {
+ private static JdbcTemplate getJdbcTemplate() throws Exception {
return LockUtil.doubleCheckProcess(
- () -> null == conn,
+ () -> null == jdbcTemplate,
DBInitialize.class,
() -> {
- Class.forName("org.sqlite.JDBC");
+ Class.forName(sqliteConfig.getDriverClass());
//建立一个数据库名data.db的连接,如果不存在就在当前目录下创建之
- DBInitialize.conn = DriverManager.getConnection("jdbc:sqlite:data.db");
+ DriverManager.getConnection(sqliteConfig.getUrl());
+ // 创建数据源
+ DruidDataSource dataSource = new DruidDataSource();
+ dataSource.setUrl(sqliteConfig.getUrl());
+ dataSource.setDriverClassName(sqliteConfig.getDriverClass());
+ // 创建jdbcTemplate
+ jdbcTemplate = new JdbcTemplate(dataSource);
},
- () -> conn
+ () -> jdbcTemplate
);
}
}
diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/SqliteConfig.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/SqliteConfig.java
new file mode 100644
index 00000000..be34cdf6
--- /dev/null
+++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/SqliteConfig.java
@@ -0,0 +1,48 @@
+/**
+ * Copyright (c) 2022 aoshiguchen
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+package fun.asgc.neutrino.proxy.server.base.rest;
+
+import fun.asgc.neutrino.core.annotation.Configuration;
+import fun.asgc.neutrino.core.annotation.NonIntercept;
+import fun.asgc.neutrino.core.annotation.Value;
+import lombok.Data;
+
+/**
+ *
+ * @author: aoshiguchen
+ * @date: 2022/7/31
+ */
+@Data
+@NonIntercept
+@Configuration(prefix = "neutrino.data.sqlite")
+public class SqliteConfig {
+ /**
+ * 连接url
+ */
+ @Value("url")
+ private String url;
+ /**
+ * 驱动类
+ */
+ @Value("driver-class")
+ private String driverClass;
+}
diff --git a/neutrino-proxy-server/src/main/resources/application.yml b/neutrino-proxy-server/src/main/resources/application.yml
index 3093ca34..cc930e81 100644
--- a/neutrino-proxy-server/src/main/resources/application.yml
+++ b/neutrino-proxy-server/src/main/resources/application.yml
@@ -29,3 +29,7 @@ neutrino:
license:
79419a1a8691413aa5e845b9e3e90051: 3
9352b1c25f564c81a5677131d7769876: 2
+ data:
+ sqlite:
+ url: jdbc:sqlite:data.db
+ driver-class: org.sqlite.JDBC
diff --git a/neutrino-proxy-server/src/main/resources/init-structure.sql b/neutrino-proxy-server/src/main/resources/init-structure.sql
deleted file mode 100644
index ae55d842..00000000
--- a/neutrino-proxy-server/src/main/resources/init-structure.sql
+++ /dev/null
@@ -1,19 +0,0 @@
-#用户表
-CREATE TABLE IF NOT EXISTS `user` (
- `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
- `name` varchar(50) NOT NULL,
- `login_name` varchar(50) NOT NULL,
- `login_password` varchar(255) NOT NULL,
- `create_time` INTEGER(20) NOT NULL,
- `update_time` INTEGER(20) NOT NULL
-);
-
-#license表
-CREATE TABLE IF NOT EXISTS `license` (
- `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
- `name` varchar(50) NOT NULL,
- `key` varchar(100) NOT NULL,
- `user_id` INTEGER NOT NULL,
- `create_time` INTEGER(20) NOT NULL,
- `update_time` INTEGER(20) NOT NULL
-);
diff --git a/neutrino-proxy-server/src/main/resources/sql/init-structure.sql b/neutrino-proxy-server/src/main/resources/sql/init-structure.sql
new file mode 100644
index 00000000..f6266061
--- /dev/null
+++ b/neutrino-proxy-server/src/main/resources/sql/init-structure.sql
@@ -0,0 +1,63 @@
+#用户表
+CREATE TABLE IF NOT EXISTS `user` (
+ `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
+ `name` varchar(50) NOT NULL,
+ `login_name` varchar(50) NOT NULL,
+ `login_password` varchar(255) NOT NULL,
+ `create_time` INTEGER(20) NOT NULL,
+ `update_time` INTEGER(20) NOT NULL
+);
+
+#license表
+CREATE TABLE IF NOT EXISTS `license` (
+ `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
+ `name` varchar(50) NOT NULL,
+ `key` varchar(100) NOT NULL,
+ `user_id` INTEGER NOT NULL,
+ `is_online` INTEGER(2) NOT NULL,
+ `create_time` INTEGER(20) NOT NULL,
+ `update_time` INTEGER(20) NOT NULL
+);
+
+#用户token表
+CREATE TABLE IF NOT EXISTS `user_token` (
+ `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
+ `token` varchar(50) NOT NULL,
+ `user_id` INTEGER NOT NULL,
+ `expiration_time` INTEGER NOT NULL,
+ `create_time` INTEGER NOT NULL,
+ `update_time` INTEGER NOT NULL
+);
+
+#用户登录记录表
+CREATE TABLE IF NOT EXISTS `user_login_record` (
+ `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
+ `user_id` INTEGER NOT NULL,
+ `ip` varchar(50) NOT NULL,
+ `token` varchar(100) NOT NULL,
+ `type` INTEGER(2) NOT NULL,
+ `create_time` INTEGER(20) NOT NULL
+);
+
+#可用端口配置表
+CREATE TABLE IF NOT EXISTS `user_login_record` (
+ `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
+ `user_id` INTEGER NOT NULL,
+ `ip` varchar(50) NOT NULL,
+ `token` varchar(50) NOT NULL,
+ `type` INTEGER(2) NOT NULL,
+ `create_time` INTEGER(20) NOT NULL
+);
+
+#端口映射表
+CREATE TABLE IF NOT EXISTS `port_mapping` (
+ `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
+ `license_id` INTEGER(20) NOT NULL,
+ `service_port` INTEGER NOT NULL,
+ `client_ip` varchar(20) NOT NULL,
+ `client_port` INTEGER NOT NULL,
+ `is_online` INTEGER(2) NOT NULL,
+ `enable` INTEGER NOT NULL,
+ `create_time` INTEGER(20) NOT NULL,
+ `update_time` INTEGER(20) NOT NULL
+);
diff --git a/neutrino-proxy-server/src/main/resources/sql/user.data.sql b/neutrino-proxy-server/src/main/resources/sql/user.data.sql
new file mode 100644
index 00000000..e69de29b