Skip to content

Commit

Permalink
feat: 优化系统启动配置
Browse files Browse the repository at this point in the history
  • Loading branch information
javahuang committed Feb 20, 2022
1 parent b637fed commit 100f7c3
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public class SurveyServerApplication {

public static void main(String[] args) {
// 快速执行数据库初始化操作
if (args.length > 0 && "i".equalsIgnoreCase(args[0])) {
DatabaseInitHelper.init();
return;
if (args.length > 0) {
DatabaseInitHelper.init(args);
}

SpringApplication.run(SurveyServerApplication.class, args);
}

Expand Down
4 changes: 2 additions & 2 deletions api/src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>./survey/logs/info/info.%d{yyyy-MM-dd}-%i.log.zip</fileNamePattern>
<fileNamePattern>./logs/info/info.%d{yyyy-MM-dd}-%i.log.zip</fileNamePattern>
<maxFileSize>60MB</maxFileSize>
<totalSizeCap>5GB</totalSizeCap>
<maxHistory>30</maxHistory>
Expand All @@ -37,7 +37,7 @@

<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>./survey/logs/error/error.%d{yyyy-MM-dd}-%i.log.zip
<fileNamePattern>./logs/error/error.%d{yyyy-MM-dd}-%i.log.zip
</fileNamePattern>
<maxHistory>90</maxHistory>
<totalSizeCap>10GB</totalSizeCap>
Expand Down
4 changes: 2 additions & 2 deletions rdbms/src/main/resources/config/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ spring:
# url: jdbc:h2:tcp://localhost/~/survey
# driver-class-name: org.h2.Driver
username: root
password: dota8888
url: jdbc:mysql://localhost:3306/workflow
password: 123456
url: jdbc:mysql://localhost:3306/surveyking
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
configuration:
Expand Down
5 changes: 1 addition & 4 deletions rdbms/src/main/resources/config/application-pro.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ spring:
console:
enabled: true
datasource:
username: sa
password: sa
url: jdbc:h2:./survey;INIT=runscript from 'classpath:scripts/init-h2.sql'
driver-class-name: org.h2.Driver
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,43 @@ public class DatabaseInitHelper {

private static String db = "mysql";

public static void init() {
public static void init(String[] args) {
try {
if (!needCreateProperties()) {
return;
}
colorOutput("请选择您的数据库类型 : ");
colorOutput("1) MySql");
colorOutput("2) H2");
String selection = sc.nextLine();
switch (selection) {
case "1":
db = "mysql";
break;
case "2":
db = "h2";
break;
default:
return;
// 初始化启动配置文件,初始化数据库脚本
if ("i".equals(args[0])) {
initializeDatabase();
}
else {
// 通过启动命令行参数来创建配置文件
createFileFromProperties(setPropertiesFromArguments(args));
}
initDb();
}
finally {
sc.close();
}
}

private static void initializeDatabase() {
colorOutput("请选择您的数据库类型 : ");
colorOutput("1) MySql");
colorOutput("2) H2");
String selection = sc.nextLine();
switch (selection) {
case "1":
db = "mysql";
break;
case "2":
db = "h2";
break;
default:
return;
}
initDb();
}

private static void initDb() {
if ("mysql".equals(db)) {
initMySql();
Expand Down Expand Up @@ -112,6 +123,27 @@ private static DatabaseInitProperties setProperties() {
return properties;
}

private static DatabaseInitProperties setPropertiesFromArguments(String[] args) {
DatabaseInitProperties properties = new DatabaseInitProperties();
for (String arg : args) {
String[] kv = arg.split("=", 2);
if (kv[0].toLowerCase().contains("port")) {
properties.setServerPort(Integer.parseInt(kv[1]));
}
else if (kv[0].toLowerCase().contains("url")) {
properties.setDataSourceUrl(kv[1]);
}
else if (kv[0].toLowerCase().contains("username")) {
properties.setUsername(kv[1]);
}
else if (kv[0].toLowerCase().contains("password")) {
properties.setPassword(kv[1]);
}
}

return properties;
}

private static String setProperty(String prompt, String errorMsg, String regex, String defaultValue) {
String result;
colorOutput(prompt);
Expand Down Expand Up @@ -203,23 +235,26 @@ private static void finish() {
@SneakyThrows
private static void createFileFromProperties(DatabaseInitProperties properties) {
File f = new File(CONFIG_FILE);
Files.write(f.toPath(), String.format("spring.application.name=%s\n", properties.getApplicationName())
.getBytes(StandardCharsets.UTF_8));
Files.write(f.toPath(),
String.format("server.port=%d\n", properties.getServerPort()).getBytes(StandardCharsets.UTF_8),
StandardOpenOption.APPEND);
Files.write(f.toPath(),
String.format("spring.datasource.driver-class-name=%s\n", properties.getDriverClassName())
.getBytes(StandardCharsets.UTF_8),
StandardOpenOption.APPEND);
Files.write(f.toPath(),
String.format("spring.datasource.url=jdbc:mysql://%s:%d/%s\n", properties.getDbIp(),
properties.getDbPort(), properties.getDbName()).getBytes(StandardCharsets.UTF_8),
StandardOpenOption.APPEND);
Files.write(f.toPath(), String.format("spring.datasource.username=%s\n", properties.getUsername())
.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
Files.write(f.toPath(), String.format("spring.datasource.password=%s\n", properties.getPassword())
.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
writeLine(f, "spring.application.name=%s\n", properties.getApplicationName());
writeLine(f, "server.port=%d\n", properties.getServerPort());
writeLine(f, "spring.datasource.driver-class-name=%s\n", properties.getDriverClassName());
if (properties.getDataSourceUrl() == null) {
writeLine(f, "spring.datasource.url=jdbc:mysql://%s:%d/%s\n", properties.getDbIp(), properties.getDbPort(),
properties.getDbName());
}
else {
writeLine(f, "spring.datasource.url=%s\n", properties.getDataSourceUrl());
}
writeLine(f, "spring.datasource.username=%s\n", properties.getUsername());
writeLine(f, "spring.datasource.password=%s\n", properties.getPassword());
}

@SneakyThrows
private static void writeLine(File file, String key, Object... value) {
if (value.length > 0 && value[0] != null) {
Files.write(file.toPath(), String.format(key, value).getBytes(StandardCharsets.UTF_8),
StandardOpenOption.APPEND);
}
}

private static boolean yesOrNo(String prompt) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,7 @@ public class DatabaseInitProperties {

private String applicationName = "SurveyKing";

/** 数据库 url */
private String dataSourceUrl;

}

0 comments on commit 100f7c3

Please sign in to comment.