Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: A user-friendly config management page for apollo portal #4592

Merged
merged 40 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
dd68b53
提交
webSue Sep 29, 2022
d70c705
feature: A user-friendly config management page for apollo portal(upd…
webSue Sep 29, 2022
a2e9f7c
Merge branch 'master' into yst01
webSue Sep 29, 2022
f5a0110
A user-friendly config management page for apollo portal(update code …
webSue Sep 29, 2022
d0c738c
Merge remote-tracking branch 'origin/yst01' into yst01
webSue Sep 29, 2022
d938bd0
update 1 file
webSue Sep 29, 2022
cf7f679
Merge remote-tracking branch 'origin/yst01' into yst01
webSue Sep 29, 2022
e709b8c
feature: A user-friendly config management page for apollo portal
webSue Sep 29, 2022
3668bfc
update
webSue Sep 29, 2022
b2d4b0b
update
webSue Oct 2, 2022
c4546c4
update
webSue Oct 2, 2022
696544f
update json
webSue Oct 2, 2022
eb5e48d
update code style
webSue Oct 2, 2022
c0a5ca9
change json
webSue Oct 2, 2022
b1b3ece
Merge branch 'master' into yst01
webSue Oct 8, 2022
8d70ce8
add license headers
webSue Oct 8, 2022
3aafe6c
Merge remote-tracking branch 'origin/yst01' into yst01
webSue Oct 8, 2022
2e9bc80
update(only portal,delete configDB)
webSue Oct 12, 2022
54d83b2
Merge branch 'master' into yst01
webSue Oct 12, 2022
23e67e0
update
webSue Oct 12, 2022
a883aab
Merge remote-tracking branch 'origin/yst01' into yst01
webSue Oct 12, 2022
368d3ef
update
webSue Oct 12, 2022
429ac9e
update 1 file
webSue Oct 12, 2022
4cd469d
update
webSue Oct 12, 2022
c32f3ab
update test
webSue Oct 12, 2022
b91faa4
update test
webSue Oct 12, 2022
93525d1
update
webSue Oct 13, 2022
08ddf61
update
webSue Oct 13, 2022
3520429
update (pageable分页)
webSue Oct 13, 2022
ccd0b1c
update
webSue Oct 13, 2022
ab95a07
update test
webSue Oct 13, 2022
f4e9f83
update
webSue Oct 13, 2022
b67a6ef
update
webSue Oct 14, 2022
38ab3c9
update
webSue Oct 14, 2022
ee06261
update
webSue Oct 15, 2022
b8cca31
update(CHANGES.md)
webSue Oct 15, 2022
81b866f
update(使用文档)
webSue Oct 15, 2022
e904a29
Merge branch 'master' into yst01
webSue Oct 17, 2022
26aa925
update
webSue Oct 19, 2022
3029aad
Merge remote-tracking branch 'origin/yst01' into yst01
webSue Oct 19, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,21 @@
import com.ctrip.framework.apollo.portal.entity.po.ServerConfig;
import com.ctrip.framework.apollo.portal.repository.ServerConfigRepository;
import com.ctrip.framework.apollo.portal.spi.UserInfoHolder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
Expand All @@ -39,7 +47,8 @@ public class ServerConfigController {
private final ServerConfigRepository serverConfigRepository;
private final UserInfoHolder userInfoHolder;

public ServerConfigController(final ServerConfigRepository serverConfigRepository, final UserInfoHolder userInfoHolder) {
public ServerConfigController(final ServerConfigRepository serverConfigRepository,
final UserInfoHolder userInfoHolder) {
this.serverConfigRepository = serverConfigRepository;
this.userInfoHolder = userInfoHolder;
}
Expand All @@ -63,10 +72,123 @@ public ServerConfig createOrUpdate(@Valid @RequestBody ServerConfig serverConfig
return serverConfigRepository.save(storedConfig);
}

@PostMapping("/server/config/addConfigService")
public void addConfigService(@Valid @RequestBody ServerConfig serverConfig) throws SQLException {
String modifiedBy = userInfoHolder.getUser().getUserId();

Connection conn = getConn();
PreparedStatement pst = (PreparedStatement) conn.prepareStatement(
"select * from serverconfig where serverconfig.Key='" + serverConfig.getKey() + "'");
ResultSet rs = pst.executeQuery();

if (rs.next()) {
pst = (PreparedStatement) conn.prepareStatement(
"update serverconfig set Value='" + serverConfig.getValue() + "',Comment='"
+ serverConfig.getComment() + "' where serverconfig.Key='" + serverConfig.getKey()
+ "'");

pst.executeUpdate();
} else {
pst = (PreparedStatement) conn.prepareStatement(
"insert into serverconfig(Id,serverconfig.Key,Value,Comment,DataChange_CreatedBy,DataChange_LastModifiedBy) values (null,'"
+ serverConfig.getKey() +
"','" + serverConfig.getValue() + "','" + serverConfig.getComment() + "','"
+ modifiedBy + "','')");
pst.executeUpdate();
}
}

/**
* 获取所有PortalDB的配置信息
*
* @return
*/
@PreAuthorize(value = "@permissionValidator.isSuperAdmin()")
@GetMapping("/server/config/findAll")
public List<ServerConfig> findAllServerConfig(
@RequestParam(value = "offset", defaultValue = "0") int offset,
@RequestParam(value = "limit", defaultValue = "10") int limit) {

Iterable<ServerConfig> all = serverConfigRepository.findAll();

List<ServerConfig> serverConfigs = new ArrayList<>();

for (ServerConfig item : all) {
serverConfigs.add(item);
klboke marked this conversation as resolved.
Show resolved Hide resolved
}

try {
return serverConfigs.subList((offset - 1) * limit,
offset * limit > serverConfigs.size() ? serverConfigs.size() : offset * limit);
} catch (Exception ex) {
klboke marked this conversation as resolved.
Show resolved Hide resolved
return new ArrayList<>();
}
}

@GetMapping("/server/config/findAllConfigService")
public List<ServerConfig> findAllConfigService(
@RequestParam(value = "offset", defaultValue = "0") int offset,
klboke marked this conversation as resolved.
Show resolved Hide resolved
@RequestParam(value = "limit", defaultValue = "10") int limit) {
Connection conn = getConn();
List<ServerConfig> serverConfigs = new ArrayList<>();
String sql = "select * FROM serverconfig where 1=1 limit " + (offset - 1) * limit + "," + limit;
try {
PreparedStatement pst = (PreparedStatement) conn.prepareStatement(sql);

ResultSet rs = pst.executeQuery();

while (rs.next()) {
String key = rs.getString("Key");
String value = rs.getString("Value");
String comment = rs.getString("Comment");

ServerConfig config = new ServerConfig();
config.setKey(key);
config.setValue(value);
config.setComment(comment);

serverConfigs.add(config);
}
} catch (SQLException e) {
return null;
}

return serverConfigs;
}

@PreAuthorize(value = "@permissionValidator.isSuperAdmin()")
@GetMapping("/server/config/{key:.+}")
public ServerConfig loadServerConfig(@PathVariable String key) {
return serverConfigRepository.findByKey(key);
}

public Connection getConn() {
klboke marked this conversation as resolved.
Show resolved Hide resolved
// 数据库连接对象
Connection conn = null;

// 数据库链接地址
String dbUrl = "jdbc:mysql://localhost:3306/apolloconfigdb?characterEncoding=utf-8";

// 用户名
String dbUserName = "root";

// 密码
String dbPassword = "123456";

// 数据库驱动类
String jdbcName = "com.mysql.jdbc.Driver";

try {
// 加载数据库驱动类
Class.forName(jdbcName);
conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}

return conn;
}

}
Loading