From dc9fc73aee7134d1e3ea4423ce5caaaabc44dcd1 Mon Sep 17 00:00:00 2001 From: onemsg Date: Mon, 30 Sep 2024 19:52:33 +0800 Subject: [PATCH] Update Codes --- .../onemsg/authorization/ObservationTest.java | 28 +++ .../controller/AuthController.java | 21 ++ .../event/RefreshGroupStoreEvent.java | 10 + .../model/ApplicationCreation.java | 25 +++ .../model/ApplicationCreationRequest.java | 14 ++ .../application/model/ApplicationEntity.java | 20 ++ .../application/model/ApplicationNameVo.java | 11 + .../model/ApplicationUpdateRequest.java | 12 ++ .../model/GroupCreationRequest.java | 8 + .../application/model/GroupEntity.java | 14 ++ .../application/model/GroupInsertion.java | 6 + .../application/model/GroupNameVo.java | 8 + .../manager/model/RemoveIdRequest.java | 8 + .../protobuf/ProtobufInfoRepository.java | 198 ++++++++++++++++++ .../manager/protobuf/ProtobufInfoService.java | 125 +++++++++++ .../protobuf/model/ProtobufCodeCreation.java | 31 +++ .../model/ProtobufCodeCreationRequest.java | 11 + .../protobuf/model/ProtobufCodeEntity.java | 23 ++ .../protobuf/model/ProtobufCodeVersion.java | 18 ++ .../protobuf/model/ProtobufInfoCreation.java | 30 +++ .../model/ProtobufInfoCreationRequest.java | 19 ++ .../protobuf/model/ProtobufInfoEntity.java | 30 +++ .../model/ProtobufInfoUpdateIntroRequest.java | 9 + .../ProtobufInfoUpdateVersionRequest.java | 9 + .../manager/util/ProtobufInfoUtil.java | 20 ++ .../manager/util/ProbufInfoUtilTest.java | 20 ++ .../onemsg/protobuf/manager/ClientMain.java | 52 +++++ test-auth-service.rest | 1 + ...00\346\234\257\346\226\207\346\241\243.md" | 15 ++ 29 files changed, 796 insertions(+) create mode 100644 authorization-service/src/main/java/com/onemsg/authorization/ObservationTest.java create mode 100644 authorization-service/src/main/java/com/onemsg/authorization/controller/AuthController.java create mode 100644 protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/event/RefreshGroupStoreEvent.java create mode 100644 protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/ApplicationCreation.java create mode 100644 protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/ApplicationCreationRequest.java create mode 100644 protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/ApplicationEntity.java create mode 100644 protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/ApplicationNameVo.java create mode 100644 protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/ApplicationUpdateRequest.java create mode 100644 protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/GroupCreationRequest.java create mode 100644 protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/GroupEntity.java create mode 100644 protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/GroupInsertion.java create mode 100644 protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/GroupNameVo.java create mode 100644 protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/model/RemoveIdRequest.java create mode 100644 protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/ProtobufInfoRepository.java create mode 100644 protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/ProtobufInfoService.java create mode 100644 protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufCodeCreation.java create mode 100644 protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufCodeCreationRequest.java create mode 100644 protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufCodeEntity.java create mode 100644 protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufCodeVersion.java create mode 100644 protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufInfoCreation.java create mode 100644 protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufInfoCreationRequest.java create mode 100644 protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufInfoEntity.java create mode 100644 protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufInfoUpdateIntroRequest.java create mode 100644 protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufInfoUpdateVersionRequest.java create mode 100644 protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/util/ProtobufInfoUtil.java create mode 100644 protobuf-manager-spring/src/test/java/com/onemsg/protobuf/manager/util/ProbufInfoUtilTest.java create mode 100644 protobuf-manager/src/main/java/com/onemsg/protobuf/manager/ClientMain.java create mode 100644 test-auth-service.rest create mode 100644 "\346\212\200\346\234\257\346\226\207\346\241\243.md" diff --git a/authorization-service/src/main/java/com/onemsg/authorization/ObservationTest.java b/authorization-service/src/main/java/com/onemsg/authorization/ObservationTest.java new file mode 100644 index 0000000..b5f9763 --- /dev/null +++ b/authorization-service/src/main/java/com/onemsg/authorization/ObservationTest.java @@ -0,0 +1,28 @@ +package com.onemsg.authorization; + +import io.micrometer.core.instrument.MeterRegistry; +import io.micrometer.core.instrument.Timer; +import io.micrometer.core.instrument.simple.SimpleMeterRegistry; +import io.micrometer.observation.Observation; +import io.micrometer.observation.ObservationRegistry; + +public class ObservationTest { + + + public static void main(String[] args) { + // ObservationRegistry registry = ObservationRegistry.create(); + + // Observation.createNotStarted("foo", registry) + // .lowCardinalityKeyValue("lowTag", "lowTagValue") + // .highCardinalityKeyValue("highTag", "highTagValue") + // .observe(() -> System.out.println("Hello")); + + MeterRegistry registry = new SimpleMeterRegistry(); + Timer.Sample sample = Timer.start(registry); + try { + // do some work here + } finally { + sample.stop(Timer.builder("my.timer").register(registry)); + } + } +} diff --git a/authorization-service/src/main/java/com/onemsg/authorization/controller/AuthController.java b/authorization-service/src/main/java/com/onemsg/authorization/controller/AuthController.java new file mode 100644 index 0000000..e6c4181 --- /dev/null +++ b/authorization-service/src/main/java/com/onemsg/authorization/controller/AuthController.java @@ -0,0 +1,21 @@ +package com.onemsg.authorization.controller; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/api/authorization") +public class AuthController { + + @GetMapping("roles") + public ResponseEntity getRoles() { + return null; + } + + @GetMapping("perms") + public ResponseEntity getPerms() { + return null; + } +} diff --git a/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/event/RefreshGroupStoreEvent.java b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/event/RefreshGroupStoreEvent.java new file mode 100644 index 0000000..4333f99 --- /dev/null +++ b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/event/RefreshGroupStoreEvent.java @@ -0,0 +1,10 @@ +package com.onemsg.protobuf.manager.application.event; + +import org.springframework.context.ApplicationEvent; + +public class RefreshGroupStoreEvent extends ApplicationEvent{ + + public RefreshGroupStoreEvent(Object source) { + super(source); + } +} diff --git a/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/ApplicationCreation.java b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/ApplicationCreation.java new file mode 100644 index 0000000..41f90d5 --- /dev/null +++ b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/ApplicationCreation.java @@ -0,0 +1,25 @@ +package com.onemsg.protobuf.manager.application.model; + +import java.util.Objects; + +import org.springframework.beans.BeanUtils; + +public class ApplicationCreation { + public String name; + public String zhName; + public String intro; + public int groupId; + public String groupName; + public String creator; + + public String getFullName() { + return groupName + "/" + name; + } + + public static ApplicationCreation create(ApplicationCreationRequest request) { + Objects.requireNonNull(request); + ApplicationCreation creation = new ApplicationCreation(); + BeanUtils.copyProperties(request, creation); + return creation; + } +} diff --git a/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/ApplicationCreationRequest.java b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/ApplicationCreationRequest.java new file mode 100644 index 0000000..f7737f1 --- /dev/null +++ b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/ApplicationCreationRequest.java @@ -0,0 +1,14 @@ +package com.onemsg.protobuf.manager.application.model; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Positive; + +public class ApplicationCreationRequest { + + @NotBlank + public String name; + public String zhName; + public String intro; + @Positive + public int groupId; +} diff --git a/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/ApplicationEntity.java b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/ApplicationEntity.java new file mode 100644 index 0000000..a010319 --- /dev/null +++ b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/ApplicationEntity.java @@ -0,0 +1,20 @@ +package com.onemsg.protobuf.manager.application.model; + +import java.time.LocalDateTime; + +public class ApplicationEntity { + + public int id; + public String name; + public String zhName; + public String intro; + public int groupId; + public String groupName; + public String creator; + public LocalDateTime createdTime; + public LocalDateTime updatedTime; + + public int getId() { + return id; + } +} diff --git a/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/ApplicationNameVo.java b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/ApplicationNameVo.java new file mode 100644 index 0000000..0ef430a --- /dev/null +++ b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/ApplicationNameVo.java @@ -0,0 +1,11 @@ +package com.onemsg.protobuf.manager.application.model; + +public record ApplicationNameVo( + int id, + String name +) { + + public static ApplicationNameVo create(ApplicationEntity entity) { + return new ApplicationNameVo(entity.id, entity.name); + } +} diff --git a/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/ApplicationUpdateRequest.java b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/ApplicationUpdateRequest.java new file mode 100644 index 0000000..420b217 --- /dev/null +++ b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/ApplicationUpdateRequest.java @@ -0,0 +1,12 @@ +package com.onemsg.protobuf.manager.application.model; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Positive; + +public class ApplicationUpdateRequest { + @Positive + public int applicationId; + @NotBlank + public String zhName; + public String intro; +} diff --git a/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/GroupCreationRequest.java b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/GroupCreationRequest.java new file mode 100644 index 0000000..19b9c3c --- /dev/null +++ b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/GroupCreationRequest.java @@ -0,0 +1,8 @@ +package com.onemsg.protobuf.manager.application.model; + +import jakarta.validation.constraints.NotBlank; + +public class GroupCreationRequest { + @NotBlank + public String name; +} diff --git a/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/GroupEntity.java b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/GroupEntity.java new file mode 100644 index 0000000..62388a7 --- /dev/null +++ b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/GroupEntity.java @@ -0,0 +1,14 @@ +package com.onemsg.protobuf.manager.application.model; + +import java.time.LocalDateTime; + +/** + * ApplicationEntity + */ +public record GroupEntity( + int id, + String name, + String creator, + LocalDateTime create +) { +} \ No newline at end of file diff --git a/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/GroupInsertion.java b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/GroupInsertion.java new file mode 100644 index 0000000..658e65d --- /dev/null +++ b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/GroupInsertion.java @@ -0,0 +1,6 @@ +package com.onemsg.protobuf.manager.application.model; + +public class GroupInsertion { + public String name; + public String creator; +} diff --git a/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/GroupNameVo.java b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/GroupNameVo.java new file mode 100644 index 0000000..91b7879 --- /dev/null +++ b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/application/model/GroupNameVo.java @@ -0,0 +1,8 @@ +package com.onemsg.protobuf.manager.application.model; + +public record GroupNameVo(int id, String name) { + + public static GroupNameVo create(GroupEntity entity) { + return new GroupNameVo(entity.id(), entity.name()); + } +} diff --git a/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/model/RemoveIdRequest.java b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/model/RemoveIdRequest.java new file mode 100644 index 0000000..966a007 --- /dev/null +++ b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/model/RemoveIdRequest.java @@ -0,0 +1,8 @@ +package com.onemsg.protobuf.manager.model; + +import jakarta.validation.constraints.Positive; + +public class RemoveIdRequest { + @Positive + public int id; +} diff --git a/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/ProtobufInfoRepository.java b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/ProtobufInfoRepository.java new file mode 100644 index 0000000..c756495 --- /dev/null +++ b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/ProtobufInfoRepository.java @@ -0,0 +1,198 @@ +package com.onemsg.protobuf.manager.protobuf; + +import java.util.List; +import java.util.Optional; + +import org.springframework.dao.DataAccessException; +import org.springframework.jdbc.core.simple.JdbcClient; +import org.springframework.jdbc.support.GeneratedKeyHolder; +import org.springframework.jdbc.support.KeyHolder; +import org.springframework.stereotype.Repository; + +import com.onemsg.protobuf.manager.protobuf.model.ProtobufCodeCreation; +import com.onemsg.protobuf.manager.protobuf.model.ProtobufCodeEntity; +import com.onemsg.protobuf.manager.protobuf.model.ProtobufCodeVersion; +import com.onemsg.protobuf.manager.protobuf.model.ProtobufInfoCreation; +import com.onemsg.protobuf.manager.protobuf.model.ProtobufInfoEntity; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@Repository +public class ProtobufInfoRepository { + + private final JdbcClient jdbcClient; + + public ProtobufInfoRepository(JdbcClient jdbcClient) { + this.jdbcClient = jdbcClient; + } + + public List search(String search, int skip, int limit) { + String sql = """ + SELECT id, name, intro, application_name group_name, protocol, current_version, creator, created_time, updated_time + FROM `protobuf_info` + WHERE group_name like ? OR application_name like ? OR name like ? OR creator like ? + ORDER BY id DESC + LIMIT ?,? + """; + String word = "%" + search + "%"; + return jdbcClient.sql(sql).params(word, word, word, word, skip, limit) + .query(ProtobufInfoEntity.class) + .list(); + } + + + public int count(String search) { + String sql = """ + SELECT count(id) FROM `protobuf_info` + WHERE group_name like ? OR application_name like ? OR name like ? + """; + String word = "%" + search + "%"; + return jdbcClient.sql(sql) + .params(word, word, word) + .query(int.class) + .single(); + } + + public List search(int skip, int limit) { + String sql = """ + SELECT id, name, intro, application_name group_name, protocol, current_version, creator, created_time, updated_time + FROM `protobuf_info` + ORDER BY id DESC + LIMIT ?,? + """; + + return jdbcClient.sql(sql).params(skip, limit).query(ProtobufInfoEntity.class).list(); + } + + public int count() { + String sql = "SELECT count(id) FROM `protobuf_info`"; + return jdbcClient.sql(sql).query(int.class).single(); + } + + public Optional findById(int id) { + String sql = """ + SELECT id, name, intro, application_name group_name, protocol, current_version, creator, created_time, updated_time + FROM `protobuf_info` WHERE id = ? + """; + return jdbcClient.sql(sql).param(id).query(ProtobufInfoEntity.class).optional(); + } + + public boolean existsById(int id) { + String sql = "SELECT count(id) FROM `protobuf_info` WHERE id = ?"; + return jdbcClient.sql(sql).param(id).query(int.class).single() > 0; + } + + public int updateIntroById(int id, String intro) { + String sql = "UPDATE `protobuf_info` SET `intro` = ? WHERE `id` = ?"; + return jdbcClient.sql(sql).param(intro, id).update(); + } + + public int insert(ProtobufInfoCreation creation) { + String sql = """ + INSERT INTO `protobuf_info`(name, intro, application_name, group_name, protocol, creator) + VALUES(:name, :intro, :applicationName, :groupName, :protocol, :creator); + """; + + log.info("Insert {}", creation); + + KeyHolder keyHolder = new GeneratedKeyHolder(); + jdbcClient.sql(sql) + .paramSource(creation) + .update(keyHolder, "id"); + + return getKeyAsInt(keyHolder, "protobuf_info"); + } + + + private static int getKeyAsInt(KeyHolder keyHolder, String tableName) { + var key = keyHolder.getKey(); + if (key == null) { + String msg = String.format("获取 Table[%s] 自增ID失败", tableName); + throw new DataAccessException(msg) {}; + } + return key.intValue(); + } + + public int delete(int id) { + String sql = "DELETE FROM `protobuf_info` WHERE id = ?"; + return jdbcClient.sql(sql).param(id).update(); + } + + public int insertCode(ProtobufCodeCreation creation) { + + int nextVersion = jdbcClient.sql("SELECT _next_version FROM `protobuf_info` WHERE id = ? FOR UPDATE") + .param(creation.protobufId) + .query(int.class) + .single(); + + creation.version = nextVersion; + + String insertSql = """ + INSERT INTO `protobuf_code`(protobuf_id, protobuf_name, code, version, creator) + VALUES(:protobufId, :protobufName, :code, :version, :creator ); + """; + KeyHolder keyHolder = new GeneratedKeyHolder(); + jdbcClient.sql(insertSql).paramSource(creation).update(keyHolder, "id"); + + String updateSql = "UPDATE `protobuf_info` SET current_version = ?, _next_version = _next_version + 1 WHERE id = ?"; + jdbcClient.sql(updateSql) + .param(nextVersion) + .param(creation.protobufId) + .update(); + + return getKeyAsInt(keyHolder, "protobuf_code"); + } + + public int updateCurrentVersion(int id, int version) { + String updateSql = "UPDATE `protobuf_info` SET current_version = ? WHERE id = ?"; + return jdbcClient.sql(updateSql).param(version, id).update(); + } + + public Optional findCodeById(int codeId) { + String sql = """ + SELECT id, protobuf_id, protobuf_name, code, version, creator, created_time + FROM `protobuf_code` WHERE id = ? + """; + return jdbcClient.sql(sql).param(codeId).query(ProtobufCodeEntity.class).optional(); + } + + public Optional findCodeByProtobufIdAndVersion(int protobufId, int version) { + String sql = """ + SELECT id, protobuf_id, protobuf_name, code, version, creator, created_time + FROM `protobuf_code` + WHERE protobuf_id = ? AND version = ? + """; + return jdbcClient.sql(sql).param(protobufId, version).query(ProtobufCodeEntity.class).optional(); + } + + public Optional findCurrentCodeByProtobufId(int protobufId) { + String sql = """ + SELECT id, protobuf_id, protobuf_name, code, version, creator, created_time + FROM `protobuf_code` + WHERE protobuf_id = ? + AND version in (SELECT current_version FROM `current_version` WHERE id = ?) + """; + return jdbcClient.sql(sql).param(protobufId, protobufId).query(ProtobufCodeEntity.class).optional(); + } + + public boolean existsCodeByProtobufIdAndVersion(int protobufId, int version) { + String sql = """ + SELECT count(id) + FROM `protobuf_code` + WHERE protobuf_id = ? AND version = ? + """; + return jdbcClient.sql(sql).param(protobufId, version).query(int.class).single() > 0; + } + + public List findCodeVersionListByProtobufId(int protobufId) { + String sql = """ + SELECT code.id, code.version, code.version = info.current_version as is_current, code.creator, code.created_time + FROM `protobuf_code` as code + LEFT JOIN `protobuf_info` as info ON code.protobuf_id = info.id + WHERE code.protobuf_id = ? + """; + return jdbcClient.sql(sql).param(protobufId).query(ProtobufCodeVersion.class).list(); + } + +} diff --git a/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/ProtobufInfoService.java b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/ProtobufInfoService.java new file mode 100644 index 0000000..b4e6dbf --- /dev/null +++ b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/ProtobufInfoService.java @@ -0,0 +1,125 @@ +package com.onemsg.protobuf.manager.protobuf; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.lang.Nullable; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Isolation; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.StringUtils; + +import com.onemsg.protobuf.manager.exception.NotExistedException; +import com.onemsg.protobuf.manager.model.Pageable; +import com.onemsg.protobuf.manager.model.Totalable; +import com.onemsg.protobuf.manager.protobuf.model.ProtobufCodeCreation; +import com.onemsg.protobuf.manager.protobuf.model.ProtobufCodeEntity; +import com.onemsg.protobuf.manager.protobuf.model.ProtobufCodeVersion; +import com.onemsg.protobuf.manager.protobuf.model.ProtobufInfoCreation; +import com.onemsg.protobuf.manager.protobuf.model.ProtobufInfoEntity; + +import lombok.AllArgsConstructor; + +@Service +@AllArgsConstructor +public class ProtobufInfoService { + + @Autowired + private final ProtobufInfoRepository protobufInfoRepository; + + /** + * + * @param search 应用名|protobuf名|author名 + * @param pageIndex + * @param pageSize + * @return + */ + public Totalable search(String search, int pageIndex, int pageSize) { + int skip = Pageable.toSkip(pageIndex, pageSize); + + List entities = null; + int count = 0; + if (StringUtils.hasText(search)) { + entities = protobufInfoRepository.search(search, skip, pageSize); + count = protobufInfoRepository.count(search); + } else { + entities = protobufInfoRepository.search(skip, pageSize); + count = protobufInfoRepository.count(); + } + return Totalable.create(entities, count); + } + + public ProtobufInfoEntity getInfoById(int id) throws NotExistedException { + return protobufInfoRepository.findById(id) + .orElseThrow(() -> new NotExistedException("Protobuf", id)); + } + + @Transactional + public int create(ProtobufInfoCreation creation) { + return protobufInfoRepository.insert(creation); + } + + @Transactional + public int updateIntro(int id, String intro) throws NotExistedException { + requireExistsById(id); + return protobufInfoRepository.updateIntroById(id, intro); + } + + @Transactional + public int delete(int id) { + return protobufInfoRepository.delete(id); + } + + @Transactional + public int createProtobufCode(ProtobufCodeCreation creation) throws NotExistedException { + requireExistsById(creation.protobufId); + var optional = protobufInfoRepository.findById(creation.protobufId); + if (optional.isEmpty()) { + throw new NotExistedException("Protobuf", creation.protobufId); + } + creation.protobufName = optional.get().name; + return protobufInfoRepository.insertCode(creation); + } + + public ProtobufCodeEntity getProtobufCodeById(int id) throws NotExistedException { + return protobufInfoRepository.findCodeById(id) + .orElseThrow(() -> new NotExistedException("PROTOBUF_CODE", id)); + } + + public ProtobufCodeEntity getProtobufCodeByProtobufIdAndVersion(int id, int version) throws NotExistedException { + return protobufInfoRepository.findCodeByProtobufIdAndVersion(id, version) + .orElseThrow(() -> new NotExistedException("PROTOBUF_CODE", id)); + } + + + @Nullable + public ProtobufCodeEntity getCurrentProtobufCodeByProtobufId(int protobufId) throws NotExistedException { + requireExistsById(protobufId); + return protobufInfoRepository.findCurrentCodeByProtobufId(protobufId).orElse(null); + } + + public List getProtobufCodeVersionsByProtobufId(int protobufId) throws NotExistedException { + requireExistsById(protobufId); + return protobufInfoRepository.findCodeVersionListByProtobufId(protobufId); + } + + @Transactional(isolation = Isolation.REPEATABLE_READ) + public void updateCurrentVersion(int protobufId, int version) throws NotExistedException { + requireExistsById(protobufId); + requireExistCodeVersionByProtobufId(protobufId, version); + protobufInfoRepository.updateCurrentVersion(protobufId, version); + } + + private void requireExistsById(int id) throws NotExistedException { + if (!protobufInfoRepository.existsById(id)) { + throw new NotExistedException("Protobuf", id); + } + } + + public void requireExistCodeVersionByProtobufId(int id, int version) throws NotExistedException { + if (!protobufInfoRepository.existsCodeByProtobufIdAndVersion(id, version)) { + throw new NotExistedException("PROTOBUF_CODE.VERSION", version); + } + } + +} diff --git a/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufCodeCreation.java b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufCodeCreation.java new file mode 100644 index 0000000..cb37702 --- /dev/null +++ b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufCodeCreation.java @@ -0,0 +1,31 @@ +package com.onemsg.protobuf.manager.protobuf.model; + +import java.util.Objects; + +import org.springframework.beans.BeanUtils; + +import com.onemsg.protobuf.manager.util.ProtobufInfoUtil; + +/** + * ProtobufCodeCreation + */ +public class ProtobufCodeCreation { + public int protobufId; + public String protobufName; + public String code; + public int version; + public String creator; + + public String getVersionText() { + return ProtobufInfoUtil.versionAsText(version); + } + + public static ProtobufCodeCreation create(ProtobufCodeCreationRequest request, String creator) { + Objects.requireNonNull(request); + ProtobufCodeCreation o = new ProtobufCodeCreation(); + o.protobufId = request.protobufId; + o.code = request.code; + o.creator = creator; + return o; + } +} \ No newline at end of file diff --git a/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufCodeCreationRequest.java b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufCodeCreationRequest.java new file mode 100644 index 0000000..b1c8bf9 --- /dev/null +++ b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufCodeCreationRequest.java @@ -0,0 +1,11 @@ +package com.onemsg.protobuf.manager.protobuf.model; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Positive; + +public class ProtobufCodeCreationRequest { + @Positive + public int protobufId; + @NotBlank + public String code; +} diff --git a/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufCodeEntity.java b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufCodeEntity.java new file mode 100644 index 0000000..c7d8af7 --- /dev/null +++ b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufCodeEntity.java @@ -0,0 +1,23 @@ +package com.onemsg.protobuf.manager.protobuf.model; + +import java.time.LocalDateTime; + +import com.onemsg.protobuf.manager.util.ProtobufInfoUtil; + +import lombok.ToString; + +@ToString +public class ProtobufCodeEntity { + + public int id; + public int protobufId; + public String protobufName; + public String code; + public int version; + public String creator; + public LocalDateTime createdTime; + + public String getVersionText() { + return ProtobufInfoUtil.versionAsText(version); + } +} diff --git a/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufCodeVersion.java b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufCodeVersion.java new file mode 100644 index 0000000..edfbc9a --- /dev/null +++ b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufCodeVersion.java @@ -0,0 +1,18 @@ +package com.onemsg.protobuf.manager.protobuf.model; + +import java.time.LocalDateTime; + +import com.onemsg.protobuf.manager.util.ProtobufInfoUtil; + +public class ProtobufCodeVersion { + + public int id; + public int version; + public boolean isCurrent = false; + public String creator; + public LocalDateTime createdTime; + + public String getVersionText() { + return ProtobufInfoUtil.versionAsText(version); + } +} diff --git a/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufInfoCreation.java b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufInfoCreation.java new file mode 100644 index 0000000..2a0cd80 --- /dev/null +++ b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufInfoCreation.java @@ -0,0 +1,30 @@ +package com.onemsg.protobuf.manager.protobuf.model; + +import java.util.Objects; + +import org.springframework.beans.BeanUtils; + +import lombok.ToString; + +@ToString +public class ProtobufInfoCreation { + + public String name; + public String intro; + public String applicationName; + public String groupName; + public String protocol; + public String creator; + + public static ProtobufInfoCreation create(ProtobufInfoCreationRequest request, String creator) { + Objects.requireNonNull(request); + ProtobufInfoCreation o = new ProtobufInfoCreation(); + o.name = request.name; + o.intro = request.intro; + o.applicationName = request.applicationName; + o.groupName = request.groupName; + o.protocol = request.protocol; + o.creator = creator; + return o; + } +} diff --git a/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufInfoCreationRequest.java b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufInfoCreationRequest.java new file mode 100644 index 0000000..b7d85e9 --- /dev/null +++ b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufInfoCreationRequest.java @@ -0,0 +1,19 @@ +package com.onemsg.protobuf.manager.protobuf.model; + +import jakarta.validation.constraints.NotBlank; +import lombok.ToString; + +@ToString +public class ProtobufInfoCreationRequest { + + @NotBlank + public String name; + public String intro; + @NotBlank + public String applicationName; + @NotBlank + public String groupName; + @NotBlank + public String protocol; + +} diff --git a/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufInfoEntity.java b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufInfoEntity.java new file mode 100644 index 0000000..f569559 --- /dev/null +++ b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufInfoEntity.java @@ -0,0 +1,30 @@ +package com.onemsg.protobuf.manager.protobuf.model; + +import java.time.LocalDateTime; + +import com.onemsg.protobuf.manager.util.ProtobufInfoUtil; + +/** + * ProtobufInfoEntity + */ +public class ProtobufInfoEntity { + + public int id; + public String name; + public String intro; + public String applicationName; + public String groupName; + public String protocol; + public int currentVersion; + public String creator; + public LocalDateTime createdTime; + public LocalDateTime updatedTime; + + public String getFullName() { + return groupName + "/" + applicationName + "/" + name; + } + + public String getCurrentVersionText() { + return ProtobufInfoUtil.versionAsText(currentVersion); + } +} \ No newline at end of file diff --git a/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufInfoUpdateIntroRequest.java b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufInfoUpdateIntroRequest.java new file mode 100644 index 0000000..b60c076 --- /dev/null +++ b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufInfoUpdateIntroRequest.java @@ -0,0 +1,9 @@ +package com.onemsg.protobuf.manager.protobuf.model; + +import jakarta.validation.constraints.NotBlank; + +public class ProtobufInfoUpdateIntroRequest { + + @NotBlank + public String intro; +} diff --git a/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufInfoUpdateVersionRequest.java b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufInfoUpdateVersionRequest.java new file mode 100644 index 0000000..5bbdf7b --- /dev/null +++ b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/protobuf/model/ProtobufInfoUpdateVersionRequest.java @@ -0,0 +1,9 @@ +package com.onemsg.protobuf.manager.protobuf.model; + +import jakarta.validation.constraints.Positive; + +public class ProtobufInfoUpdateVersionRequest { + + @Positive + public int verison; +} diff --git a/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/util/ProtobufInfoUtil.java b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/util/ProtobufInfoUtil.java new file mode 100644 index 0000000..1ad58e6 --- /dev/null +++ b/protobuf-manager-spring/src/main/java/com/onemsg/protobuf/manager/util/ProtobufInfoUtil.java @@ -0,0 +1,20 @@ +package com.onemsg.protobuf.manager.util; + +public class ProtobufInfoUtil { + + private ProtobufInfoUtil() {} + + public static String versionAsText(int version) { + if (version < 0) + return String.valueOf(version); + + var sb = new StringBuilder(); + sb.append(version); + while (sb.length() < 3) { + sb.insert(0, '0'); + } + sb.insert(sb.length() - 2, "."); + sb.insert(sb.length() - 1, "."); + return sb.toString(); + } +} diff --git a/protobuf-manager-spring/src/test/java/com/onemsg/protobuf/manager/util/ProbufInfoUtilTest.java b/protobuf-manager-spring/src/test/java/com/onemsg/protobuf/manager/util/ProbufInfoUtilTest.java new file mode 100644 index 0000000..baa0736 --- /dev/null +++ b/protobuf-manager-spring/src/test/java/com/onemsg/protobuf/manager/util/ProbufInfoUtilTest.java @@ -0,0 +1,20 @@ +package com.onemsg.protobuf.manager.util; + +import static com.onemsg.protobuf.manager.util.ProtobufInfoUtil.versionAsText; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class ProbufInfoUtilTest { + + @Test + public void testVersionAsText() { + assertEquals("-1", versionAsText(-1)); + assertEquals("0.0.0", versionAsText(0)); + assertEquals("0.0.1", versionAsText(1)); + assertEquals("0.1.1", versionAsText(11)); + assertEquals("1.1.1", versionAsText(111)); + assertEquals("11.1.1", versionAsText(1111)); + + } +} diff --git a/protobuf-manager/src/main/java/com/onemsg/protobuf/manager/ClientMain.java b/protobuf-manager/src/main/java/com/onemsg/protobuf/manager/ClientMain.java new file mode 100644 index 0000000..0303b66 --- /dev/null +++ b/protobuf-manager/src/main/java/com/onemsg/protobuf/manager/ClientMain.java @@ -0,0 +1,52 @@ +package com.onemsg.protobuf.manager; + +import io.vertx.core.Vertx; +import io.vertx.core.http.HttpClientOptions; +import io.vertx.core.http.HttpMethod; +import io.vertx.core.http.HttpVersion; +import io.vertx.core.http.RequestOptions; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class ClientMain { + + public static void main(String[] args) { + Vertx vertx = Vertx.vertx(); + + var client = vertx + .createHttpClient(new HttpClientOptions().setProtocolVersion(HttpVersion.HTTP_2).setMaxPoolSize(5) + .setHttp2MaxPoolSize(7) + ); + + var reqOptions = new RequestOptions() + .setAbsoluteURI("http://localhost:5000/sleep?time=2000") + .setMethod(HttpMethod.GET); + + client.connectionHandler(conn -> { + log.info("Build conn {} {}" , conn.localAddress(), conn.sslSession()); + conn.closeHandler(v -> { + log.info("{} close", conn.localAddress()); + }); + }); + + for (int i = 0; i < 1000; i++) { + final boolean lasted = i == 99; + client.request(reqOptions) + .onSuccess(req -> req.send() + .onSuccess(res -> { + log.info("{} {} {}", res.version(), res.statusCode(), res.request().streamId()); + if (lasted) { + vertx.setTimer(1000, id -> { + vertx.close() + .onSuccess((v) -> { + log.info("Vertx close"); + }); + }); + + + } + })) + .onFailure(System.out::println); + } + } +} diff --git a/test-auth-service.rest b/test-auth-service.rest new file mode 100644 index 0000000..5cadc74 --- /dev/null +++ b/test-auth-service.rest @@ -0,0 +1 @@ +GET http://localhost:9900/api/authorization/r \ No newline at end of file diff --git "a/\346\212\200\346\234\257\346\226\207\346\241\243.md" "b/\346\212\200\346\234\257\346\226\207\346\241\243.md" new file mode 100644 index 0000000..6a90808 --- /dev/null +++ "b/\346\212\200\346\234\257\346\226\207\346\241\243.md" @@ -0,0 +1,15 @@ +# 技术文档 + +## 数据模型 + +表名 | 名称 | 说明 +--- | --- | --- +group | 业务组 | 公司业务组,可用 "/" 分级 +application | 应用 | 对应Git仓库、部署应用等 +protobuf_info | Protobuf 信息 | Protobuf 名称、协议等 +protobuf_code | Protobuf Code | Protobuf 代码信息 + +group : application = 1 : N +application : protobuf_info = 1 : N +protobuf_info : protobuf_code = 1 : N +