From 76ed47c1b3f6b8e4596f24b3f70aff0f0e7d24bb Mon Sep 17 00:00:00 2001 From: lensferno Date: Sat, 5 Aug 2023 01:39:37 +0800 Subject: [PATCH] aa --- backend-main/backend-data/pom.xml | 20 ++++ .../java/cn/wustlinghang/main/data/Main.java | 7 -- .../data/dao/mapper/StudentMapper.java | 9 ++ .../wusthelper/data/entity/Student.java | 96 +++++++++++++++++++ backend-main/backend-web/pom.xml | 53 ++-------- .../cn/wustlinghang/main/web/BackendMain.java | 17 ---- .../wusthelper/WebBackendMain.java | 23 +++++ .../wusthelper/web/api/Health.java | 13 +++ .../v2/undergrade/UndergradController.java | 6 +- .../web/configure/CaffeineCacheConfigure.java | 34 +++++++ .../wusthelper/web/entity/CookieType.java | 35 +++++++ .../web/rpc/undergrad/CookieRemote.java | 18 ++++ .../web/rpc/undergrad/CourseTableRemote.java | 27 ++++++ .../web/rpc/undergrad/CreditStatusRemote.java | 22 +++++ .../undergrad/ExamDelayApplicationRemote.java | 27 ++++++ .../web/rpc/undergrad/ScoreRemote.java | 25 +++++ .../web/rpc/undergrad/StudentInfoRemote.java | 23 +++++ .../web/rpc/undergrad/TrainingPlanRemote.java | 22 +++++ .../campus/undergrad/CourseTableService.java | 33 +++++++ .../campus/undergrad/CreditStatusService.java | 29 ++++++ .../ExamDelayApplicationService.java | 31 ++++++ .../campus/undergrad/ScoreService.java | 32 +++++++ .../campus/undergrad/StudentInfoService.java | 30 ++++++ .../campus/undergrad/TrainingPlanService.java | 29 ++++++ .../undergrad/UndergradCookieService.java | 21 ++++ .../web/service/cookie/CookieManager.java | 62 ++++++++++++ .../src/main/resources/application.yml | 5 + .../wustlinghang/wusthelper/StudentTest.java | 26 +++++ .../cn/wustlinghang/wusthelper/TestMain.java | 29 ++++++ .../src/test/resources/application-test.yml | 24 +++++ backend-main/pom.xml | 40 ++++++++ .../internal/rpc/response/RpcResponse.java | 19 ---- .../internal/rpc/response/RpcResponseDto.java | 19 ++++ .../{main => web}/response/Response.java | 2 +- .../{main => web}/response/ResponseCode.java | 2 +- .../rpc-frp-consul/.flattened-pom.xml | 59 ++++++++++++ .../http/v1/handler/BaseExceptionHandler.java | 6 +- .../ResponseWrapperInterceptor.java | 4 +- .../internal/graduate/rpc/Register.java | 40 ++++++++ .../graduate/rpc/config/RpcConfig.java | 33 +++++++ .../graduate/rpc/health/HealthCheck.java | 14 +++ .../http/v1/handler/BaseExceptionHandler.java | 6 +- .../ResponseWrapperInterceptor.java | 4 +- .../internal/library/rpc/Register.java | 40 ++++++++ .../library/rpc/config/RpcConfig.java | 33 +++++++ .../library/rpc/health/HealthCheck.java | 14 +++ .../http/v1/handler/BaseExceptionHandler.java | 6 +- .../ResponseWrapperInterceptor.java | 4 +- .../internal/physics/rpc/Register.java | 40 ++++++++ .../physics/rpc/config/RpcConfig.java | 33 +++++++ .../physics/rpc/health/HealthCheck.java | 14 +++ .../http/v1/handler/BaseExceptionHandler.java | 6 +- .../ResponseWrapperInterceptor.java | 4 +- 53 files changed, 1156 insertions(+), 114 deletions(-) delete mode 100644 backend-main/backend-data/src/main/java/cn/wustlinghang/main/data/Main.java create mode 100644 backend-main/backend-data/src/main/java/cn/wustlinghang/wusthelper/data/dao/mapper/StudentMapper.java create mode 100644 backend-main/backend-data/src/main/java/cn/wustlinghang/wusthelper/data/entity/Student.java delete mode 100644 backend-main/backend-web/src/main/java/cn/wustlinghang/main/web/BackendMain.java create mode 100644 backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/WebBackendMain.java create mode 100644 backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/api/Health.java rename backend-main/backend-web/src/main/java/cn/wustlinghang/{main => wusthelper}/web/api/v2/undergrade/UndergradController.java (77%) create mode 100644 backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/configure/CaffeineCacheConfigure.java create mode 100644 backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/entity/CookieType.java create mode 100644 backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/rpc/undergrad/CookieRemote.java create mode 100644 backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/rpc/undergrad/CourseTableRemote.java create mode 100644 backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/rpc/undergrad/CreditStatusRemote.java create mode 100644 backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/rpc/undergrad/ExamDelayApplicationRemote.java create mode 100644 backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/rpc/undergrad/ScoreRemote.java create mode 100644 backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/rpc/undergrad/StudentInfoRemote.java create mode 100644 backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/rpc/undergrad/TrainingPlanRemote.java create mode 100644 backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/campus/undergrad/CourseTableService.java create mode 100644 backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/campus/undergrad/CreditStatusService.java create mode 100644 backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/campus/undergrad/ExamDelayApplicationService.java create mode 100644 backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/campus/undergrad/ScoreService.java create mode 100644 backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/campus/undergrad/StudentInfoService.java create mode 100644 backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/campus/undergrad/TrainingPlanService.java create mode 100644 backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/campus/undergrad/UndergradCookieService.java create mode 100644 backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/cookie/CookieManager.java create mode 100644 backend-main/backend-web/src/test/java/cn/wustlinghang/wusthelper/StudentTest.java create mode 100644 backend-main/backend-web/src/test/java/cn/wustlinghang/wusthelper/TestMain.java create mode 100644 backend-main/backend-web/src/test/resources/application-test.yml delete mode 100644 common/src/main/java/cn/wustlinghang/wusthelper/internal/rpc/response/RpcResponse.java create mode 100644 common/src/main/java/cn/wustlinghang/wusthelper/internal/rpc/response/RpcResponseDto.java rename common/src/main/java/cn/wustlinghang/wusthelper/{main => web}/response/Response.java (92%) rename common/src/main/java/cn/wustlinghang/wusthelper/{main => web}/response/ResponseCode.java (90%) create mode 100644 external-library/rpc-frp-consul/.flattened-pom.xml create mode 100644 sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/rpc/Register.java create mode 100644 sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/rpc/config/RpcConfig.java create mode 100644 sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/rpc/health/HealthCheck.java create mode 100644 sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/rpc/Register.java create mode 100644 sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/rpc/config/RpcConfig.java create mode 100644 sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/rpc/health/HealthCheck.java create mode 100644 sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/rpc/Register.java create mode 100644 sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/rpc/config/RpcConfig.java create mode 100644 sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/rpc/health/HealthCheck.java diff --git a/backend-main/backend-data/pom.xml b/backend-main/backend-data/pom.xml index 03ca9a1..8b28686 100644 --- a/backend-main/backend-data/pom.xml +++ b/backend-main/backend-data/pom.xml @@ -16,6 +16,26 @@ 17 17 UTF-8 + + 3.5.3.1 + 3.0.2 + + + com.baomidou + mybatis-plus-boot-starter + ${mybatis-plus.version} + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + ${mybatis.version} + + + com.mysql + mysql-connector-j + runtime + + \ No newline at end of file diff --git a/backend-main/backend-data/src/main/java/cn/wustlinghang/main/data/Main.java b/backend-main/backend-data/src/main/java/cn/wustlinghang/main/data/Main.java deleted file mode 100644 index f550351..0000000 --- a/backend-main/backend-data/src/main/java/cn/wustlinghang/main/data/Main.java +++ /dev/null @@ -1,7 +0,0 @@ -package cn.wustlinghang.main.data; - -public class Main { - public static void main(String[] args) { - System.out.println("Hello world!"); - } -} \ No newline at end of file diff --git a/backend-main/backend-data/src/main/java/cn/wustlinghang/wusthelper/data/dao/mapper/StudentMapper.java b/backend-main/backend-data/src/main/java/cn/wustlinghang/wusthelper/data/dao/mapper/StudentMapper.java new file mode 100644 index 0000000..4752f6b --- /dev/null +++ b/backend-main/backend-data/src/main/java/cn/wustlinghang/wusthelper/data/dao/mapper/StudentMapper.java @@ -0,0 +1,9 @@ +package cn.wustlinghang.wusthelper.data.dao.mapper; + +import cn.wustlinghang.wusthelper.data.entity.Student; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface StudentMapper extends BaseMapper { +} diff --git a/backend-main/backend-data/src/main/java/cn/wustlinghang/wusthelper/data/entity/Student.java b/backend-main/backend-data/src/main/java/cn/wustlinghang/wusthelper/data/entity/Student.java new file mode 100644 index 0000000..ea5f7da --- /dev/null +++ b/backend-main/backend-data/src/main/java/cn/wustlinghang/wusthelper/data/entity/Student.java @@ -0,0 +1,96 @@ +package cn.wustlinghang.wusthelper.data.entity; + +import lombok.Data; + +@Data +public class Student { + /** + * 本科生id + */ + private Long id; + + /** + * 学号 + */ + private String stuNum; + + /** + * 姓名 + */ + private String stuName; + + /** + * 教务处密码 + */ + private String jwcPwd; + + /** + * 物理实验系统密码 + */ + private String wlsyPwd; + + /** + * 学院编号 + */ + private Long collegeId; + + /** + * 专业编号 + */ + private Long majorId; + + /** + * 班级编号 + */ + private Long classId; + + /** + * 生日 + */ + private String birthday; + + /** + * 性别 + */ + private String sex; + + /** + * 民族 + */ + private String nation; + + /** + * 籍贯 + */ + private String nativePlace; + + /** + * 昵称 + */ + private String nickName; + + /** + * 电话 + */ + private String phone; + + /** + * 电子邮箱 + */ + private String email; + + /** + * qq号 + */ + private String qqNum; + + /** + * 微信号码 + */ + private String wechatNum; + + /** + * 客户端平台 + */ + private String platform; +} \ No newline at end of file diff --git a/backend-main/backend-web/pom.xml b/backend-main/backend-web/pom.xml index 4620992..f1d5644 100644 --- a/backend-main/backend-web/pom.xml +++ b/backend-main/backend-web/pom.xml @@ -16,37 +16,12 @@ 17 UTF-8 - 3.1.1 - 2.0 4.0.2 + 4.0.3 - - - - - org.springframework.boot - spring-boot-dependencies - ${springboot.version} - pom - import - - - - - - - org.yaml - snakeyaml - ${snakeyaml.version} - - - - org.springframework.boot - spring-boot-starter-validation - org.springframework.boot spring-boot-starter-web @@ -64,20 +39,9 @@ - com.mysql - mysql-connector-j - runtime - - - org.projectlombok - lombok - true - provided - - - org.springframework.boot - spring-boot-starter-test - test + cn.wustlinghang.wusthelper + backend-data + ${revision} @@ -91,12 +55,9 @@ org.springframework.boot spring-boot-maven-plugin - - - org.projectlombok - lombok - - + + UTF-8 + diff --git a/backend-main/backend-web/src/main/java/cn/wustlinghang/main/web/BackendMain.java b/backend-main/backend-web/src/main/java/cn/wustlinghang/main/web/BackendMain.java deleted file mode 100644 index c428d72..0000000 --- a/backend-main/backend-web/src/main/java/cn/wustlinghang/main/web/BackendMain.java +++ /dev/null @@ -1,17 +0,0 @@ -package cn.wustlinghang.main.web; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.cloud.openfeign.EnableFeignClients; -import org.springframework.scheduling.annotation.EnableAsync; -import org.springframework.scheduling.annotation.EnableScheduling; - -@EnableAsync -@EnableScheduling -@EnableFeignClients -@SpringBootApplication -public class BackendMain { - public static void main(String[] args) { - SpringApplication.run(BackendMain.class); - } -} diff --git a/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/WebBackendMain.java b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/WebBackendMain.java new file mode 100644 index 0000000..9d08b80 --- /dev/null +++ b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/WebBackendMain.java @@ -0,0 +1,23 @@ +package cn.wustlinghang.wusthelper; + +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.annotation.EnableScheduling; + +@EnableAsync +@EnableScheduling +@EnableFeignClients(basePackages = {"cn.wustlinghang.wusthelper.web.rpc"}) +@SpringBootApplication(scanBasePackages = { + // 指定springboot的bean扫描路径,有新增请及时更新 + "cn.wustlinghang.wusthelper.data", + "cn.wustlinghang.wusthelper.web", +}) +@MapperScan("cn.wustlinghang.wusthelper.data.dao.mapper") +public class WebBackendMain { + public static void main(String[] args) { + SpringApplication.run(WebBackendMain.class); + } +} diff --git a/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/api/Health.java b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/api/Health.java new file mode 100644 index 0000000..fc811f2 --- /dev/null +++ b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/api/Health.java @@ -0,0 +1,13 @@ +package cn.wustlinghang.wusthelper.web.api; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +@RequestMapping("/") +public class Health { + @RequestMapping("/health") + public String health() { + return ""; + } +} diff --git a/backend-main/backend-web/src/main/java/cn/wustlinghang/main/web/api/v2/undergrade/UndergradController.java b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/api/v2/undergrade/UndergradController.java similarity index 77% rename from backend-main/backend-web/src/main/java/cn/wustlinghang/main/web/api/v2/undergrade/UndergradController.java rename to backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/api/v2/undergrade/UndergradController.java index 532da83..b8ab30c 100644 --- a/backend-main/backend-web/src/main/java/cn/wustlinghang/main/web/api/v2/undergrade/UndergradController.java +++ b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/api/v2/undergrade/UndergradController.java @@ -1,7 +1,7 @@ -package cn.wustlinghang.main.web.api.v2.undergrade; +package cn.wustlinghang.wusthelper.web.api.v2.undergrade; -import cn.wustlinghang.main.web.service.campus.undergrad.UndergradCookieService; -import cn.wustlinghang.wusthelper.main.response.Response; +import cn.wustlinghang.wusthelper.web.service.campus.undergrad.UndergradCookieService; +import cn.wustlinghang.wusthelper.web.response.Response; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; diff --git a/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/configure/CaffeineCacheConfigure.java b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/configure/CaffeineCacheConfigure.java new file mode 100644 index 0000000..a514e6c --- /dev/null +++ b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/configure/CaffeineCacheConfigure.java @@ -0,0 +1,34 @@ +package cn.wustlinghang.wusthelper.web.configure; + +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import org.springframework.cache.CacheManager; +import org.springframework.cache.caffeine.CaffeineCacheManager; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.concurrent.TimeUnit; + +@Configuration +public class CaffeineCacheConfigure { + @Bean + public CacheManager cacheManager() { + CaffeineCacheManager cacheManager = new CaffeineCacheManager(); + cacheManager.setCaffeine(Caffeine.newBuilder() + .expireAfterAccess(1, TimeUnit.HOURS) + .initialCapacity(100) + .maximumSize(1024) + ); + + return cacheManager; + } + + @Bean + public Cache cache() { + return Caffeine.newBuilder() + .expireAfterWrite(1, TimeUnit.HOURS) + .initialCapacity(100) + .maximumSize(1024) + .build(); + } +} diff --git a/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/entity/CookieType.java b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/entity/CookieType.java new file mode 100644 index 0000000..ea3889b --- /dev/null +++ b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/entity/CookieType.java @@ -0,0 +1,35 @@ +package cn.wustlinghang.wusthelper.web.entity; + +import java.util.StringJoiner; + +public enum CookieType { + UNDERGRAD(0, "本科生"), + GRADUATE(1, "研究生"), + LIBRARY(2, "图书馆"), + PHYSICS(3, "物理实验"),; + + private final int code; + + private final String message; + + CookieType(int id, String describe) { + this.code = id; + this.message = describe; + } + + public int getCode() { + return code; + } + + public String getMessage() { + return message; + } + + @Override + public String toString() { + return new StringJoiner(", ", CookieType.class.getSimpleName() + "[", "]") + .add("code=" + code) + .add("message='" + message + "'") + .toString(); + } +} \ No newline at end of file diff --git a/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/rpc/undergrad/CookieRemote.java b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/rpc/undergrad/CookieRemote.java new file mode 100644 index 0000000..9be83db --- /dev/null +++ b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/rpc/undergrad/CookieRemote.java @@ -0,0 +1,18 @@ +package cn.wustlinghang.wusthelper.web.rpc.undergrad; + +import cn.wustlinghang.wusthelper.internal.rpc.response.RpcResponseDto; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; + +@FeignClient(name = "wusthelper.undergrad", contextId = "cookie") +public interface CookieRemote { + String COOKIE_ROOT_PATH = "/cookie"; + + @GetMapping(COOKIE_ROOT_PATH) + RpcResponseDto login(@RequestParam("username") String username, + @RequestParam("password") String password); + + @GetMapping(COOKIE_ROOT_PATH + "/verify") + RpcResponseDto verify(@RequestParam("cookie") String cookie); +} diff --git a/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/rpc/undergrad/CourseTableRemote.java b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/rpc/undergrad/CourseTableRemote.java new file mode 100644 index 0000000..085529c --- /dev/null +++ b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/rpc/undergrad/CourseTableRemote.java @@ -0,0 +1,27 @@ +package cn.wustlinghang.wusthelper.web.rpc.undergrad; + +import cn.wustlinghang.mywust.data.global.Course; +import cn.wustlinghang.wusthelper.internal.rpc.response.RpcResponseDto; +import jakarta.validation.constraints.NotNull; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; + +import java.util.List; + +@FeignClient(name = "wusthelper.undergrad", contextId = "courseTable") +public interface CourseTableRemote { + String ROOT_PATH = "/course_table"; + + @GetMapping(ROOT_PATH) + RpcResponseDto> get(@RequestParam("cookie") String cookie, + @RequestParam("term") String term); + + @GetMapping(ROOT_PATH + "/agent") + RpcResponseDto agent(@RequestParam("cookie") @NotNull String cookie, + @RequestParam("term") String term); + + @PostMapping(ROOT_PATH + "/parse") + RpcResponseDto> parse(String html); +} diff --git a/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/rpc/undergrad/CreditStatusRemote.java b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/rpc/undergrad/CreditStatusRemote.java new file mode 100644 index 0000000..54cefb7 --- /dev/null +++ b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/rpc/undergrad/CreditStatusRemote.java @@ -0,0 +1,22 @@ +package cn.wustlinghang.wusthelper.web.rpc.undergrad; + +import cn.wustlinghang.wusthelper.internal.rpc.response.RpcResponseDto; +import jakarta.validation.constraints.NotNull; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; + +@FeignClient(name = "wusthelper.undergrad", contextId = "creditStatus") +public interface CreditStatusRemote { + String ROOT_PATH = "/credit_status"; + + @GetMapping(ROOT_PATH) + RpcResponseDto get(@RequestParam("cookie") String cookie); + + @GetMapping(ROOT_PATH + "/agent") + RpcResponseDto agent(@RequestParam("cookie") @NotNull String cookie); + + @PostMapping(ROOT_PATH + "/parse") + RpcResponseDto parse(String html); +} diff --git a/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/rpc/undergrad/ExamDelayApplicationRemote.java b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/rpc/undergrad/ExamDelayApplicationRemote.java new file mode 100644 index 0000000..4ca5bb6 --- /dev/null +++ b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/rpc/undergrad/ExamDelayApplicationRemote.java @@ -0,0 +1,27 @@ +package cn.wustlinghang.wusthelper.web.rpc.undergrad; + +import cn.wustlinghang.mywust.data.undergrad.ExamDelayApplication; +import cn.wustlinghang.wusthelper.internal.rpc.response.RpcResponseDto; +import jakarta.validation.constraints.NotNull; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; + +@FeignClient(name = "wusthelper.undergrad", contextId = "examDelayApplication") +public interface ExamDelayApplicationRemote { + String ROOT_PATH = "/exam_delay_application"; + + @GetMapping(ROOT_PATH) + RpcResponseDto get(@RequestParam("cookie") String cookie, + @RequestParam("term") String term, + @RequestParam("activity_id") String activityId); + + @GetMapping(ROOT_PATH + "/agent") + RpcResponseDto agent(@RequestParam("cookie") @NotNull String cookie, + @RequestParam("term") String term, + @RequestParam("activity_id") String activityId); + + @PostMapping(ROOT_PATH + "/parse") + RpcResponseDto parse(String html); +} diff --git a/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/rpc/undergrad/ScoreRemote.java b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/rpc/undergrad/ScoreRemote.java new file mode 100644 index 0000000..cb12a47 --- /dev/null +++ b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/rpc/undergrad/ScoreRemote.java @@ -0,0 +1,25 @@ +package cn.wustlinghang.wusthelper.web.rpc.undergrad; + +import cn.wustlinghang.mywust.data.global.Score; +import cn.wustlinghang.wusthelper.internal.rpc.response.RpcResponseDto; +import jakarta.validation.constraints.NotNull; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; + +import java.util.List; + +@FeignClient(name = "wusthelper.undergrad", contextId = "score") +public interface ScoreRemote { + String ROOT_PATH = "/score"; + + @GetMapping(ROOT_PATH) + RpcResponseDto> get(@RequestParam("cookie") String cookie); + + @GetMapping(ROOT_PATH + "/agent") + RpcResponseDto agent(@RequestParam("cookie") @NotNull String cookie); + + @PostMapping(ROOT_PATH + "/parse") + RpcResponseDto> parse(String html); +} diff --git a/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/rpc/undergrad/StudentInfoRemote.java b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/rpc/undergrad/StudentInfoRemote.java new file mode 100644 index 0000000..1f3f37f --- /dev/null +++ b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/rpc/undergrad/StudentInfoRemote.java @@ -0,0 +1,23 @@ +package cn.wustlinghang.wusthelper.web.rpc.undergrad; + +import cn.wustlinghang.mywust.data.global.StudentInfo; +import cn.wustlinghang.wusthelper.internal.rpc.response.RpcResponseDto; +import jakarta.validation.constraints.NotNull; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; + +@FeignClient(name = "wusthelper.undergrad", contextId = "studentInfo") +public interface StudentInfoRemote { + String ROOT_PATH = "/student_info"; + + @GetMapping(ROOT_PATH) + RpcResponseDto get(@RequestParam("cookie") String cookie); + + @GetMapping(ROOT_PATH + "/agent") + RpcResponseDto agent(@RequestParam("cookie") @NotNull String cookie); + + @PostMapping(ROOT_PATH + "/parse") + RpcResponseDto parse(String html); +} diff --git a/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/rpc/undergrad/TrainingPlanRemote.java b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/rpc/undergrad/TrainingPlanRemote.java new file mode 100644 index 0000000..661b9b0 --- /dev/null +++ b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/rpc/undergrad/TrainingPlanRemote.java @@ -0,0 +1,22 @@ +package cn.wustlinghang.wusthelper.web.rpc.undergrad; + +import cn.wustlinghang.wusthelper.internal.rpc.response.RpcResponseDto; +import jakarta.validation.constraints.NotNull; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; + +@FeignClient(name = "wusthelper.undergrad", contextId = "trainingPlan") +public interface TrainingPlanRemote { + String ROOT_PATH = "/training_plan"; + + @GetMapping(ROOT_PATH) + RpcResponseDto get(@RequestParam("cookie") String cookie); + + @GetMapping(ROOT_PATH + "/agent") + RpcResponseDto agent(@RequestParam("cookie") @NotNull String cookie); + + @PostMapping(ROOT_PATH + "/parse") + RpcResponseDto parse(String html); +} diff --git a/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/campus/undergrad/CourseTableService.java b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/campus/undergrad/CourseTableService.java new file mode 100644 index 0000000..3ddcea9 --- /dev/null +++ b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/campus/undergrad/CourseTableService.java @@ -0,0 +1,33 @@ +package cn.wustlinghang.wusthelper.web.service.campus.undergrad; + +import cn.wustlinghang.wusthelper.web.entity.CookieType; +import cn.wustlinghang.wusthelper.web.rpc.undergrad.CourseTableRemote; +import cn.wustlinghang.wusthelper.web.service.cookie.CookieManager; +import cn.wustlinghang.mywust.data.global.Course; +import cn.wustlinghang.wusthelper.internal.rpc.response.RpcCommonResponseCode; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class CourseTableService { + private final CourseTableRemote courseTableRemote; + + private final CookieManager cookieManager; + + public CourseTableService(CourseTableRemote courseTableRemote, CookieManager cookieManager) { + this.courseTableRemote = courseTableRemote; + this.cookieManager = cookieManager; + } + + public List getCourseTable(String user, String term) { + String cookie = cookieManager.getCookie(user, CookieType.UNDERGRAD); + var coursesResponse = courseTableRemote.get(cookie, term); + if (coursesResponse.code() != RpcCommonResponseCode.SUCCESS.getCode()) { + return null; + } + + // todo 异步存用户课表,异常判断等等 + return coursesResponse.data(); + } +} diff --git a/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/campus/undergrad/CreditStatusService.java b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/campus/undergrad/CreditStatusService.java new file mode 100644 index 0000000..5107b3a --- /dev/null +++ b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/campus/undergrad/CreditStatusService.java @@ -0,0 +1,29 @@ +package cn.wustlinghang.wusthelper.web.service.campus.undergrad; + +import cn.wustlinghang.wusthelper.web.entity.CookieType; +import cn.wustlinghang.wusthelper.web.rpc.undergrad.CreditStatusRemote; +import cn.wustlinghang.wusthelper.web.service.cookie.CookieManager; +import cn.wustlinghang.wusthelper.internal.rpc.response.RpcCommonResponseCode; +import org.springframework.stereotype.Service; + +@Service +public class CreditStatusService { + private final CreditStatusRemote creditStatusRemote; + private final CookieManager cookieManager; + + public CreditStatusService(CreditStatusRemote creditStatusRemote, CookieManager cookieManager) { + this.creditStatusRemote = creditStatusRemote; + this.cookieManager = cookieManager; + } + + public String getCreditStatus(String user) { + String cookie = cookieManager.getCookie(user, CookieType.UNDERGRAD); + var response = creditStatusRemote.get(cookie); + if (response.code() != RpcCommonResponseCode.SUCCESS.getCode()) { + return null; + } + + // todo 异步存用户课表,异常判断等等 + return response.data(); + } +} diff --git a/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/campus/undergrad/ExamDelayApplicationService.java b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/campus/undergrad/ExamDelayApplicationService.java new file mode 100644 index 0000000..e3777dd --- /dev/null +++ b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/campus/undergrad/ExamDelayApplicationService.java @@ -0,0 +1,31 @@ +package cn.wustlinghang.wusthelper.web.service.campus.undergrad; + +import cn.wustlinghang.wusthelper.web.rpc.undergrad.ExamDelayApplicationRemote; +import cn.wustlinghang.wusthelper.web.entity.CookieType; +import cn.wustlinghang.wusthelper.web.service.cookie.CookieManager; +import cn.wustlinghang.mywust.data.undergrad.ExamDelayApplication; +import cn.wustlinghang.wusthelper.internal.rpc.response.RpcCommonResponseCode; +import org.springframework.stereotype.Service; + +@Service +public class ExamDelayApplicationService { + private final ExamDelayApplicationRemote examDelayApplicationRemote; + private final CookieManager cookieManager; + + public ExamDelayApplicationService(ExamDelayApplicationRemote examDelayApplicationRemote, + CookieManager cookieManager) { + this.examDelayApplicationRemote = examDelayApplicationRemote; + this.cookieManager = cookieManager; + } + + public ExamDelayApplication getExamDelayApplication(String user, String term, String activityId) { + String cookie = cookieManager.getCookie(user, CookieType.UNDERGRAD); + var response = examDelayApplicationRemote.get(cookie, term, activityId); + if (response.code() != RpcCommonResponseCode.SUCCESS.getCode()) { + return null; + } + + // todo 异步存用户课表,异常判断等等 + return response.data(); + } +} diff --git a/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/campus/undergrad/ScoreService.java b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/campus/undergrad/ScoreService.java new file mode 100644 index 0000000..f40fdb6 --- /dev/null +++ b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/campus/undergrad/ScoreService.java @@ -0,0 +1,32 @@ +package cn.wustlinghang.wusthelper.web.service.campus.undergrad; + +import cn.wustlinghang.wusthelper.web.entity.CookieType; +import cn.wustlinghang.wusthelper.web.rpc.undergrad.ScoreRemote; +import cn.wustlinghang.wusthelper.web.service.cookie.CookieManager; +import cn.wustlinghang.mywust.data.global.Score; +import cn.wustlinghang.wusthelper.internal.rpc.response.RpcCommonResponseCode; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class ScoreService { + private final ScoreRemote scoreRemote; + private final CookieManager cookieManager; + + public ScoreService(ScoreRemote scoreRemote, CookieManager cookieManager) { + this.scoreRemote = scoreRemote; + this.cookieManager = cookieManager; + } + + public List getScore(String user) { + String cookie = cookieManager.getCookie(user, CookieType.UNDERGRAD); + var response = scoreRemote.get(cookie); + if (response.code() != RpcCommonResponseCode.SUCCESS.getCode()) { + return null; + } + + // todo 异步存用户课表,异常判断等等 + return response.data(); + } +} diff --git a/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/campus/undergrad/StudentInfoService.java b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/campus/undergrad/StudentInfoService.java new file mode 100644 index 0000000..b7d5bdb --- /dev/null +++ b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/campus/undergrad/StudentInfoService.java @@ -0,0 +1,30 @@ +package cn.wustlinghang.wusthelper.web.service.campus.undergrad; + +import cn.wustlinghang.wusthelper.web.rpc.undergrad.StudentInfoRemote; +import cn.wustlinghang.wusthelper.web.entity.CookieType; +import cn.wustlinghang.wusthelper.web.service.cookie.CookieManager; +import cn.wustlinghang.mywust.data.global.StudentInfo; +import cn.wustlinghang.wusthelper.internal.rpc.response.RpcCommonResponseCode; +import org.springframework.stereotype.Service; + +@Service +public class StudentInfoService { + private final StudentInfoRemote studentInfoRemote; + private final CookieManager cookieManager; + + public StudentInfoService(StudentInfoRemote studentInfoRemote, CookieManager cookieManager) { + this.studentInfoRemote = studentInfoRemote; + this.cookieManager = cookieManager; + } + + public StudentInfo getStudentInfo(String user) { + String cookie = cookieManager.getCookie(user, CookieType.UNDERGRAD); + var response = studentInfoRemote.get(cookie); + if (response.code() != RpcCommonResponseCode.SUCCESS.getCode()) { + return null; + } + + // todo 异步存用户课表,异常判断等等 + return response.data(); + } +} diff --git a/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/campus/undergrad/TrainingPlanService.java b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/campus/undergrad/TrainingPlanService.java new file mode 100644 index 0000000..80fe47c --- /dev/null +++ b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/campus/undergrad/TrainingPlanService.java @@ -0,0 +1,29 @@ +package cn.wustlinghang.wusthelper.web.service.campus.undergrad; + +import cn.wustlinghang.wusthelper.web.rpc.undergrad.TrainingPlanRemote; +import cn.wustlinghang.wusthelper.web.entity.CookieType; +import cn.wustlinghang.wusthelper.web.service.cookie.CookieManager; +import cn.wustlinghang.wusthelper.internal.rpc.response.RpcCommonResponseCode; +import org.springframework.stereotype.Service; + +@Service +public class TrainingPlanService { + private final TrainingPlanRemote trainingPlanRemote; + private final CookieManager cookieManager; + + public TrainingPlanService(TrainingPlanRemote trainingPlanRemote, CookieManager cookieManager) { + this.trainingPlanRemote = trainingPlanRemote; + this.cookieManager = cookieManager; + } + + public String getTrainingPlan(String user) { + String cookie = cookieManager.getCookie(user, CookieType.UNDERGRAD); + var response = trainingPlanRemote.get(cookie); + if (response.code() != RpcCommonResponseCode.SUCCESS.getCode()) { + return null; + } + + // todo 异步存用户课表,异常判断等等 + return response.data(); + } +} diff --git a/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/campus/undergrad/UndergradCookieService.java b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/campus/undergrad/UndergradCookieService.java new file mode 100644 index 0000000..ebaf2c2 --- /dev/null +++ b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/campus/undergrad/UndergradCookieService.java @@ -0,0 +1,21 @@ +package cn.wustlinghang.wusthelper.web.service.campus.undergrad; + +import cn.wustlinghang.wusthelper.web.rpc.undergrad.CookieRemote; +import org.springframework.stereotype.Service; + +@Service +public class UndergradCookieService { + private final CookieRemote cookieRemote; + + public UndergradCookieService(CookieRemote cookieRemote) { + this.cookieRemote = cookieRemote; + } + + public String getLoginCookie(String username, String password) { + return cookieRemote.login(username, password).data(); + } + + public Boolean checkCookie(String cookie) { + return cookieRemote.verify(cookie).data(); + } +} diff --git a/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/cookie/CookieManager.java b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/cookie/CookieManager.java new file mode 100644 index 0000000..79a40ab --- /dev/null +++ b/backend-main/backend-web/src/main/java/cn/wustlinghang/wusthelper/web/service/cookie/CookieManager.java @@ -0,0 +1,62 @@ +package cn.wustlinghang.wusthelper.web.service.cookie; + +import cn.wustlinghang.wusthelper.web.entity.CookieType; +import cn.wustlinghang.wusthelper.web.service.campus.undergrad.UndergradCookieService; +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Service; + +import java.util.concurrent.TimeUnit; + +@Service +@Scope("singleton") +public class CookieManager { + private static final Cache cookiePool = Caffeine.newBuilder() + .expireAfterWrite(2, TimeUnit.HOURS) + .initialCapacity(256) + .maximumSize(2048) + .build(); + + private final UndergradCookieService undergradCookieService; + + public CookieManager(UndergradCookieService undergradCookieService) { + this.undergradCookieService = undergradCookieService; + } + + public String getCookie(String username, CookieType cookieType) { + return getCookie(username, cookieType, false); + } + + public String getCookie(String username, CookieType cookieType, boolean forceRefresh) { + String cacheKey = String.format("%s:%d", username, cookieType.getCode()); + String cookie = cookiePool.getIfPresent(cacheKey); + + boolean valid = cookie != null && checkCookie(cookie, cookieType) && !forceRefresh; + if (valid) { + return cookie; + } + + cookie = this.refreshCookie(username, cookieType); + cookiePool.put(cacheKey, cookie); + + return cookie; + } + + public boolean checkCookie(String cookie, CookieType cookieType) { + return switch (cookieType) { + case UNDERGRAD -> undergradCookieService.checkCookie(cookie); + case GRADUATE -> undergradCookieService.checkCookie(cookie); + default -> false; + }; + } + + public String refreshCookie(String username, CookieType cookieType) { + String password = ""; + return switch (cookieType) { + case UNDERGRAD -> undergradCookieService.getLoginCookie(username, password); + case GRADUATE -> undergradCookieService.getLoginCookie(username, password); + default -> null; + }; + } +} \ No newline at end of file diff --git a/backend-main/backend-web/src/main/resources/application.yml b/backend-main/backend-web/src/main/resources/application.yml index 711216e..d9c6271 100644 --- a/backend-main/backend-web/src/main/resources/application.yml +++ b/backend-main/backend-web/src/main/resources/application.yml @@ -14,6 +14,11 @@ spring: port: 8500 discovery: register: false + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://localhost:3306/wust_helper?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghai + username: root + password: Test2333! server: port: ${RUN_PORT} diff --git a/backend-main/backend-web/src/test/java/cn/wustlinghang/wusthelper/StudentTest.java b/backend-main/backend-web/src/test/java/cn/wustlinghang/wusthelper/StudentTest.java new file mode 100644 index 0000000..c4cca6a --- /dev/null +++ b/backend-main/backend-web/src/test/java/cn/wustlinghang/wusthelper/StudentTest.java @@ -0,0 +1,26 @@ +package cn.wustlinghang.wusthelper; + +import cn.wustlinghang.wusthelper.data.dao.mapper.StudentMapper; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@Configuration +@ExtendWith(SpringExtension.class) +@SpringBootTest(classes = TestMain.class) +@TestPropertySource(properties = { + "spring.config.location=classpath:application-test.yml" +}) +public class StudentTest { + @Autowired + private StudentMapper studentMapper; + + @Test + public void testStudent() { + System.out.println(studentMapper.selectById(38)); + } +} diff --git a/backend-main/backend-web/src/test/java/cn/wustlinghang/wusthelper/TestMain.java b/backend-main/backend-web/src/test/java/cn/wustlinghang/wusthelper/TestMain.java new file mode 100644 index 0000000..8c45b48 --- /dev/null +++ b/backend-main/backend-web/src/test/java/cn/wustlinghang/wusthelper/TestMain.java @@ -0,0 +1,29 @@ +package cn.wustlinghang.wusthelper; + +import org.junit.jupiter.api.extension.ExtendWith; +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@EnableAsync +@Configuration +@EnableScheduling +@ExtendWith(SpringExtension.class) +@EnableFeignClients(basePackages = {"cn.wustlinghang.wusthelper.web.rpc"}) +@TestPropertySource(properties = { + "spring.config.location=classpath:application-test.yml" +}) +@SpringBootApplication(scanBasePackages = { + // 指定springboot的bean扫描路径,有新增请及时更新 + "cn.wustlinghang.wusthelper.data", + "cn.wustlinghang.wusthelper.web", +}) +@MapperScan("cn.wustlinghang.wusthelper.data.dao.mapper") +public class TestMain { +} diff --git a/backend-main/backend-web/src/test/resources/application-test.yml b/backend-main/backend-web/src/test/resources/application-test.yml new file mode 100644 index 0000000..a1251e3 --- /dev/null +++ b/backend-main/backend-web/src/test/resources/application-test.yml @@ -0,0 +1,24 @@ +# 服务端配置 +# 密码/密钥/内部地址『禁止』写在此处或其他git能检测到的地方 +# 具体数值由运维填写在.env文件中,不能添加到git仓库中 + +spring: + config: + import: optional:file:.env[.properties] + application: + name: wusthelper-backend-main + + cloud: + consul: + host: 127.0.0.1 + port: 8500 + discovery: + register: false + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://localhost:3306/wust_helper?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghai + username: root + password: Test2333! + +server: + port: 10000 diff --git a/backend-main/pom.xml b/backend-main/pom.xml index eb00bd4..2a2dc86 100644 --- a/backend-main/pom.xml +++ b/backend-main/pom.xml @@ -23,6 +23,10 @@ UTF-8 3.1.6 + + 3.1.1 + 2.0 + 5.10.0-RC1 @@ -43,5 +47,41 @@ mywust-common ${mywust.version} + + + + org.yaml + snakeyaml + ${snakeyaml.version} + + + org.springframework.boot + spring-boot-starter-validation + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + + + + + + org.springframework.boot + spring-boot-dependencies + ${springboot.version} + pom + import + + + + \ No newline at end of file diff --git a/common/src/main/java/cn/wustlinghang/wusthelper/internal/rpc/response/RpcResponse.java b/common/src/main/java/cn/wustlinghang/wusthelper/internal/rpc/response/RpcResponse.java deleted file mode 100644 index d1406e9..0000000 --- a/common/src/main/java/cn/wustlinghang/wusthelper/internal/rpc/response/RpcResponse.java +++ /dev/null @@ -1,19 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.rpc.response; - -public record RpcResponse(int code, String msg, T data) { - public static RpcResponse success(T data) { - return new RpcResponse<>(RpcCommonResponseCode.SUCCESS.getCode(), "ok", data); - } - - public static RpcResponse success() { - return success(null); - } - - public static RpcResponse error(int code, String message) { - return new RpcResponse<>(code, message, null); - } - - public static RpcResponse error(RpcCommonResponseCode code) { - return error(code.getCode(), code.getMessage()); - } -} \ No newline at end of file diff --git a/common/src/main/java/cn/wustlinghang/wusthelper/internal/rpc/response/RpcResponseDto.java b/common/src/main/java/cn/wustlinghang/wusthelper/internal/rpc/response/RpcResponseDto.java new file mode 100644 index 0000000..f5bb2d7 --- /dev/null +++ b/common/src/main/java/cn/wustlinghang/wusthelper/internal/rpc/response/RpcResponseDto.java @@ -0,0 +1,19 @@ +package cn.wustlinghang.wusthelper.internal.rpc.response; + +public record RpcResponseDto(int code, String msg, T data) { + public static RpcResponseDto success(T data) { + return new RpcResponseDto<>(RpcCommonResponseCode.SUCCESS.getCode(), "ok", data); + } + + public static RpcResponseDto success() { + return success(null); + } + + public static RpcResponseDto error(int code, String message) { + return new RpcResponseDto<>(code, message, null); + } + + public static RpcResponseDto error(RpcCommonResponseCode code) { + return error(code.getCode(), code.getMessage()); + } +} \ No newline at end of file diff --git a/common/src/main/java/cn/wustlinghang/wusthelper/main/response/Response.java b/common/src/main/java/cn/wustlinghang/wusthelper/web/response/Response.java similarity index 92% rename from common/src/main/java/cn/wustlinghang/wusthelper/main/response/Response.java rename to common/src/main/java/cn/wustlinghang/wusthelper/web/response/Response.java index 2518116..74bb514 100644 --- a/common/src/main/java/cn/wustlinghang/wusthelper/main/response/Response.java +++ b/common/src/main/java/cn/wustlinghang/wusthelper/web/response/Response.java @@ -1,4 +1,4 @@ -package cn.wustlinghang.wusthelper.main.response; +package cn.wustlinghang.wusthelper.web.response; /** diff --git a/common/src/main/java/cn/wustlinghang/wusthelper/main/response/ResponseCode.java b/common/src/main/java/cn/wustlinghang/wusthelper/web/response/ResponseCode.java similarity index 90% rename from common/src/main/java/cn/wustlinghang/wusthelper/main/response/ResponseCode.java rename to common/src/main/java/cn/wustlinghang/wusthelper/web/response/ResponseCode.java index 9ad8f89..a9fa7f2 100644 --- a/common/src/main/java/cn/wustlinghang/wusthelper/main/response/ResponseCode.java +++ b/common/src/main/java/cn/wustlinghang/wusthelper/web/response/ResponseCode.java @@ -2,7 +2,7 @@ * Class created by lensfrex. */ -package cn.wustlinghang.wusthelper.main.response; +package cn.wustlinghang.wusthelper.web.response; public enum ResponseCode { SUCCESS(0, "成功"), diff --git a/external-library/rpc-frp-consul/.flattened-pom.xml b/external-library/rpc-frp-consul/.flattened-pom.xml new file mode 100644 index 0000000..e7fe799 --- /dev/null +++ b/external-library/rpc-frp-consul/.flattened-pom.xml @@ -0,0 +1,59 @@ + + + 4.0.0 + cn.wustlinghang.wusthelper + rpc-frp-consul + 0.0.1-SNAPSHOT + + + com.fasterxml.jackson.core + jackson-databind + 2.15.2 + compile + + + cn.wustlinghang.mywust + mywust-network-okhttp + 0.0.2-beta + compile + + + org.ini4j + ini4j + 0.5.4 + compile + + + jakarta.annotation + jakarta.annotation-api + 2.1.1 + compile + + + javax.annotation + javax.annotation-api + 1.3.2 + compile + + + org.projectlombok + lombok + 1.18.26 + provided + + + + + central + https://repo1.maven.org/maven2 + + + + true + + github + https://maven.pkg.github.com/LingHangStudio/mywust + + + diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/http/v1/handler/BaseExceptionHandler.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/http/v1/handler/BaseExceptionHandler.java index 66fc0ce..d6e1948 100644 --- a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/http/v1/handler/BaseExceptionHandler.java +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/http/v1/handler/BaseExceptionHandler.java @@ -1,7 +1,7 @@ package cn.wustlinghang.wusthelper.internal.graduate.api.http.v1.handler; import cn.wustlinghang.wusthelper.internal.rpc.response.RpcCommonResponseCode; -import cn.wustlinghang.wusthelper.internal.rpc.response.RpcResponse; +import cn.wustlinghang.wusthelper.internal.rpc.response.RpcResponseDto; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.ws.rs.core.MediaType; @@ -27,9 +27,9 @@ public abstract class BaseExceptionHandler { public Response toResponse(Response.Status status, int code, String msg, String handlerName) { Object response; try { - response = objectMapper.writeValueAsString(RpcResponse.error(code, msg)); + response = objectMapper.writeValueAsString(RpcResponseDto.error(code, msg)); } catch (JsonProcessingException e) { - response = RpcResponse.error(RpcCommonResponseCode.SERVER_INTERNAL_ERROR); + response = RpcResponseDto.error(RpcCommonResponseCode.SERVER_INTERNAL_ERROR); } return Response.status(status) diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/http/v1/interceptor/ResponseWrapperInterceptor.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/http/v1/interceptor/ResponseWrapperInterceptor.java index 3898001..0a96082 100644 --- a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/http/v1/interceptor/ResponseWrapperInterceptor.java +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/http/v1/interceptor/ResponseWrapperInterceptor.java @@ -1,7 +1,7 @@ package cn.wustlinghang.wusthelper.internal.graduate.api.http.v1.interceptor; import cn.wustlinghang.wusthelper.internal.graduate.api.http.v1.handler.BaseExceptionHandler; -import cn.wustlinghang.wusthelper.internal.rpc.response.RpcResponse; +import cn.wustlinghang.wusthelper.internal.rpc.response.RpcResponseDto; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.ws.rs.WebApplicationException; import jakarta.ws.rs.core.HttpHeaders; @@ -25,7 +25,7 @@ public class ResponseWrapperInterceptor implements WriterInterceptor { boolean hasException = context.getHeaders().get(BaseExceptionHandler.EXCEPTION_HEADER_KEY) != null; if (!hasException) { Object data = context.getEntity(); - RpcResponse wrappedResponse = RpcResponse.success(data); + RpcResponseDto wrappedResponse = RpcResponseDto.success(data); String json = objectMapper.writeValueAsString(wrappedResponse); context.getHeaders().putSingle(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); context.setEntity(json); diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/rpc/Register.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/rpc/Register.java new file mode 100644 index 0000000..85924d1 --- /dev/null +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/rpc/Register.java @@ -0,0 +1,40 @@ +package cn.wustlinghang.wusthelper.internal.graduate.rpc; + +import cn.wustlinghang.mywust.network.Requester; +import cn.wustlinghang.wusthelper.internal.graduate.rpc.config.RpcConfig; +import cn.wustlinghang.wusthelper.internal.rpc.FrpConsulRegister; +import cn.wustlinghang.wusthelper.internal.rpc.config.FrpConfig; +import cn.wustlinghang.wusthelper.internal.rpc.config.RegisterConfig; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.quarkus.runtime.Startup; +import jakarta.annotation.PostConstruct; +import jakarta.enterprise.context.ApplicationScoped; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@Startup +@ApplicationScoped +public class Register { + + private final FrpConsulRegister register; + + public Register(RpcConfig rpcConfig, Requester requester, ObjectMapper objectMapper) { + FrpConfig frpConfig = FrpConfig.builder() + .frpcAdminAddress(rpcConfig.getFrpcAdminAddress()) + .frpcAdminUsername(rpcConfig.getFrpcAdminUsername()) + .frpcAdminPassword(rpcConfig.getFrpcAdminPassword()) + .build(); + RegisterConfig registerConfig = RegisterConfig.builder() + .consulAddress(rpcConfig.getConsulAddress()) + .localServicePort(rpcConfig.getLocalServicePort()) + .serviceName(rpcConfig.getServiceName()) + .build(); + + this.register = new FrpConsulRegister(registerConfig, frpConfig, requester, objectMapper); + } + + @PostConstruct + public void onStartup() { + register.register(); + } +} diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/rpc/config/RpcConfig.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/rpc/config/RpcConfig.java new file mode 100644 index 0000000..fe61fd3 --- /dev/null +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/rpc/config/RpcConfig.java @@ -0,0 +1,33 @@ +package cn.wustlinghang.wusthelper.internal.graduate.rpc.config; + +import jakarta.inject.Singleton; +import jakarta.ws.rs.DefaultValue; +import lombok.Data; +import org.eclipse.microprofile.config.inject.ConfigProperty; + +@Data +@Singleton +public class RpcConfig { + @DefaultValue("default.service") + @ConfigProperty(name = "rpc.service.name") + String serviceName; + + @DefaultValue("http://127.0.0.1:8500") + @ConfigProperty(name = "rpc.register.consul-api.address") + String consulAddress; + + @DefaultValue("http://127.0.0.1:7400") + @ConfigProperty(name = "rpc.frpc.admin-api.address") + String frpcAdminAddress; + + @DefaultValue("") + @ConfigProperty(name = "rpc.frpc.admin.username") + String frpcAdminUsername; + + @DefaultValue("") + @ConfigProperty(name = "rpc.frpc.admin.password") + String frpcAdminPassword; + + @ConfigProperty(name = "quarkus.http.port") + String localServicePort; +} diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/rpc/health/HealthCheck.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/rpc/health/HealthCheck.java new file mode 100644 index 0000000..4c2e8b9 --- /dev/null +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/rpc/health/HealthCheck.java @@ -0,0 +1,14 @@ +package cn.wustlinghang.wusthelper.internal.graduate.rpc.health; + +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.core.Response; + +@Path("/health") +public class HealthCheck { + private static final Response successResponse = Response.accepted().status(200).build(); + + @GET + @Path("/") + public Response response() {return successResponse;} +} diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/http/v1/handler/BaseExceptionHandler.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/http/v1/handler/BaseExceptionHandler.java index f5aeb27..8512daa 100644 --- a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/http/v1/handler/BaseExceptionHandler.java +++ b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/http/v1/handler/BaseExceptionHandler.java @@ -1,7 +1,7 @@ package cn.wustlinghang.wusthelper.internal.library.api.http.v1.handler; import cn.wustlinghang.wusthelper.internal.rpc.response.RpcCommonResponseCode; -import cn.wustlinghang.wusthelper.internal.rpc.response.RpcResponse; +import cn.wustlinghang.wusthelper.internal.rpc.response.RpcResponseDto; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.ws.rs.core.MediaType; @@ -27,9 +27,9 @@ public abstract class BaseExceptionHandler { public Response toResponse(Response.Status status, int code, String msg, String handlerName) { Object response; try { - response = objectMapper.writeValueAsString(RpcResponse.error(code, msg)); + response = objectMapper.writeValueAsString(RpcResponseDto.error(code, msg)); } catch (JsonProcessingException e) { - response = RpcResponse.error(RpcCommonResponseCode.SERVER_INTERNAL_ERROR); + response = RpcResponseDto.error(RpcCommonResponseCode.SERVER_INTERNAL_ERROR); } return Response.status(status) diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/http/v1/interceptor/ResponseWrapperInterceptor.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/http/v1/interceptor/ResponseWrapperInterceptor.java index 5776a51..8386491 100644 --- a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/http/v1/interceptor/ResponseWrapperInterceptor.java +++ b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/http/v1/interceptor/ResponseWrapperInterceptor.java @@ -1,7 +1,7 @@ package cn.wustlinghang.wusthelper.internal.library.api.http.v1.interceptor; import cn.wustlinghang.wusthelper.internal.library.api.http.v1.handler.BaseExceptionHandler; -import cn.wustlinghang.wusthelper.internal.rpc.response.RpcResponse; +import cn.wustlinghang.wusthelper.internal.rpc.response.RpcResponseDto; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.ws.rs.WebApplicationException; import jakarta.ws.rs.core.HttpHeaders; @@ -25,7 +25,7 @@ public class ResponseWrapperInterceptor implements WriterInterceptor { boolean hasException = context.getHeaders().get(BaseExceptionHandler.EXCEPTION_HEADER_KEY) != null; if (!hasException) { Object data = context.getEntity(); - RpcResponse wrappedResponse = RpcResponse.success(data); + RpcResponseDto wrappedResponse = RpcResponseDto.success(data); String json = objectMapper.writeValueAsString(wrappedResponse); context.getHeaders().putSingle(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); context.setEntity(json); diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/rpc/Register.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/rpc/Register.java new file mode 100644 index 0000000..e16c456 --- /dev/null +++ b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/rpc/Register.java @@ -0,0 +1,40 @@ +package cn.wustlinghang.wusthelper.internal.library.rpc; + +import cn.wustlinghang.mywust.network.Requester; +import cn.wustlinghang.wusthelper.internal.library.rpc.config.RpcConfig; +import cn.wustlinghang.wusthelper.internal.rpc.FrpConsulRegister; +import cn.wustlinghang.wusthelper.internal.rpc.config.FrpConfig; +import cn.wustlinghang.wusthelper.internal.rpc.config.RegisterConfig; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.quarkus.runtime.Startup; +import jakarta.annotation.PostConstruct; +import jakarta.enterprise.context.ApplicationScoped; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@Startup +@ApplicationScoped +public class Register { + + private final FrpConsulRegister register; + + public Register(RpcConfig rpcConfig, Requester requester, ObjectMapper objectMapper) { + FrpConfig frpConfig = FrpConfig.builder() + .frpcAdminAddress(rpcConfig.getFrpcAdminAddress()) + .frpcAdminUsername(rpcConfig.getFrpcAdminUsername()) + .frpcAdminPassword(rpcConfig.getFrpcAdminPassword()) + .build(); + RegisterConfig registerConfig = RegisterConfig.builder() + .consulAddress(rpcConfig.getConsulAddress()) + .localServicePort(rpcConfig.getLocalServicePort()) + .serviceName(rpcConfig.getServiceName()) + .build(); + + this.register = new FrpConsulRegister(registerConfig, frpConfig, requester, objectMapper); + } + + @PostConstruct + public void onStartup() { + register.register(); + } +} diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/rpc/config/RpcConfig.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/rpc/config/RpcConfig.java new file mode 100644 index 0000000..6a74767 --- /dev/null +++ b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/rpc/config/RpcConfig.java @@ -0,0 +1,33 @@ +package cn.wustlinghang.wusthelper.internal.library.rpc.config; + +import jakarta.inject.Singleton; +import jakarta.ws.rs.DefaultValue; +import lombok.Data; +import org.eclipse.microprofile.config.inject.ConfigProperty; + +@Data +@Singleton +public class RpcConfig { + @DefaultValue("default.service") + @ConfigProperty(name = "rpc.service.name") + String serviceName; + + @DefaultValue("http://127.0.0.1:8500") + @ConfigProperty(name = "rpc.register.consul-api.address") + String consulAddress; + + @DefaultValue("http://127.0.0.1:7400") + @ConfigProperty(name = "rpc.frpc.admin-api.address") + String frpcAdminAddress; + + @DefaultValue("") + @ConfigProperty(name = "rpc.frpc.admin.username") + String frpcAdminUsername; + + @DefaultValue("") + @ConfigProperty(name = "rpc.frpc.admin.password") + String frpcAdminPassword; + + @ConfigProperty(name = "quarkus.http.port") + String localServicePort; +} diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/rpc/health/HealthCheck.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/rpc/health/HealthCheck.java new file mode 100644 index 0000000..c981f40 --- /dev/null +++ b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/rpc/health/HealthCheck.java @@ -0,0 +1,14 @@ +package cn.wustlinghang.wusthelper.internal.library.rpc.health; + +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.core.Response; + +@Path("/health") +public class HealthCheck { + private static final Response successResponse = Response.accepted().status(200).build(); + + @GET + @Path("/") + public Response response() {return successResponse;} +} diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/http/v1/handler/BaseExceptionHandler.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/http/v1/handler/BaseExceptionHandler.java index 8fc6aab..1fd3a96 100644 --- a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/http/v1/handler/BaseExceptionHandler.java +++ b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/http/v1/handler/BaseExceptionHandler.java @@ -1,7 +1,7 @@ package cn.wustlinghang.wusthelper.internal.physics.api.http.v1.handler; import cn.wustlinghang.wusthelper.internal.rpc.response.RpcCommonResponseCode; -import cn.wustlinghang.wusthelper.internal.rpc.response.RpcResponse; +import cn.wustlinghang.wusthelper.internal.rpc.response.RpcResponseDto; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.ws.rs.core.MediaType; @@ -27,9 +27,9 @@ public abstract class BaseExceptionHandler { public Response toResponse(Response.Status status, int code, String msg, String handlerName) { Object response; try { - response = objectMapper.writeValueAsString(RpcResponse.error(code, msg)); + response = objectMapper.writeValueAsString(RpcResponseDto.error(code, msg)); } catch (JsonProcessingException e) { - response = RpcResponse.error(RpcCommonResponseCode.SERVER_INTERNAL_ERROR); + response = RpcResponseDto.error(RpcCommonResponseCode.SERVER_INTERNAL_ERROR); } return Response.status(status) diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/http/v1/interceptor/ResponseWrapperInterceptor.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/http/v1/interceptor/ResponseWrapperInterceptor.java index d774eeb..ace0416 100644 --- a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/http/v1/interceptor/ResponseWrapperInterceptor.java +++ b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/http/v1/interceptor/ResponseWrapperInterceptor.java @@ -1,7 +1,7 @@ package cn.wustlinghang.wusthelper.internal.physics.api.http.v1.interceptor; import cn.wustlinghang.wusthelper.internal.physics.api.http.v1.handler.BaseExceptionHandler; -import cn.wustlinghang.wusthelper.internal.rpc.response.RpcResponse; +import cn.wustlinghang.wusthelper.internal.rpc.response.RpcResponseDto; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.ws.rs.WebApplicationException; import jakarta.ws.rs.core.HttpHeaders; @@ -25,7 +25,7 @@ public class ResponseWrapperInterceptor implements WriterInterceptor { boolean hasException = context.getHeaders().get(BaseExceptionHandler.EXCEPTION_HEADER_KEY) != null; if (!hasException) { Object data = context.getEntity(); - RpcResponse wrappedResponse = RpcResponse.success(data); + RpcResponseDto wrappedResponse = RpcResponseDto.success(data); String json = objectMapper.writeValueAsString(wrappedResponse); context.getHeaders().putSingle(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); context.setEntity(json); diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/rpc/Register.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/rpc/Register.java new file mode 100644 index 0000000..5ec5286 --- /dev/null +++ b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/rpc/Register.java @@ -0,0 +1,40 @@ +package cn.wustlinghang.wusthelper.internal.physics.rpc; + +import cn.wustlinghang.mywust.network.Requester; +import cn.wustlinghang.wusthelper.internal.physics.rpc.config.RpcConfig; +import cn.wustlinghang.wusthelper.internal.rpc.FrpConsulRegister; +import cn.wustlinghang.wusthelper.internal.rpc.config.FrpConfig; +import cn.wustlinghang.wusthelper.internal.rpc.config.RegisterConfig; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.quarkus.runtime.Startup; +import jakarta.annotation.PostConstruct; +import jakarta.enterprise.context.ApplicationScoped; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@Startup +@ApplicationScoped +public class Register { + + private final FrpConsulRegister register; + + public Register(RpcConfig rpcConfig, Requester requester, ObjectMapper objectMapper) { + FrpConfig frpConfig = FrpConfig.builder() + .frpcAdminAddress(rpcConfig.getFrpcAdminAddress()) + .frpcAdminUsername(rpcConfig.getFrpcAdminUsername()) + .frpcAdminPassword(rpcConfig.getFrpcAdminPassword()) + .build(); + RegisterConfig registerConfig = RegisterConfig.builder() + .consulAddress(rpcConfig.getConsulAddress()) + .localServicePort(rpcConfig.getLocalServicePort()) + .serviceName(rpcConfig.getServiceName()) + .build(); + + this.register = new FrpConsulRegister(registerConfig, frpConfig, requester, objectMapper); + } + + @PostConstruct + public void onStartup() { + register.register(); + } +} diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/rpc/config/RpcConfig.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/rpc/config/RpcConfig.java new file mode 100644 index 0000000..17f085b --- /dev/null +++ b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/rpc/config/RpcConfig.java @@ -0,0 +1,33 @@ +package cn.wustlinghang.wusthelper.internal.physics.rpc.config; + +import jakarta.inject.Singleton; +import jakarta.ws.rs.DefaultValue; +import lombok.Data; +import org.eclipse.microprofile.config.inject.ConfigProperty; + +@Data +@Singleton +public class RpcConfig { + @DefaultValue("default.service") + @ConfigProperty(name = "rpc.service.name") + String serviceName; + + @DefaultValue("http://127.0.0.1:8500") + @ConfigProperty(name = "rpc.register.consul-api.address") + String consulAddress; + + @DefaultValue("http://127.0.0.1:7400") + @ConfigProperty(name = "rpc.frpc.admin-api.address") + String frpcAdminAddress; + + @DefaultValue("") + @ConfigProperty(name = "rpc.frpc.admin.username") + String frpcAdminUsername; + + @DefaultValue("") + @ConfigProperty(name = "rpc.frpc.admin.password") + String frpcAdminPassword; + + @ConfigProperty(name = "quarkus.http.port") + String localServicePort; +} diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/rpc/health/HealthCheck.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/rpc/health/HealthCheck.java new file mode 100644 index 0000000..75f2e5f --- /dev/null +++ b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/rpc/health/HealthCheck.java @@ -0,0 +1,14 @@ +package cn.wustlinghang.wusthelper.internal.physics.rpc.health; + +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.core.Response; + +@Path("/health") +public class HealthCheck { + private static final Response successResponse = Response.accepted().status(200).build(); + + @GET + @Path("/") + public Response response() {return successResponse;} +} diff --git a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/http/v1/handler/BaseExceptionHandler.java b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/http/v1/handler/BaseExceptionHandler.java index a77f71f..250bde6 100644 --- a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/http/v1/handler/BaseExceptionHandler.java +++ b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/http/v1/handler/BaseExceptionHandler.java @@ -1,7 +1,7 @@ package cn.wustlinghang.wusthelper.internal.undergrad.api.http.v1.handler; import cn.wustlinghang.wusthelper.internal.rpc.response.RpcCommonResponseCode; -import cn.wustlinghang.wusthelper.internal.rpc.response.RpcResponse; +import cn.wustlinghang.wusthelper.internal.rpc.response.RpcResponseDto; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.ws.rs.core.MediaType; @@ -27,9 +27,9 @@ public abstract class BaseExceptionHandler { public Response toResponse(Response.Status status, int code, String msg, String handlerName) { Object response; try { - response = objectMapper.writeValueAsString(RpcResponse.error(code, msg)); + response = objectMapper.writeValueAsString(RpcResponseDto.error(code, msg)); } catch (JsonProcessingException e) { - response = RpcResponse.error(RpcCommonResponseCode.SERVER_INTERNAL_ERROR); + response = RpcResponseDto.error(RpcCommonResponseCode.SERVER_INTERNAL_ERROR); } return Response.status(status) diff --git a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/http/v1/interceptor/ResponseWrapperInterceptor.java b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/http/v1/interceptor/ResponseWrapperInterceptor.java index 05f60d0..4c46e22 100644 --- a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/http/v1/interceptor/ResponseWrapperInterceptor.java +++ b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/http/v1/interceptor/ResponseWrapperInterceptor.java @@ -1,7 +1,7 @@ package cn.wustlinghang.wusthelper.internal.undergrad.api.http.v1.interceptor; import cn.wustlinghang.wusthelper.internal.undergrad.api.http.v1.handler.BaseExceptionHandler; -import cn.wustlinghang.wusthelper.internal.rpc.response.RpcResponse; +import cn.wustlinghang.wusthelper.internal.rpc.response.RpcResponseDto; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.ws.rs.WebApplicationException; import jakarta.ws.rs.core.HttpHeaders; @@ -25,7 +25,7 @@ public class ResponseWrapperInterceptor implements WriterInterceptor { boolean hasException = context.getHeaders().get(BaseExceptionHandler.EXCEPTION_HEADER_KEY) != null; if (!hasException) { Object data = context.getEntity(); - RpcResponse wrappedResponse = RpcResponse.success(data); + RpcResponseDto wrappedResponse = RpcResponseDto.success(data); String json = objectMapper.writeValueAsString(wrappedResponse); context.getHeaders().putSingle(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); context.setEntity(json);