Add real-time layout sync and refresh default layout
This commit is contained in:
1 parent
274b8538b7
commit
f79e30897b
20 files changed
+289
-77
No files matched your search
@@ -26,7 +26,7 @@ spec:
|
||||
spec:
|
||||
containers:
|
||||
- name: server
|
||||
image: docker.io/library/vibe-launcher-backend:1.1.0-deploy8
|
||||
image: docker.io/library/vibe-launcher-backend:1.1.0-deploy11
|
||||
imagePullPolicy: IfNotPresent
|
||||
ports:
|
||||
- name: http
|
||||
|
||||
@@ -2,8 +2,10 @@ package com.ttstd.launcher;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableScheduling
|
||||
public class LauncherApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(LauncherApplication.class, args);
|
||||
|
||||
@@ -23,6 +23,7 @@ import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
||||
import tools.jackson.core.JacksonException;
|
||||
import tools.jackson.databind.DeserializationFeature;
|
||||
import tools.jackson.databind.JsonNode;
|
||||
@@ -39,13 +40,15 @@ class LayoutController {
|
||||
+ "img-src 'self' data:; connect-src 'self'; frame-ancestors 'none'; base-uri 'none'; form-action 'none'";
|
||||
|
||||
private final LayoutStore store;
|
||||
private final LayoutEvents events;
|
||||
private final RequestPolicy policy;
|
||||
private final LayoutValidator validator;
|
||||
private final ObjectMapper mapper;
|
||||
private final Resource editor = new ClassPathResource("launcher/web/index.html");
|
||||
|
||||
LayoutController(LayoutStore store, RequestPolicy policy, LayoutValidator validator, ObjectMapper mapper) {
|
||||
LayoutController(LayoutStore store, RequestPolicy policy, LayoutValidator validator, ObjectMapper mapper, LayoutEvents events) {
|
||||
this.store = store;
|
||||
this.events = events;
|
||||
this.policy = policy;
|
||||
this.validator = validator;
|
||||
this.mapper = mapper;
|
||||
@@ -73,6 +76,13 @@ class LayoutController {
|
||||
return Map.of("status", "ok");
|
||||
}
|
||||
|
||||
@GetMapping(value = "/api/layout/events", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
||||
SseEmitter layoutEvents(HttpServletRequest request, HttpServletResponse response) {
|
||||
policy.requireDevice(request, response);
|
||||
response.setHeader("X-Accel-Buffering", "no");
|
||||
return events.subscribe();
|
||||
}
|
||||
|
||||
@GetMapping("/api/layout")
|
||||
ResponseEntity<byte[]> deviceLayout(HttpServletRequest request, HttpServletResponse response) throws SQLException {
|
||||
policy.requireDevice(request, response);
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.ttstd.launcher;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
||||
|
||||
@Component
|
||||
class LayoutEvents {
|
||||
private final LayoutStore store;
|
||||
private final Set<SseEmitter> subscribers = ConcurrentHashMap.newKeySet();
|
||||
private int lastRevision;
|
||||
private long lastHeartbeat;
|
||||
|
||||
LayoutEvents(LayoutStore store) {
|
||||
this.store = store;
|
||||
}
|
||||
|
||||
SseEmitter subscribe() {
|
||||
// Periodic reconnects also refresh device authorization and check for missed releases.
|
||||
var emitter = new SseEmitter(TimeUnit.MINUTES.toMillis(30));
|
||||
emitter.onCompletion(() -> subscribers.remove(emitter));
|
||||
emitter.onTimeout(() -> subscribers.remove(emitter));
|
||||
emitter.onError(error -> subscribers.remove(emitter));
|
||||
subscribers.add(emitter);
|
||||
send(emitter, SseEmitter.event().comment("connected"));
|
||||
return emitter;
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelay = 1000)
|
||||
void emitLatestRevision() throws SQLException {
|
||||
if (subscribers.isEmpty()) return;
|
||||
// Read the committed revision once across all subscribers, including after rollback.
|
||||
int revision = store.latestRevision();
|
||||
if (revision != lastRevision) {
|
||||
lastRevision = revision;
|
||||
for (var emitter : subscribers) {
|
||||
send(emitter, SseEmitter.event().name("layout-changed").id(Integer.toString(revision))
|
||||
.data(Integer.toString(revision)));
|
||||
}
|
||||
}
|
||||
long now = System.nanoTime();
|
||||
if (now - lastHeartbeat >= TimeUnit.SECONDS.toNanos(10) || lastHeartbeat == 0) {
|
||||
lastHeartbeat = now;
|
||||
for (var emitter : subscribers) send(emitter, SseEmitter.event().comment("heartbeat"));
|
||||
}
|
||||
}
|
||||
|
||||
private void send(SseEmitter emitter, SseEmitter.SseEventBuilder event) {
|
||||
try {
|
||||
emitter.send(event);
|
||||
} catch (IOException | IllegalStateException error) {
|
||||
subscribers.remove(emitter);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -141,6 +141,13 @@ class LayoutStore {
|
||||
}
|
||||
}
|
||||
|
||||
int latestRevision() throws SQLException {
|
||||
try (var connection = connect(); var statement = connection.createStatement();
|
||||
var rows = statement.executeQuery("SELECT MAX(revision) FROM releases")) {
|
||||
return rows.getInt(1);
|
||||
}
|
||||
}
|
||||
|
||||
Release latest() throws SQLException {
|
||||
try (var connection = connect(); var statement = connection.createStatement();
|
||||
var rows = statement.executeQuery(LATEST)) {
|
||||
|
||||
@@ -49,7 +49,7 @@ class LayoutValidator {
|
||||
}
|
||||
int width = canvas.path("designWidth").asInt(1080);
|
||||
int height = canvas.path("designHeight").asInt(1920);
|
||||
int safeTop = canvas.path("safeTop").asInt(132);
|
||||
int safeTop = canvas.path("safeTop").asInt(72);
|
||||
int safeBottom = canvas.path("safeBottom").asInt(72);
|
||||
if (width < 320 || height < 480) error(issues, "canvas", "画布最小尺寸为 320 × 480");
|
||||
if (width > MAX_CANVAS || height > MAX_CANVAS) error(issues, "canvas", "画布尺寸不能超过 " + MAX_CANVAS);
|
||||
|
||||
@@ -34,6 +34,17 @@ class LayoutApiTest {
|
||||
|
||||
@TempDir Path dir;
|
||||
|
||||
@Test
|
||||
void layoutEventStreamRequiresDeviceToken() throws Exception {
|
||||
MockMvc mvc = mvc(dir.resolve("events.sqlite"));
|
||||
mvc.perform(get("/api/layout/events")).andExpect(status().isUnauthorized());
|
||||
mvc.perform(get("/api/layout/events").header("Authorization", ADMIN)).andExpect(status().isUnauthorized());
|
||||
mvc.perform(get("/api/layout/events").header("Authorization", DEVICE))
|
||||
.andExpect(org.springframework.test.web.servlet.result.MockMvcResultMatchers.request().asyncStarted())
|
||||
.andExpect(header().string("X-Accel-Buffering", "no"))
|
||||
.andExpect(content().string(org.hamcrest.Matchers.containsString(":connected")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void widgetCatalogRequiresAdminAndPersistsUniqueComponents() throws Exception {
|
||||
String path = "/api/admin/widget-providers";
|
||||
@@ -269,7 +280,7 @@ class LayoutApiTest {
|
||||
var validator = new LayoutValidator();
|
||||
var store = new LayoutStore(database.toString(), mapper, validator);
|
||||
var policy = new RequestPolicy(ADMIN.substring(7), DEVICE.substring(7));
|
||||
return MockMvcBuilders.standaloneSetup(new LayoutController(store, policy, validator, mapper))
|
||||
return MockMvcBuilders.standaloneSetup(new LayoutController(store, policy, validator, mapper, new LayoutEvents(store)))
|
||||
.setControllerAdvice(new ApiExceptionHandler())
|
||||
.addFilters(policy).build();
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ class UniformityTest {
|
||||
assertEquals(3, layout.path("schemaVersion").intValue());
|
||||
assertEquals(1080, layout.path("canvas").path("designWidth").intValue());
|
||||
assertEquals(1920, layout.path("canvas").path("designHeight").intValue());
|
||||
assertEquals(132, layout.path("canvas").path("safeTop").intValue());
|
||||
assertEquals(72, layout.path("canvas").path("safeTop").intValue());
|
||||
assertEquals(72, layout.path("canvas").path("safeBottom").intValue());
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user