@rdmind/rdmind 0.1.8 → 0.1.9-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/cli.js +284 -77
  2. package/locales/en.js +3 -1
  3. package/locales/zh.js +3 -0
  4. package/package.json +2 -2
  5. package/template/pom.xml +40 -0
  6. package/template/sns-demo-app/pom.xml +0 -5
  7. package/template/sns-demo-common/pom.xml +4 -5
  8. package/template/sns-demo-common/src/main/java/com/xiaohongshu/sns/demo/common/constants/AppPlatform.java +57 -0
  9. package/template/sns-demo-common/src/main/java/com/xiaohongshu/sns/demo/common/enums/ErrorCodeEnum.java +30 -0
  10. package/template/sns-demo-common/src/main/java/com/xiaohongshu/sns/demo/common/exception/AppBizException.java +51 -0
  11. package/template/sns-demo-common/src/main/java/com/xiaohongshu/sns/demo/common/utils/AssertUtils.java +137 -0
  12. package/template/sns-demo-common/src/main/java/com/xiaohongshu/sns/demo/common/utils/JsonHelper.java +129 -0
  13. package/template/sns-demo-common/src/main/java/com/xiaohongshu/sns/demo/common/utils/WrappedContextHelper.java +182 -0
  14. package/template/sns-demo-domain/pom.xml +0 -5
  15. package/template/sns-demo-infrastructure/pom.xml +0 -5
  16. package/template/sns-demo-start/pom.xml +0 -5
  17. package/template/sns-demo-start/src/main/resources/logback-spring.xml +16 -19
  18. package/templates/idl-template/wiki/.idea/codeStyles/Project.xml +7 -0
  19. package/templates/idl-template/wiki/.idea/codeStyles/codeStyleConfig.xml +5 -0
  20. package/templates/idl-template/wiki/.idea/inspectionProfiles/Project_Default.xml +5 -0
  21. package/templates/idl-template/wiki/.idea/misc.xml +14 -0
  22. package/templates/idl-template/wiki/.idea/modules.xml +8 -0
  23. package/templates/idl-template/wiki/.idea/vcs.xml +6 -0
  24. package/templates/idl-template/wiki/.idea/wiki.iml +9 -0
  25. package/templates/idl-template/wiki/SDK-Dev-Guide.md +0 -2
  26. package/templates/idl-template/wiki/example/.gitlab-ci.yml +3 -17
  27. package/templates/idl-template/wiki/example/base.thrift +94 -0
  28. package/templates/idl-template/wiki/example/common.thrift +39 -0
  29. package/templates/idl-template/wiki/example/dto.thrift +3 -0
  30. package/templates/idl-template/wiki/example/enum.thrift +1 -0
  31. package/templates/idl-template/wiki/example/gen-java.sh +5 -2
  32. package/templates/idl-template/wiki/example/maven_project/pom.xml +102 -83
  33. package/templates/idl-template/wiki/example/req.thrift +4 -0
  34. package/templates/idl-template/wiki/example/res.thrift +5 -0
  35. package/templates/idl-template/wiki/example/sdk-spec.yml +4 -3
  36. package/templates/idl-template/wiki/example/service.thrift +15 -0
  37. package/template/sns-demo-common/src/main/java/com/xiaohongshu/sns/demo/common/enums/.gitkeep +0 -0
  38. package/templates/idl-template/wiki/.arcconfig +0 -3
  39. package/templates/idl-template/wiki/example/.arcconfig +0 -3
  40. package/templates/idl-template/wiki/example/hello.thrift +0 -29
  41. package/templates/idl-template/wiki/example/maven_project/src/main/java/com/xiaohongshu/sns/thrift/hello/HelloServiceAutoConfiguration.java +0 -46
  42. package/templates/idl-template/wiki/example/maven_project/src/main/java/com/xiaohongshu/sns/thrift/hello/HelloServiceProperties.java +0 -18
  43. package/templates/idl-template/wiki/example/maven_project/src/main/resources/META-INF/spring.factories +0 -3
  44. package/templates/idl-template/wiki/example/maven_project/src/test/java/com/xiaohongshu/sns/thrift/test/AutoConfigureTest.java +0 -53
@@ -0,0 +1,182 @@
1
+ package com.xiaohongshu.sns.demo.common.utils;
2
+
3
+ import com.fasterxml.jackson.annotation.JsonProperty;
4
+ import com.xiaohongshu.infra.apm.utils.ContextHelper;
5
+ import com.xiaohongshu.infra.rpc.base.Context;
6
+
7
+ import com.xiaohongshu.sns.demo.common.constants.AppPlatform;
8
+ import lombok.Data;
9
+ import lombok.extern.slf4j.Slf4j;
10
+ import org.apache.commons.lang3.ObjectUtils;
11
+ import org.apache.commons.lang3.StringUtils;
12
+ import org.apache.commons.lang3.math.NumberUtils;
13
+ import org.springframework.stereotype.Component;
14
+ import org.springframework.util.CollectionUtils;
15
+
16
+ import java.net.URLDecoder;
17
+ import java.util.Collections;
18
+ import java.util.HashMap;
19
+ import java.util.Map;
20
+ import java.util.Optional;
21
+
22
+ /**
23
+ * 对中间件 ContextHelper 的封装,便于拿到当前的请求信息。
24
+ * 整合了 ContextUtils 现有功能及增强的上下文解析能力。
25
+ *
26
+ * @author RDMind
27
+ */
28
+ @Component
29
+ @Slf4j
30
+ public class WrappedContextHelper {
31
+
32
+ public static final String APP_CONTEXT_PREFIX = "__app_context:";
33
+ public static final String HTTP_HEADER = "httpHeader";
34
+
35
+ /**
36
+ * 获取原始 Context
37
+ */
38
+ public static Context get() {
39
+ return ContextHelper.getContext();
40
+ }
41
+
42
+ /**
43
+ * 获取当前用户 ID
44
+ */
45
+ public static String getUserId() {
46
+ Context context = get();
47
+ if (context == null) {
48
+ return StringUtils.EMPTY;
49
+ }
50
+ String userId = context.getUserID();
51
+ if (StringUtils.isBlank(userId)) {
52
+ Map<String, String> baggageMap = context.getBaggage();
53
+ if (!CollectionUtils.isEmpty(baggageMap)) {
54
+ userId = baggageMap.get("viewerUserId");
55
+ }
56
+ }
57
+ return StringUtils.isBlank(userId) ? StringUtils.EMPTY : userId;
58
+ }
59
+
60
+ /**
61
+ * 获取当前操作者邮箱
62
+ */
63
+ public static String getUserEmail() {
64
+ Context context = get();
65
+ return context == null ? StringUtils.EMPTY : context.getBaggage().getOrDefault("__app_context:operatorEmail", StringUtils.EMPTY);
66
+ }
67
+
68
+ /**
69
+ * 获取当前操作者展示名称
70
+ */
71
+ public static String getUserDisplayName() {
72
+ Context context = get();
73
+ return context == null ? StringUtils.EMPTY : context.getBaggage().getOrDefault("__app_context:displayName", StringUtils.EMPTY);
74
+ }
75
+
76
+ /**
77
+ * 获取当前操作者真实姓名
78
+ */
79
+ public static String getUserRealName() {
80
+ Context context = get();
81
+ return context == null ? StringUtils.EMPTY : context.getBaggage().getOrDefault("__app_context:operatorName", StringUtils.EMPTY);
82
+ }
83
+
84
+ /**
85
+ * 获取当前用户IP
86
+ */
87
+ public static String getUserIp() {
88
+ Context context = get();
89
+ if (context == null) {
90
+ return StringUtils.EMPTY;
91
+ }
92
+ return StringUtils.isBlank(context.getUserIP()) ? StringUtils.EMPTY : context.getUserIP();
93
+ }
94
+
95
+ /**
96
+ * 获取 requestId
97
+ */
98
+ public static String getRequestId() {
99
+ Context context = get();
100
+ if (context == null || CollectionUtils.isEmpty(context.getBaggage())) {
101
+ return StringUtils.EMPTY;
102
+ }
103
+ return context.getBaggage().getOrDefault(APP_CONTEXT_PREFIX + "requestId", StringUtils.EMPTY);
104
+ }
105
+
106
+ /**
107
+ * 获取当前客户端平台
108
+ */
109
+ public static String getPlatform() {
110
+ Context context = get();
111
+ if (context == null) {
112
+ return AppPlatform.UNKNOWN.value();
113
+ }
114
+ String platform = context.getAppPlatform();
115
+ return StringUtils.isBlank(platform) ? AppPlatform.UNKNOWN.value() : AppPlatform.findBy(platform).value();
116
+ }
117
+
118
+ /**
119
+ * 获取 networkType
120
+ */
121
+ public static int getNetworkType() {
122
+ String cAppNetworkType = ContextHelper.getAppContext("c_app_network_type");
123
+ return NumberUtils.toInt(cAppNetworkType);
124
+ }
125
+
126
+ public static String getHttpHeaderStr() {
127
+ Context context = get();
128
+ if (context == null) {
129
+ return StringUtils.EMPTY;
130
+ }
131
+ Map<String, String> baggage = Optional.ofNullable(context.getBaggage()).orElse(Collections.emptyMap());
132
+ return baggage.getOrDefault(APP_CONTEXT_PREFIX + HTTP_HEADER, "");
133
+ }
134
+
135
+ private HttpHeader getParsedHttpHeader() {
136
+ Context context = get();
137
+ if (context != null && context.getBaggage().containsKey(APP_CONTEXT_PREFIX + HTTP_HEADER)) {
138
+ String headerJson = ContextHelper.getAppContext(HTTP_HEADER);
139
+ HttpHeader header = JsonHelper.fromJson(headerJson, HttpHeader.class, false);
140
+ return ObjectUtils.defaultIfNull(header, new HttpHeader());
141
+ }
142
+ return new HttpHeader();
143
+ }
144
+
145
+ /**
146
+ * 内部封装的 Http Header 对象 (使用 Jackson 注解)
147
+ */
148
+ @Data
149
+ static class HttpHeader {
150
+ @JsonProperty("User-Agent")
151
+ private String userAgent;
152
+ @JsonProperty("xy-common-params")
153
+ private String commonParams;
154
+ @JsonProperty("X-Network-Source")
155
+ private String xNetworkSource;
156
+ @JsonProperty("Xy-Platform-Info")
157
+ private String platformInfo;
158
+ }
159
+
160
+ private Map<String, String> parseCommonParams() {
161
+ String paramStr = this.getParsedHttpHeader().getCommonParams();
162
+ if (StringUtils.isEmpty(paramStr)) {
163
+ return Collections.emptyMap();
164
+ }
165
+ Map<String, String> params = new HashMap<>();
166
+ for (String kv : paramStr.split("&")) {
167
+ String[] arr = kv.split("=");
168
+ if (arr.length == 2) {
169
+ try {
170
+ params.put(arr[0], URLDecoder.decode(arr[1], "UTF-8"));
171
+ } catch (Exception e) {
172
+ // ignore
173
+ }
174
+ }
175
+ }
176
+ return params;
177
+ }
178
+
179
+ public String getDeviceId() {
180
+ return StringUtils.trimToEmpty(parseCommonParams().get("deviceId"));
181
+ }
182
+ }
@@ -26,11 +26,6 @@
26
26
  <groupId>com.xiaohongshu</groupId>
27
27
  <artifactId>infra-framework-context</artifactId>
28
28
  </dependency>
29
- <dependency>
30
- <groupId>org.springframework.boot</groupId>
31
- <artifactId>spring-boot-starter-test</artifactId>
32
- <scope>test</scope>
33
- </dependency>
34
29
  </dependencies>
35
30
 
36
31
  </project>
@@ -47,11 +47,6 @@
47
47
  <groupId>com.xiaohongshu</groupId>
48
48
  <artifactId>infra-framework-rpc-core</artifactId>
49
49
  </dependency>
50
- <dependency>
51
- <groupId>org.springframework.boot</groupId>
52
- <artifactId>spring-boot-starter-test</artifactId>
53
- <scope>test</scope>
54
- </dependency>
55
50
  </dependencies>
56
51
 
57
52
  </project>
@@ -24,11 +24,6 @@
24
24
  <groupId>com.xiaohongshu.xray</groupId>
25
25
  <artifactId>xray-logging</artifactId>
26
26
  </dependency>
27
- <dependency>
28
- <groupId>org.springframework.boot</groupId>
29
- <artifactId>spring-boot-starter-test</artifactId>
30
- <scope>test</scope>
31
- </dependency>
32
27
  </dependencies>
33
28
 
34
29
  <build>
@@ -1,22 +1,19 @@
1
1
  <?xml version="1.0" encoding="UTF-8"?>
2
2
  <configuration>
3
- <property name="LOG_LEVEL" value="INFO"/>
4
- <include resource="logback-xray-base.xml"/>
3
+ <property name="LOG_LEVEL" value="INFO"/>
4
+ <include resource="logback-xray-base.xml"/>
5
5
 
6
- <root level="${LOG_LEVEL}">
7
- <appender-ref ref="ASYNC-CONSOLE-APPENDER"/>
8
- <!-- <appender-ref ref="Sentry"/>-->
9
- </root>
10
-
11
- <logger name="com.xiaohongshu.sns" level="debug"></logger>
12
- <logger name="org.apache.ibatis" level="INFO"></logger>
13
- <logger name="org.apache.catalina" level="ERROR"></logger>
14
- <logger name="org.apache.zookeeper" level="INFO"></logger>
15
- <logger name="org.springframework" level="INFO"></logger>
16
- <logger name="com.xiaohongshu.infra" level="ERROR"></logger>
17
- <logger name="com.xiaohongshu.racing" level="WARN"></logger>
18
- <logger name="com.dianping.cat" level="INFO"></logger>
19
- <logger name="red.midware.shaded" level="INFO"></logger>
20
- <logger name="events" level="INFO"></logger>
21
-
22
- </configuration>
6
+ <logger name="org.apache.ibatis" level="INFO"></logger>
7
+ <logger name="org.apache.catalina" level="ERROR"></logger>
8
+ <logger name="org.apache.zookeeper" level="INFO"></logger>
9
+ <logger name="org.springframework" level="INFO"></logger>
10
+ <logger name="com.xiaohongshu.infra" level="ERROR"></logger>
11
+ <logger name="com.xiaohongshu.racing" level="WARN"></logger>
12
+ <logger name="com.dianping.cat" level="INFO"></logger>
13
+ <logger name="red.midware.shaded" level="INFO"></logger>
14
+ <logger name="com.xiaohongshu.infra.xds.cds.CDSSharedStubWrapper" level="ERROR"></logger>
15
+ <logger name="events" level="INFO"></logger>
16
+ <logger name="com.zaxxer.hikari.HikariConfig" level="WARN"></logger>
17
+ <logger name="com.zaxxer.hikari.HikariDataSource" level="WARN"></logger>
18
+ <logger name="com.xiaohongshu.infra.rpc.core.registry.eds.MultiRegionsEdsThriftAddressSubscriberProvider" level="off"/>
19
+ </configuration>
@@ -0,0 +1,7 @@
1
+ <component name="ProjectCodeStyleConfiguration">
2
+ <code_scheme name="Project" version="173">
3
+ <ScalaCodeStyleSettings>
4
+ <option name="MULTILINE_STRING_CLOSING_QUOTES_ON_NEW_LINE" value="true" />
5
+ </ScalaCodeStyleSettings>
6
+ </code_scheme>
7
+ </component>
@@ -0,0 +1,5 @@
1
+ <component name="ProjectCodeStyleConfiguration">
2
+ <state>
3
+ <option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
4
+ </state>
5
+ </component>
@@ -0,0 +1,5 @@
1
+ <component name="InspectionProjectProfileManager">
2
+ <profile version="1.0">
3
+ <option name="myName" value="Project Default" />
4
+ </profile>
5
+ </component>
@@ -0,0 +1,14 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="AnalysisProjectProfileManager">
4
+ <option name="PROJECT_PROFILE" />
5
+ <option name="USE_PROJECT_LEVEL_SETTINGS" value="false" />
6
+ <list size="0" />
7
+ </component>
8
+ <component name="ProjectRootManager">
9
+ <output url="file://$PROJECT_DIR$/out" />
10
+ </component>
11
+ <component name="SuppressionsComponent">
12
+ <option name="suppComments" value="[]" />
13
+ </component>
14
+ </project>
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/wiki.iml" filepath="$PROJECT_DIR$/.idea/wiki.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="$PROJECT_DIR$/../../../../.." vcs="Git" />
5
+ </component>
6
+ </project>
@@ -0,0 +1,9 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="JAVA_MODULE" version="4">
3
+ <component name="NewModuleRootManager" inherit-compiler-output="true">
4
+ <exclude-output />
5
+ <content url="file://$MODULE_DIR$" />
6
+ <orderEntry type="inheritedJdk" />
7
+ <orderEntry type="sourceFolder" forTests="false" />
8
+ </component>
9
+ </module>
@@ -49,8 +49,6 @@ thrift idl开发参考[Thrift IDL开发规范](./README.md)
49
49
 
50
50
  Java sdk会以`maven_project`目录作为打包的基础目录,因此可以在目录中添加代码来定制sdk的功能。
51
51
 
52
- 例如,使用[SpringBoot AutoConfiguration](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-auto-configuration.html)为sdk开发自动配置功能,参考[例子](example)。
53
-
54
52
  ## 常见问题
55
53
 
56
54
  1. Invalid remote: origin
@@ -1,26 +1,12 @@
1
+ image: docker-reg.devops.xiaohongshu.com/shequ/sns-sdk-ci:latest
2
+
1
3
  stages:
2
4
  - build
3
- # 根据项目类型保留需要build的stage 并删掉其他stage
4
- build:java:
5
- stage: build
6
- image: docker-reg.devops.xiaohongshu.com/shequ/sns-sdk-ci:latest
7
- script:
8
- - skr -p ${CI_PROJECT_NAME} -c ${CI_COMMIT_REF_NAME}
9
- only:
10
- - branches
11
- - /^\d+\.\d+\.\d+$/
12
5
 
13
- build:go:
6
+ build:java:
14
7
  stage: build
15
- image: docker-reg.devops.xiaohongshu.com/shequ/sns-gosdk-ci:latest
16
8
  script:
17
9
  - skr -p ${CI_PROJECT_NAME} -c ${CI_COMMIT_REF_NAME}
18
10
  only:
19
11
  - branches
20
12
  - /^\d+\.\d+\.\d+$/
21
-
22
- build:node:
23
- stage: build
24
- image: docker-reg.devops.xiaohongshu.com/fe/ts-generator:latest
25
- script:
26
- - DEBUG=common-bin tsGen build --branch ${CI_COMMIT_REF_NAME} --userEmail ${GITLAB_USER_EMAIL} --ciId ${CI_COMMIT_SHA:0:8} --pId ${CI_PROJECT_ID}
@@ -0,0 +1,94 @@
1
+ namespace java com.xiaohongshu.infra.rpc.base
2
+
3
+ /**
4
+ * rpc context that contains:
5
+ * 1: open tracing infomation
6
+ * 2: userID for log coloring
7
+ * 3: an expandable map for custom loads
8
+ */
9
+ struct Context {
10
+ /**
11
+ * open tracing trace id
12
+ **/
13
+ 1: string traceID;
14
+ /**
15
+ * the caller's host (IP or hostname)
16
+ **/
17
+ 2: string clientHost;
18
+ /**
19
+ * open tracing baggae, can be used to carry custom loads
20
+ **/
21
+ 3: map<string, string> baggage;
22
+ /**
23
+ * open tracing span id
24
+ **/
25
+ 4: string spanID;
26
+ /**
27
+ * whether the very request is sampled
28
+ **/
29
+ 5: bool sampled;
30
+ /**
31
+ * open tracing parent span ID
32
+ **/
33
+ 6: string parentSpanID;
34
+ /**
35
+ * userID for log coloring
36
+ **/
37
+ 7: string userID;
38
+ /**
39
+ * appID for multi-app
40
+ **/
41
+ 8: string appID;
42
+ /**
43
+ * projectID for multi-app
44
+ **/
45
+ 9: string projectID;
46
+ /**
47
+ * app build number
48
+ **/
49
+ 10: i32 appBuild;
50
+ /**
51
+ * app platform
52
+ **/
53
+ 11: string appPlatform;
54
+ /**
55
+ * user ip address
56
+ **/
57
+ 12: string userIP;
58
+ /**
59
+ * session locale
60
+ **/
61
+ 13: string locale;
62
+ }
63
+
64
+ /**
65
+ * multi-app 枚举类型
66
+ **/
67
+ enum ProjectType {
68
+ XHS = 0
69
+ TOP = 1
70
+ }
71
+
72
+ /**
73
+ * common result
74
+ */
75
+ struct Result {
76
+ 1: required bool success,
77
+ 2: optional i32 code,
78
+ 3: optional string message
79
+ }
80
+
81
+ /**
82
+ * typedefs
83
+ */
84
+ typedef i64 Timestamp
85
+
86
+ /**
87
+ * the BaseService is defined for management purpose, such as check alive.
88
+ **/
89
+ service BaseService {
90
+ /**
91
+ * for checking alive
92
+ **/
93
+ void ping()
94
+ }
@@ -0,0 +1,39 @@
1
+ namespace java com.xiaohongshu.sns.demo.api.common
2
+
3
+ include "../base/base.thrift"
4
+
5
+ /**
6
+ * 分页-请求对象
7
+ **/
8
+ struct PageParam {
9
+ /**
10
+ * 页码
11
+ **/
12
+ 1:optional i32 pageNo;
13
+ /**
14
+ * 分页大小
15
+ **/
16
+ 2:optional i32 pageSize;
17
+ }
18
+
19
+ /**
20
+ * 分页-返回对象
21
+ **/
22
+ struct PageResp {
23
+ /**
24
+ * 当前页
25
+ **/
26
+ 1:optional i32 currentPage;
27
+ /**
28
+ * 分页大小
29
+ **/
30
+ 2:optional i32 pageSize;
31
+ /**
32
+ * 总页数
33
+ **/
34
+ 3:optional i32 totalPages;
35
+ /**
36
+ * 总条数
37
+ **/
38
+ 4:optional i32 totalCount;
39
+ }
@@ -0,0 +1,3 @@
1
+ namespace java com.xiaohongshu.sns.demo.api.dto
2
+
3
+ include "enum.thrift"
@@ -0,0 +1 @@
1
+ namespace java com.xiaohongshu.sns.demo.api.enum
@@ -1,4 +1,7 @@
1
1
  #!/bin/bash
2
2
 
3
- filename="./hello.thrift"
4
- thrift --gen java:generated_annotations=undated -out maven_project/src/main/java/ $filename
3
+ # 这里每一行就是一个thrift文件路径,直接遍历来快速构建
4
+ filename="./service.thrift"
5
+ for file in $filename; do
6
+ thrift --gen java:generated_annotations=undated -out maven_project/src/main/java/ $file
7
+ done