社区初始化
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package com.linkerbot.community.api;
|
||||
|
||||
import com.linkerbot.community.bean.Contracts.*;
|
||||
import com.linkerbot.community.bean.entity.User;
|
||||
import com.linkerbot.community.common.ApiResponse;
|
||||
import com.linkerbot.community.security.*;
|
||||
import com.linkerbot.community.service.UserService;
|
||||
import com.linkerbot.community.service.config.CommunityProperties;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.security.SecurityRequirements;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@Tag(name = "登录与用户")
|
||||
public class AuthController {
|
||||
private final CaptchaService captcha;
|
||||
private final TokenService tokens;
|
||||
private final UserService users;
|
||||
private final CommunityProperties config;
|
||||
|
||||
@GetMapping("/api/auth/captcha")
|
||||
@Operation(summary = "获取一次性图片验证码")
|
||||
@SecurityRequirements
|
||||
public ApiResponse<CaptchaView> captcha() {
|
||||
return ApiResponse.success(captcha.issue());
|
||||
}
|
||||
|
||||
@PostMapping("/api/auth/login")
|
||||
@Operation(summary = "邮箱和图片验证码登录,首次自动创建用户")
|
||||
@SecurityRequirements
|
||||
public ApiResponse<LoginView> login(@Valid @RequestBody LoginRequest input) {
|
||||
captcha.verify(input.captchaCode(), input.captchaAnswer());
|
||||
User user = users.login(input.email());
|
||||
return ApiResponse.success(
|
||||
new LoginView(
|
||||
tokens.issue(user.getId()),
|
||||
"Bearer",
|
||||
config.getTokenTtl().toSeconds(),
|
||||
users.view(user)));
|
||||
}
|
||||
|
||||
@PostMapping("/api/auth/logout")
|
||||
@Operation(summary = "退出登录")
|
||||
public ApiResponse<Void> logout(@RequestHeader("Authorization") String authorization) {
|
||||
tokens.revoke(authorization.startsWith("Bearer ") ? authorization.substring(7) : null);
|
||||
return ApiResponse.success(null);
|
||||
}
|
||||
|
||||
@GetMapping("/api/users/me")
|
||||
@Operation(summary = "当前用户")
|
||||
public ApiResponse<UserView> me() {
|
||||
return ApiResponse.success(users.view(users.require(CurrentUser.required())));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.linkerbot.community.api;
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration;
|
||||
|
||||
@MapperScan("com.linkerbot.community.service.mapper")
|
||||
@SpringBootApplication(
|
||||
scanBasePackages = "com.linkerbot.community",
|
||||
exclude = UserDetailsServiceAutoConfiguration.class)
|
||||
public class CommunityApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(CommunityApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
package com.linkerbot.community.api;
|
||||
|
||||
import com.linkerbot.community.bean.Contracts.*;
|
||||
import com.linkerbot.community.common.*;
|
||||
import com.linkerbot.community.security.CurrentUser;
|
||||
import com.linkerbot.community.service.*;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.security.SecurityRequirements;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@Tag(name = "社区")
|
||||
public class CommunityController {
|
||||
private final ProjectService projects;
|
||||
private final FileService files;
|
||||
private final InteractionService interactions;
|
||||
|
||||
@GetMapping("/api/project-types")
|
||||
@Operation(summary = "配置中的项目类型")
|
||||
@SecurityRequirements
|
||||
public ApiResponse<List<TypeView>> types() {
|
||||
return ApiResponse.success(projects.types());
|
||||
}
|
||||
|
||||
@GetMapping("/api/community/projects")
|
||||
@Operation(summary = "已发布项目列表,支持类型和关键词筛选")
|
||||
@SecurityRequirements
|
||||
public ApiResponse<PageResponse<ProjectView>> list(
|
||||
@RequestParam(defaultValue = "1") long page,
|
||||
@RequestParam(defaultValue = "20") long size,
|
||||
@RequestParam(required = false) String type,
|
||||
@RequestParam(required = false) String keyword) {
|
||||
return ApiResponse.success(
|
||||
projects.list(null, type, keyword, page, size, CurrentUser.optional()));
|
||||
}
|
||||
|
||||
@GetMapping("/api/community/projects/{projectCode}")
|
||||
@Operation(summary = "社区项目详情")
|
||||
@SecurityRequirements
|
||||
public ApiResponse<ProjectView> detail(@PathVariable String projectCode) {
|
||||
return ApiResponse.success(
|
||||
projects.view(projects.published(projectCode), CurrentUser.optional()));
|
||||
}
|
||||
|
||||
@GetMapping("/api/community/projects/{projectCode}/tree")
|
||||
@Operation(summary = "已发布版本的目录")
|
||||
@SecurityRequirements
|
||||
public ApiResponse<TreeView> tree(
|
||||
@PathVariable String projectCode,
|
||||
@RequestParam(defaultValue = "") String path,
|
||||
@RequestParam(required = false) String commitSha) {
|
||||
rejectRef(commitSha);
|
||||
return ApiResponse.success(files.communityTree(projectCode, path));
|
||||
}
|
||||
|
||||
@GetMapping("/api/community/projects/{projectCode}/files")
|
||||
@Operation(summary = "已发布版本文件,download=true 时下载")
|
||||
@SecurityRequirements
|
||||
public ResponseEntity<?> file(
|
||||
@PathVariable String projectCode,
|
||||
@RequestParam String path,
|
||||
@RequestParam(defaultValue = "false") boolean download,
|
||||
@RequestParam(required = false) String commitSha) {
|
||||
rejectRef(commitSha);
|
||||
return download
|
||||
? WorkspaceController.download(files.communityFile(projectCode, path))
|
||||
: ResponseEntity.ok(
|
||||
ApiResponse.success(files.communityFileView(projectCode, path)));
|
||||
}
|
||||
|
||||
private void rejectRef(String sha) {
|
||||
if (sha != null) throw BusinessException.badRequest("社区文件仅允许读取当前已发布版本");
|
||||
}
|
||||
|
||||
@PostMapping("/api/community/projects/{projectCode}/forks")
|
||||
@Operation(summary = "复制已发布文件到我的工作区")
|
||||
public ApiResponse<ProjectView> fork(
|
||||
@PathVariable String projectCode, @Valid @RequestBody ForkInput input) {
|
||||
return ApiResponse.success(projects.fork(CurrentUser.required(), projectCode, input));
|
||||
}
|
||||
|
||||
@GetMapping("/api/community/projects/{projectCode}/comments")
|
||||
@Operation(summary = "评论列表")
|
||||
@SecurityRequirements
|
||||
public ApiResponse<PageResponse<CommentView>> comments(
|
||||
@PathVariable String projectCode,
|
||||
@RequestParam(defaultValue = "1") long page,
|
||||
@RequestParam(defaultValue = "20") long size) {
|
||||
return ApiResponse.success(interactions.list(projectCode, page, size));
|
||||
}
|
||||
|
||||
@PostMapping("/api/community/projects/{projectCode}/comments")
|
||||
@Operation(summary = "新增纯文本评论")
|
||||
public ApiResponse<CommentView> comment(
|
||||
@PathVariable String projectCode, @Valid @RequestBody CommentInput input) {
|
||||
return ApiResponse.success(
|
||||
interactions.comment(CurrentUser.required(), projectCode, input));
|
||||
}
|
||||
|
||||
@DeleteMapping("/api/comments/{commentCode}")
|
||||
@Operation(summary = "删除自己的评论")
|
||||
public ApiResponse<Void> deleteComment(@PathVariable String commentCode) {
|
||||
interactions.deleteComment(CurrentUser.required(), commentCode);
|
||||
return ApiResponse.success(null);
|
||||
}
|
||||
|
||||
@PutMapping("/api/community/projects/{projectCode}/like")
|
||||
@Operation(summary = "幂等点赞")
|
||||
public ApiResponse<Void> like(@PathVariable String projectCode) {
|
||||
interactions.like(CurrentUser.required(), projectCode);
|
||||
return ApiResponse.success(null);
|
||||
}
|
||||
|
||||
@DeleteMapping("/api/community/projects/{projectCode}/like")
|
||||
@Operation(summary = "幂等取消点赞")
|
||||
public ApiResponse<Void> unlike(@PathVariable String projectCode) {
|
||||
interactions.unlike(CurrentUser.required(), projectCode);
|
||||
return ApiResponse.success(null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.linkerbot.community.api;
|
||||
|
||||
import io.swagger.v3.oas.models.*;
|
||||
import io.swagger.v3.oas.models.info.Info;
|
||||
import io.swagger.v3.oas.models.security.*;
|
||||
|
||||
import org.springframework.context.annotation.*;
|
||||
|
||||
@Configuration
|
||||
public class OpenApiConfiguration {
|
||||
@Bean
|
||||
public OpenAPI openAPI() {
|
||||
return new OpenAPI()
|
||||
.info(
|
||||
new Info()
|
||||
.title("Community 首期接口")
|
||||
.version("v1")
|
||||
.description(
|
||||
"四表社区后端。登录仅邮箱和图片验证码,无法验证邮箱归属。Gitea"
|
||||
+ " 仓库统一存放在组织下。文件下载返回二进制,其余响应使用统一包装。"))
|
||||
.components(
|
||||
new Components()
|
||||
.addSecuritySchemes(
|
||||
"bearer",
|
||||
new SecurityScheme()
|
||||
.type(SecurityScheme.Type.HTTP)
|
||||
.scheme("bearer")))
|
||||
.addSecurityItem(new SecurityRequirement().addList("bearer"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.linkerbot.community.api;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Profile("prod")
|
||||
public class ProductionConfiguration {
|
||||
public ProductionConfiguration(Environment environment) {
|
||||
for (String property :
|
||||
List.of(
|
||||
"spring.datasource.url",
|
||||
"spring.datasource.username",
|
||||
"spring.datasource.password",
|
||||
"community.gitea.base-url",
|
||||
"community.gitea.token")) {
|
||||
String value;
|
||||
try {
|
||||
value = environment.getRequiredProperty(property);
|
||||
} catch (IllegalArgumentException | IllegalStateException error) {
|
||||
// 仅提示配置项名称,避免异常链携带配置值。
|
||||
throw new IllegalStateException("生产环境必须配置 " + property);
|
||||
}
|
||||
if (value.isBlank()) {
|
||||
throw new IllegalStateException("生产环境必须配置 " + property);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
package com.linkerbot.community.api;
|
||||
|
||||
import com.linkerbot.community.bean.Contracts.*;
|
||||
import com.linkerbot.community.common.*;
|
||||
import com.linkerbot.community.security.CurrentUser;
|
||||
import com.linkerbot.community.service.*;
|
||||
import com.linkerbot.community.service.gitea.GiteaClient;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/api/workspace/projects")
|
||||
@Tag(name = "我的工作区")
|
||||
public class WorkspaceController {
|
||||
private final ProjectService projects;
|
||||
private final FileService files;
|
||||
|
||||
@GetMapping
|
||||
@Operation(summary = "我的项目列表")
|
||||
public ApiResponse<PageResponse<ProjectView>> list(
|
||||
@RequestParam(defaultValue = "1") long page,
|
||||
@RequestParam(defaultValue = "20") long size,
|
||||
@RequestParam(required = false) String type,
|
||||
@RequestParam(required = false) String keyword) {
|
||||
long user = CurrentUser.required();
|
||||
return ApiResponse.success(projects.list(user, type, keyword, page, size, user));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@Operation(summary = "创建项目和组织中的私有仓库")
|
||||
public ApiResponse<ProjectView> create(@Valid @RequestBody ProjectInput input) {
|
||||
return ApiResponse.success(projects.create(CurrentUser.required(), input));
|
||||
}
|
||||
|
||||
@GetMapping("/{projectCode}")
|
||||
@Operation(summary = "我的项目详情")
|
||||
public ApiResponse<ProjectView> detail(@PathVariable String projectCode) {
|
||||
long user = CurrentUser.required();
|
||||
return ApiResponse.success(projects.view(projects.owner(user, projectCode), user));
|
||||
}
|
||||
|
||||
@PatchMapping("/{projectCode}")
|
||||
@Operation(summary = "修改名称、描述和类型")
|
||||
public ApiResponse<ProjectView> update(
|
||||
@PathVariable String projectCode, @Valid @RequestBody ProjectInput input) {
|
||||
return ApiResponse.success(projects.update(CurrentUser.required(), projectCode, input));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{projectCode}")
|
||||
@Operation(summary = "逻辑删除项目并撤回社区")
|
||||
public ApiResponse<Void> delete(@PathVariable String projectCode) {
|
||||
projects.delete(CurrentUser.required(), projectCode);
|
||||
return ApiResponse.success(null);
|
||||
}
|
||||
|
||||
@GetMapping("/{projectCode}/tree")
|
||||
@Operation(summary = "读取目录,可指定本项目 Commit")
|
||||
public ApiResponse<TreeView> tree(
|
||||
@PathVariable String projectCode,
|
||||
@RequestParam(defaultValue = "") String path,
|
||||
@RequestParam(required = false) String commitSha) {
|
||||
return ApiResponse.success(
|
||||
files.ownerTree(CurrentUser.required(), projectCode, path, commitSha));
|
||||
}
|
||||
|
||||
@GetMapping("/{projectCode}/files")
|
||||
@Operation(summary = "读取文件,download=true 时下载")
|
||||
public ResponseEntity<?> file(
|
||||
@PathVariable String projectCode,
|
||||
@RequestParam String path,
|
||||
@RequestParam(required = false) String commitSha,
|
||||
@RequestParam(defaultValue = "false") boolean download) {
|
||||
long user = CurrentUser.required();
|
||||
if (!download)
|
||||
return ResponseEntity.ok(
|
||||
ApiResponse.success(files.ownerFileView(user, projectCode, path, commitSha)));
|
||||
return download(files.ownerFile(user, projectCode, path, commitSha));
|
||||
}
|
||||
|
||||
public static ResponseEntity<byte[]> download(GiteaClient.StoredFile file) {
|
||||
String name = file.path().substring(file.path().lastIndexOf('/') + 1);
|
||||
return ResponseEntity.ok()
|
||||
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
||||
.header(
|
||||
HttpHeaders.CONTENT_DISPOSITION,
|
||||
ContentDisposition.attachment()
|
||||
.filename(name, StandardCharsets.UTF_8)
|
||||
.build()
|
||||
.toString())
|
||||
.body(file.bytes());
|
||||
}
|
||||
|
||||
@PostMapping("/{projectCode}/commits")
|
||||
@Operation(summary = "多文件变更提交为一个 Commit")
|
||||
public ApiResponse<CommitView> commit(
|
||||
@PathVariable String projectCode, @Valid @RequestBody CommitInput input) {
|
||||
return ApiResponse.success(files.commit(CurrentUser.required(), projectCode, input));
|
||||
}
|
||||
|
||||
@GetMapping("/{projectCode}/commits")
|
||||
@Operation(summary = "main 提交历史")
|
||||
public ApiResponse<PageResponse<HistoryEntry>> history(
|
||||
@PathVariable String projectCode,
|
||||
@RequestParam(defaultValue = "1") long page,
|
||||
@RequestParam(defaultValue = "20") long size) {
|
||||
return ApiResponse.success(files.history(CurrentUser.required(), projectCode, page, size));
|
||||
}
|
||||
|
||||
@PostMapping(
|
||||
value = "/{projectCode}/uploads/commit",
|
||||
consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
@Operation(summary = "上传或替换文件并一次提交")
|
||||
public ApiResponse<CommitView> upload(
|
||||
@PathVariable String projectCode,
|
||||
@RequestParam String expectedCommitSha,
|
||||
@RequestParam String message,
|
||||
@RequestParam(defaultValue = "") String directory,
|
||||
@RequestParam("files") List<MultipartFile> uploaded,
|
||||
@RequestParam(required = false) List<String> paths) {
|
||||
return ApiResponse.success(
|
||||
files.upload(
|
||||
CurrentUser.required(),
|
||||
projectCode,
|
||||
expectedCommitSha,
|
||||
message,
|
||||
directory,
|
||||
uploaded,
|
||||
paths));
|
||||
}
|
||||
|
||||
@PutMapping("/{projectCode}/publication")
|
||||
@Operation(summary = "发布或手动更新社区文件版本")
|
||||
public ApiResponse<ProjectView> publish(
|
||||
@PathVariable String projectCode, @Valid @RequestBody PublicationInput input) {
|
||||
return ApiResponse.success(
|
||||
projects.publish(CurrentUser.required(), projectCode, input.commitSha()));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{projectCode}/publication")
|
||||
@Operation(summary = "撤回社区")
|
||||
public ApiResponse<Void> withdraw(@PathVariable String projectCode) {
|
||||
projects.withdraw(CurrentUser.required(), projectCode);
|
||||
return ApiResponse.success(null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: ${DB_URL}
|
||||
username: ${DB_USERNAME}
|
||||
password: ${DB_PASSWORD}
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
hikari:
|
||||
pool-name: CommunityHikariCP
|
||||
minimum-idle: 1
|
||||
maximum-pool-size: 20
|
||||
connection-timeout: 5000
|
||||
|
||||
community:
|
||||
gitea:
|
||||
base-url: ${GITEA_BASE_URL}
|
||||
owner: ${GITEA_ORGANIZATION:community}
|
||||
token: ${GITEA_TOKEN}
|
||||
|
||||
logging:
|
||||
file:
|
||||
path: ${LOG_PATH:logs/prod}
|
||||
level:
|
||||
com.linkerbot.community: INFO
|
||||
@@ -0,0 +1,23 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: ${DB_URL:jdbc:mysql://localhost:3306/community?useUnicode=true&characterEncoding=utf8&connectionTimeZone=UTC}
|
||||
username: ${DB_USERNAME:root}
|
||||
password: ${DB_PASSWORD:}
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
hikari:
|
||||
pool-name: CommunityHikariCP
|
||||
minimum-idle: 1
|
||||
maximum-pool-size: 10
|
||||
connection-timeout: 5000
|
||||
|
||||
community:
|
||||
gitea:
|
||||
base-url: ${GITEA_BASE_URL:http://localhost:3000}
|
||||
owner: ${GITEA_ORGANIZATION:community}
|
||||
token: ${GITEA_TOKEN:}
|
||||
|
||||
logging:
|
||||
file:
|
||||
path: ${LOG_PATH:logs/test}
|
||||
level:
|
||||
com.linkerbot.community: DEBUG
|
||||
@@ -0,0 +1,45 @@
|
||||
server:
|
||||
port: ${SERVER_PORT:${PORT:8993}}
|
||||
spring:
|
||||
application:
|
||||
name: community
|
||||
profiles:
|
||||
default: test
|
||||
sql:
|
||||
init:
|
||||
mode: never
|
||||
servlet:
|
||||
multipart:
|
||||
max-file-size: 10MB
|
||||
max-request-size: 52MB
|
||||
jackson:
|
||||
time-zone: UTC
|
||||
main:
|
||||
banner-mode: off
|
||||
mybatis-plus:
|
||||
configuration:
|
||||
map-underscore-to-camel-case: true
|
||||
community:
|
||||
types:
|
||||
GENERAL: 通用
|
||||
HUMANOID: 人形
|
||||
QUADRUPED: 四足
|
||||
WHEELED: 轮式
|
||||
captcha-ttl: 2m
|
||||
token-ttl: 24h
|
||||
max-text-bytes: 1048576
|
||||
max-file-bytes: 10485760
|
||||
max-request-bytes: 52428800
|
||||
gitea:
|
||||
connect-timeout: 5s
|
||||
request-timeout: 30s
|
||||
springdoc:
|
||||
swagger-ui:
|
||||
path: /swagger-ui.html
|
||||
logging:
|
||||
level:
|
||||
root: INFO
|
||||
org.springframework: INFO
|
||||
# 测试环境开启项目 DEBUG 时,不输出 SQL 参数或完整请求内容。
|
||||
com.linkerbot.community.service.mapper: INFO
|
||||
org.springframework.web: INFO
|
||||
Reference in New Issue
Block a user