添加多运行环境支持 #14
@@ -1,9 +1,11 @@
|
|||||||
package cn.celess.blog;
|
package cn.celess.blog;
|
||||||
|
|
||||||
import org.mybatis.spring.annotation.MapperScan;
|
import org.mybatis.spring.annotation.MapperScan;
|
||||||
|
import org.springframework.boot.ApplicationArguments;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
import org.springframework.scheduling.annotation.EnableAsync;
|
import org.springframework.scheduling.annotation.EnableAsync;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -13,9 +15,20 @@ import org.springframework.scheduling.annotation.EnableAsync;
|
|||||||
@EnableAsync
|
@EnableAsync
|
||||||
@MapperScan("cn.celess.blog.mapper")
|
@MapperScan("cn.celess.blog.mapper")
|
||||||
public class BlogApplication {
|
public class BlogApplication {
|
||||||
public static ApplicationContext context;
|
public static ConfigurableApplicationContext context;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
context = SpringApplication.run(BlogApplication.class, args);
|
context = SpringApplication.run(BlogApplication.class, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void restart() {
|
||||||
|
ApplicationArguments args = context.getBean(ApplicationArguments.class);
|
||||||
|
|
||||||
|
Thread thread = new Thread(() -> {
|
||||||
|
context.close();
|
||||||
|
context = SpringApplication.run(BlogApplication.class, args.getSourceArgs());
|
||||||
|
});
|
||||||
|
thread.setDaemon(false);
|
||||||
|
thread.start();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
103
src/main/java/cn/celess/blog/controller/InstallController.java
Normal file
103
src/main/java/cn/celess/blog/controller/InstallController.java
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
package cn.celess.blog.controller;
|
||||||
|
|
||||||
|
import cn.celess.blog.BlogApplication;
|
||||||
|
import cn.celess.blog.enmu.ConfigKeyEnum;
|
||||||
|
import cn.celess.blog.enmu.ResponseEnum;
|
||||||
|
import cn.celess.blog.entity.Config;
|
||||||
|
import cn.celess.blog.entity.InstallParam;
|
||||||
|
import cn.celess.blog.entity.Response;
|
||||||
|
import cn.celess.blog.exception.MyException;
|
||||||
|
import cn.celess.blog.mapper.ConfigMapper;
|
||||||
|
import cn.celess.blog.service.fileserviceimpl.LocalFileServiceImpl;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : xiaohai
|
||||||
|
* @date : 2020/10/18 15:36
|
||||||
|
* @desc :
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Controller
|
||||||
|
public class InstallController {
|
||||||
|
@Autowired
|
||||||
|
ConfigMapper configMapper;
|
||||||
|
|
||||||
|
public static final String MYSQL_DRIVER_CLASS_NAME = "com.mysql.cj.jdbc.Driver";
|
||||||
|
public static final String H2_DRIVER_CLASS_NAME = "org.h2.Driver";
|
||||||
|
|
||||||
|
@GetMapping("/is_install")
|
||||||
|
@ResponseBody
|
||||||
|
public Response<Map<String, Object>> isInstall() {
|
||||||
|
Config installed = configMapper.getConfiguration(ConfigKeyEnum.BLOG_INSTALLED.getKey());
|
||||||
|
Boolean isInstall = Boolean.valueOf(installed.getValue());
|
||||||
|
Map<String, Object> result = new HashMap<>(3);
|
||||||
|
result.put("is_install", isInstall);
|
||||||
|
if (isInstall) {
|
||||||
|
Map<String, String> configMap = configMapper.getConfigurations()
|
||||||
|
.stream()
|
||||||
|
.filter(config -> config.getValue() != null)
|
||||||
|
.collect(Collectors.toMap(Config::getName, Config::getValue));
|
||||||
|
result.put(ConfigKeyEnum.FILE_TYPE.getKey(), configMap.get(ConfigKeyEnum.FILE_TYPE.getKey()));
|
||||||
|
result.put(ConfigKeyEnum.DB_TYPE.getKey(), configMap.get(ConfigKeyEnum.DB_TYPE.getKey()));
|
||||||
|
}
|
||||||
|
return Response.success(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@PostMapping("/install")
|
||||||
|
@ResponseBody
|
||||||
|
public Response install(@RequestBody InstallParam installParam) throws IOException {
|
||||||
|
Config install = configMapper.getConfiguration(ConfigKeyEnum.BLOG_INSTALLED.getKey());
|
||||||
|
boolean isInstall = Boolean.parseBoolean(install.getValue());
|
||||||
|
if (isInstall) {
|
||||||
|
throw new MyException(ResponseEnum.FAILURE, "已经安装过了");
|
||||||
|
}
|
||||||
|
|
||||||
|
Config config = new Config(ConfigKeyEnum.DB_TYPE);
|
||||||
|
config.setValue(installParam.getDbType());
|
||||||
|
Properties properties = new Properties();
|
||||||
|
if ("h2".equals(installParam.getDbType())) {
|
||||||
|
properties.setProperty(ConfigKeyEnum.DB_DRIVER_CLASS_NAME.getKey(), H2_DRIVER_CLASS_NAME);
|
||||||
|
} else {
|
||||||
|
properties.setProperty(ConfigKeyEnum.DB_DRIVER_CLASS_NAME.getKey(), MYSQL_DRIVER_CLASS_NAME);
|
||||||
|
}
|
||||||
|
properties.setProperty(ConfigKeyEnum.DB_URL.getKey(), installParam.getDbUrl());
|
||||||
|
properties.setProperty(ConfigKeyEnum.DB_USERNAME.getKey(), installParam.getDbUsername());
|
||||||
|
properties.setProperty(ConfigKeyEnum.DB_PASSWORD.getKey(), installParam.getDbPassword());
|
||||||
|
|
||||||
|
Config configuration = configMapper.getConfiguration(ConfigKeyEnum.BLOG_DB_PATH.getKey());
|
||||||
|
|
||||||
|
File file = new File(LocalFileServiceImpl.getPath(configuration.getValue()));
|
||||||
|
if (!file.exists() && file.createNewFile()) {
|
||||||
|
log.info("创建数据库配置文件: {}", file.getAbsolutePath());
|
||||||
|
}
|
||||||
|
FileInputStream fis = new FileInputStream(file);
|
||||||
|
FileOutputStream fos = new FileOutputStream(file);
|
||||||
|
properties.load(fis);
|
||||||
|
|
||||||
|
configMapper.addConfiguration(config);
|
||||||
|
properties.store(fos, "DB CONFIG");
|
||||||
|
|
||||||
|
install.setValue(Boolean.valueOf(true).toString());
|
||||||
|
configMapper.updateConfiguration(install);
|
||||||
|
|
||||||
|
log.info("重启...");
|
||||||
|
|
||||||
|
// 重启
|
||||||
|
BlogApplication.restart();
|
||||||
|
|
||||||
|
return Response.success(installParam);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,7 +16,13 @@ public enum ConfigKeyEnum {
|
|||||||
FILE_QINIU_BUCKET("file.qiniu.bucket"),
|
FILE_QINIU_BUCKET("file.qiniu.bucket"),
|
||||||
FILE_LOCAL_DIRECTORY_PATH("file.local.directoryPath"),
|
FILE_LOCAL_DIRECTORY_PATH("file.local.directoryPath"),
|
||||||
BLOG_FILE_PATH("blog.file.path"),
|
BLOG_FILE_PATH("blog.file.path"),
|
||||||
DB_TYPE("db.type");
|
BLOG_INSTALLED("blog.installed"),
|
||||||
|
DB_TYPE("db.type"),
|
||||||
|
DB_URL("spring.datasource.url"),
|
||||||
|
DB_USERNAME("spring.datasource.username"),
|
||||||
|
DB_PASSWORD("spring.datasource.password"),
|
||||||
|
DB_DRIVER_CLASS_NAME("spring.datasource.driver-class-name"),
|
||||||
|
BLOG_DB_PATH("blog.db.path");
|
||||||
|
|
||||||
|
|
||||||
private final String key;
|
private final String key;
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package cn.celess.blog.entity;
|
package cn.celess.blog.entity;
|
||||||
|
|
||||||
|
import cn.celess.blog.enmu.ConfigKeyEnum;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author : xiaohai
|
* @author : xiaohai
|
||||||
@@ -8,8 +10,17 @@ import lombok.Data;
|
|||||||
* @desc :
|
* @desc :
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
public class Config {
|
public class Config {
|
||||||
private Integer id;
|
private Integer id;
|
||||||
private String name;
|
private String name;
|
||||||
private String value;
|
private String value;
|
||||||
|
|
||||||
|
public Config(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Config(ConfigKeyEnum e) {
|
||||||
|
this.name = e.getKey();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
16
src/main/java/cn/celess/blog/entity/InstallParam.java
Normal file
16
src/main/java/cn/celess/blog/entity/InstallParam.java
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package cn.celess.blog.entity;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : xiaohai
|
||||||
|
* @date : 2020/10/18 14:52
|
||||||
|
* @desc :
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class InstallParam {
|
||||||
|
private String dbType;
|
||||||
|
private String dbUrl;
|
||||||
|
private String dbUsername;
|
||||||
|
private String dbPassword;
|
||||||
|
}
|
||||||
@@ -221,4 +221,5 @@ VALUES (1, 'file.type', 'local'),
|
|||||||
(4, 'file.qiniu.bucket', null),
|
(4, 'file.qiniu.bucket', null),
|
||||||
(5, 'blog.file.path', '~/blog/'),
|
(5, 'blog.file.path', '~/blog/'),
|
||||||
(6, 'file.local.directoryPath', '~/blog/files/'),
|
(6, 'file.local.directoryPath', '~/blog/files/'),
|
||||||
(7, 'db.type', 'h2')
|
(8, 'blog.installed', 'false'),
|
||||||
|
(9, 'blog.db.path', '~/blog/db.properties')
|
||||||
Reference in New Issue
Block a user