224 lines
7.9 KiB
Java
224 lines
7.9 KiB
Java
package run.webup.platform;
|
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
import jakarta.validation.Valid;
|
|
import jakarta.validation.constraints.*;
|
|
import java.math.BigDecimal;
|
|
import java.util.*;
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
@RestController
|
|
@RequestMapping("/api")
|
|
public class HostingController {
|
|
private final JdbcTemplate db;
|
|
private final HostingService hosting;
|
|
private final SandboxClient sandbox;
|
|
private final RuntimeGateway runtime;
|
|
|
|
public HostingController(
|
|
JdbcTemplate db, HostingService hosting, SandboxClient sandbox, RuntimeGateway runtime) {
|
|
this.db = db;
|
|
this.hosting = hosting;
|
|
this.sandbox = sandbox;
|
|
this.runtime = runtime;
|
|
}
|
|
|
|
public record Plan(
|
|
@NotBlank @Size(max = 100) String name,
|
|
@NotNull @DecimalMin("0") @Digits(integer = 12, fraction = 8) BigDecimal monthlyPrice,
|
|
@Min(50) @Max(64000) int cpuMillis,
|
|
@Min(64) @Max(262144) int memoryMb,
|
|
@Min(1) @Max(100000) int diskGb,
|
|
@Min(0) @Max(10000000) int trafficGb,
|
|
@Min(1) @Max(100) int maxServices,
|
|
@Min(1) @Max(20) int maxReplicas,
|
|
boolean autoFailover,
|
|
boolean enabled) {}
|
|
|
|
public record Purchase(@NotNull UUID planId, @NotBlank @Size(max = 180) String idempotencyKey) {}
|
|
|
|
public record Deploy(@NotBlank @Size(max = 200) String idempotencyKey, UUID rollbackId) {}
|
|
|
|
public record SourceDeploy(@NotBlank @Size(max = 200) String idempotencyKey) {}
|
|
|
|
public record ServiceSpec(
|
|
@NotBlank @Pattern(regexp = "[a-z][a-z0-9-]{0,62}") String name,
|
|
@NotBlank @Pattern(regexp = "[a-zA-Z0-9][a-zA-Z0-9._/:@-]{0,499}") String image,
|
|
@NotNull List<String> command,
|
|
Map<String, String> environment,
|
|
@Min(1) @Max(65535) int port,
|
|
@Min(50) @Max(64000) int cpuMillis,
|
|
@Min(64) @Max(262144) int memoryMb,
|
|
@Min(1) @Max(20) int replicas,
|
|
@NotNull @Pattern(regexp = "/[^\\s]{0,499}|^$") String healthPath,
|
|
boolean enabled) {}
|
|
|
|
@GetMapping("/plans")
|
|
public Object plans() {
|
|
return db.queryForList("SELECT * FROM billing_plans WHERE enabled=TRUE ORDER BY monthly_price");
|
|
}
|
|
|
|
@GetMapping("/admin/plans")
|
|
@PreAuthorize("hasAuthority('finance:write')")
|
|
public Object all() {
|
|
return db.queryForList("SELECT * FROM billing_plans ORDER BY created_at");
|
|
}
|
|
|
|
@PostMapping("/admin/plans")
|
|
@PreAuthorize("hasAuthority('finance:write')")
|
|
@Transactional
|
|
public Object create(
|
|
@AuthenticationPrincipal SecurityConfig.Identity who,
|
|
@Valid @RequestBody Plan p,
|
|
HttpServletRequest req) {
|
|
UUID id = UUID.randomUUID();
|
|
db.update(
|
|
"INSERT INTO"
|
|
+ " billing_plans(id,name,monthly_price,cpu_millis,memory_mb,disk_gb,traffic_gb,max_services,max_replicas,auto_failover,enabled)"
|
|
+ " VALUES (?,?,?,?,?,?,?,?,?,?,?)",
|
|
id,
|
|
p.name(),
|
|
p.monthlyPrice(),
|
|
p.cpuMillis(),
|
|
p.memoryMb(),
|
|
p.diskGb(),
|
|
p.trafficGb(),
|
|
p.maxServices(),
|
|
p.maxReplicas(),
|
|
p.autoFailover(),
|
|
p.enabled());
|
|
audit(who, id, req);
|
|
return Map.of("id", id);
|
|
}
|
|
|
|
@PutMapping("/admin/plans/{id}")
|
|
@PreAuthorize("hasAuthority('finance:write')")
|
|
@Transactional
|
|
public Object update(
|
|
@AuthenticationPrincipal SecurityConfig.Identity who,
|
|
@PathVariable UUID id,
|
|
@Valid @RequestBody Plan p,
|
|
HttpServletRequest req) {
|
|
if (db.update(
|
|
"UPDATE billing_plans SET"
|
|
+ " name=?,monthly_price=?,cpu_millis=?,memory_mb=?,disk_gb=?,traffic_gb=?,max_services=?,max_replicas=?,auto_failover=?,enabled=?,updated_at=CURRENT_TIMESTAMP"
|
|
+ " WHERE id=?",
|
|
p.name(),
|
|
p.monthlyPrice(),
|
|
p.cpuMillis(),
|
|
p.memoryMb(),
|
|
p.diskGb(),
|
|
p.trafficGb(),
|
|
p.maxServices(),
|
|
p.maxReplicas(),
|
|
p.autoFailover(),
|
|
p.enabled(),
|
|
id)
|
|
== 0) throw new ApiError.Failure(404, "套餐不存在");
|
|
audit(who, id, req);
|
|
return Map.of("message", "套餐已保存,已有订阅保持购买快照");
|
|
}
|
|
|
|
void audit(SecurityConfig.Identity who, UUID id, HttpServletRequest req) {
|
|
db.update(
|
|
"INSERT INTO admin_operation_logs(id,actor_id,action,target,ip,details) VALUES"
|
|
+ " (?,?,?,?,?,?)",
|
|
UUID.randomUUID(),
|
|
who.id(),
|
|
"PLAN_SAVE",
|
|
id.toString(),
|
|
AuthController.ip(req),
|
|
"套餐配置已保存");
|
|
}
|
|
|
|
@PostMapping("/projects/{project}/subscription")
|
|
public Object buy(
|
|
@AuthenticationPrincipal SecurityConfig.Identity who,
|
|
@PathVariable UUID project,
|
|
@Valid @RequestBody Purchase p) {
|
|
return hosting.subscribe(who.id(), project, p.planId(), p.idempotencyKey());
|
|
}
|
|
|
|
@GetMapping("/projects/{project}/subscription")
|
|
public Object subscription(
|
|
@AuthenticationPrincipal SecurityConfig.Identity who, @PathVariable UUID project) {
|
|
return hosting.subscription(who.id(), project);
|
|
}
|
|
|
|
@GetMapping("/projects/{project}/publication-status")
|
|
public Object publicationStatus(
|
|
@AuthenticationPrincipal SecurityConfig.Identity who, @PathVariable UUID project) {
|
|
return hosting.publicationStatus(who.id(), project);
|
|
}
|
|
|
|
@GetMapping("/projects/{project}/services")
|
|
public Object services(
|
|
@AuthenticationPrincipal SecurityConfig.Identity who, @PathVariable UUID project) {
|
|
return hosting.services(who.id(), project);
|
|
}
|
|
|
|
@PostMapping("/projects/{project}/services")
|
|
public Object service(
|
|
@AuthenticationPrincipal SecurityConfig.Identity who,
|
|
@PathVariable UUID project,
|
|
@Valid @RequestBody ServiceSpec s) {
|
|
return hosting.service(who.id(), project, null, s);
|
|
}
|
|
|
|
@PutMapping("/projects/{project}/services/{id}")
|
|
public Object updateService(
|
|
@AuthenticationPrincipal SecurityConfig.Identity who,
|
|
@PathVariable UUID project,
|
|
@PathVariable UUID id,
|
|
@Valid @RequestBody ServiceSpec s) {
|
|
return hosting.service(who.id(), project, id, s);
|
|
}
|
|
|
|
@PostMapping("/projects/{project}/deployments")
|
|
public Object deploy(
|
|
@AuthenticationPrincipal SecurityConfig.Identity who,
|
|
@PathVariable UUID project,
|
|
@Valid @RequestBody Deploy p) {
|
|
return hosting.deploy(who.id(), project, p.idempotencyKey(), p.rollbackId());
|
|
}
|
|
|
|
@GetMapping("/projects/{project}/deployments")
|
|
public Object deployments(
|
|
@AuthenticationPrincipal SecurityConfig.Identity who, @PathVariable UUID project) {
|
|
return hosting.deployments(who.id(), project);
|
|
}
|
|
|
|
@PostMapping("/projects/{project}/deployments/source")
|
|
public Object deploySource(
|
|
@AuthenticationPrincipal SecurityConfig.Identity who,
|
|
@PathVariable UUID project,
|
|
@Valid @RequestBody SourceDeploy request) {
|
|
var existing = hosting.beforeSourceSnapshot(who.id(), project, request.idempotencyKey());
|
|
if (existing != null) return existing;
|
|
if (!runtime.request("/runtime/source-ready", null).path("ready").asBoolean(false))
|
|
throw new ApiError.Failure(503, "源码发布运行环境尚未启用,请联系管理员");
|
|
return hosting.deploySource(
|
|
who.id(), project, request.idempotencyKey(), sandbox.releaseSnapshot(project));
|
|
}
|
|
|
|
@GetMapping("/projects/{project}/deployments/{id}/events")
|
|
public Object events(
|
|
@AuthenticationPrincipal SecurityConfig.Identity who,
|
|
@PathVariable UUID project,
|
|
@PathVariable UUID id) {
|
|
hosting.own(who.id(), project);
|
|
if (db.queryForObject(
|
|
"SELECT COUNT(*) FROM project_deployments WHERE id=? AND project_id=?",
|
|
Integer.class,
|
|
id,
|
|
project)
|
|
== 0) throw new ApiError.Failure(404, "部署不存在");
|
|
return db.queryForList(
|
|
"SELECT * FROM deployment_events WHERE deployment_id=? ORDER BY created_at", id);
|
|
}
|
|
}
|