XuqmGroup-Server/tenant-service/src/main/java/com/xuqm/tenant/controller/SdkConfigController.java

84 行
3.0 KiB
Java

package com.xuqm.tenant.controller;
import com.xuqm.common.model.ApiResponse;
import com.xuqm.tenant.entity.FeatureServiceEntity;
import com.xuqm.tenant.repository.AppRepository;
import com.xuqm.tenant.repository.FeatureServiceRepository;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/api/sdk")
public class SdkConfigController {
private final AppRepository appRepository;
private final FeatureServiceRepository featureServiceRepository;
@Value("${sdk.im-ws-url:wss://dev.xuqinmin.com/ws/im}")
private String imWsUrl;
@Value("${sdk.file-service-url:https://dev.xuqinmin.com}")
private String fileServiceUrl;
@Value("${sdk.im-api-url:https://dev.xuqinmin.com}")
private String imApiUrl;
public SdkConfigController(AppRepository appRepository,
FeatureServiceRepository featureServiceRepository) {
this.appRepository = appRepository;
this.featureServiceRepository = featureServiceRepository;
}
/**
* GET /api/sdk/config?appId=XXX public, no auth required.
*
* Returns SDK configuration URLs and enabled feature flags for the given appId.
* Returns 404 if the appId does not exist in the system.
*/
@GetMapping("/config")
public ResponseEntity<ApiResponse<SdkConfigResponse>> getConfig(
@RequestParam String appId) {
if (!appRepository.existsById(appId)) {
return ResponseEntity.status(404)
.body(ApiResponse.error(404, "App not found: " + appId));
}
List<FeatureServiceEntity> features = featureServiceRepository.findByAppId(appId);
boolean imEnabled = features.stream()
.anyMatch(f -> f.getServiceType() == FeatureServiceEntity.ServiceType.IM && f.isEnabled());
boolean pushEnabled = features.stream()
.anyMatch(f -> f.getServiceType() == FeatureServiceEntity.ServiceType.PUSH && f.isEnabled());
boolean updateEnabled = features.stream()
.anyMatch(f -> f.getServiceType() == FeatureServiceEntity.ServiceType.UPDATE && f.isEnabled());
SdkConfigResponse response = new SdkConfigResponse(
imWsUrl,
fileServiceUrl,
imApiUrl,
Map.of(
"im", imEnabled,
"push", pushEnabled,
"update", updateEnabled
)
);
return ResponseEntity.ok(ApiResponse.success(response));
}
public record SdkConfigResponse(
String imWsUrl,
String fileServiceUrl,
String imApiUrl,
Map<String, Boolean> features
) {}
}