Complete docking with the gateway

This commit is contained in:
JiaJu Zhuang
2023-07-09 19:18:14 +08:00
parent ab7680b9e0
commit d0cdb420c0
14 changed files with 301 additions and 17 deletions

View File

@ -50,6 +50,12 @@
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<!-- http -->
<dependency>
<groupId>com.dtflys.forest</groupId>
<artifactId>forest-spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -6,7 +6,7 @@ package ai.chat2db.server.web.api.controller;
import ai.chat2db.server.domain.core.cache.CacheManage;
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
import ai.chat2db.server.tools.common.config.AliDbhubProperties;
import ai.chat2db.server.tools.common.config.Chat2dbProperties;
import ai.chat2db.server.tools.common.util.I18nUtils;
import ai.chat2db.spi.ssh.SSHManager;
import lombok.extern.slf4j.Slf4j;
@ -31,7 +31,7 @@ public class SystemController {
private ApplicationContext applicationContext;
@Autowired
private AliDbhubProperties aliDbhubProperties;
private Chat2dbProperties chat2dbProperties;
/**
* 检测是否成功
@ -53,7 +53,7 @@ public class SystemController {
*/
@GetMapping("/get-version-a")
public DataResult<String> getVersion() {
return DataResult.of(aliDbhubProperties.getVersion());
return DataResult.of(chat2dbProperties.getVersion());
}
/**

View File

@ -0,0 +1,79 @@
package ai.chat2db.server.web.api.controller.ai;
import ai.chat2db.server.domain.api.param.SystemConfigParam;
import ai.chat2db.server.domain.api.service.ConfigService;
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
import ai.chat2db.server.tools.common.config.Chat2dbProperties;
import ai.chat2db.server.web.api.aspect.ConnectionInfoAspect;
import ai.chat2db.server.web.api.http.GatewayClientServiceV2;
import ai.chat2db.server.web.api.http.response.ApiKeyResponse;
import ai.chat2db.server.web.api.http.response.QrCodeResponse;
import ai.chat2db.server.web.api.util.OpenAIClient;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* AI configuration information interface
*
* @author Jiaju Zhuang
*/
@RestController
@ConnectionInfoAspect
@RequestMapping("/api/ai/config")
@Slf4j
public class AiConfigController {
//@Autowired
//private GatewayClientService gatewayClientService;
@Autowired
private GatewayClientServiceV2 gatewayClientService;
@Autowired
private ConfigService configService;
@Resource
private Chat2dbProperties chat2dbProperties;
/**
* AI configuration information interface
*
* @param token
* @return
*/
@GetMapping("/getQrCode")
public DataResult<QrCodeResponse> getQrCode(@RequestParam(required = false) String token) {
DataResult<QrCodeResponse> dataResult = gatewayClientService.getQrCode(token);
QrCodeResponse qrCodeResponse = dataResult.getData();
// Representative successfully logged in
if (StringUtils.isNotBlank(qrCodeResponse.getApiKey())) {
SystemConfigParam param = SystemConfigParam.builder()
.code(OpenAIClient.OPENAI_KEY).content(qrCodeResponse.getApiKey())
.build();
configService.createOrUpdate(param);
SystemConfigParam hostParam = SystemConfigParam.builder()
.code(OpenAIClient.OPENAI_HOST)
.content(chat2dbProperties.getGateway().getBaseUrl() + "/model")
.build();
configService.createOrUpdate(hostParam);
}
return dataResult;
}
/**
* Return remaining times
*
* @param key
* @return
*/
@GetMapping("/remaininguses/{key}")
public DataResult<ApiKeyResponse> remaininguses(@PathVariable String key) {
return gatewayClientService.remaininguses(key);
}
}

View File

@ -0,0 +1,38 @@
package ai.chat2db.server.web.api.http;
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
import ai.chat2db.server.web.api.http.response.ApiKeyResponse;
import ai.chat2db.server.web.api.http.response.QrCodeResponse;
import com.dtflys.forest.annotation.BaseRequest;
import com.dtflys.forest.annotation.Get;
import com.dtflys.forest.annotation.Query;
import com.dtflys.forest.annotation.Var;
/**
* Gateway 的http 服务
*
* @author Jiaju Zhuang
*/
@BaseRequest(
baseURL = "{gatewayBaseUrl}"
)
public interface GatewayClientService {
/**
* 获取公众号的二维码
*
* @param token
* @return
*/
@Get("/api/client/getQrCode")
DataResult<QrCodeResponse> getQrCode(@Query("token") String token);
/**
* 返回剩余次数
*
* @param key
* @return
*/
@Get("/api/client/remaininguses/{key}")
DataResult<ApiKeyResponse> remaininguses(@Var("key") String key);
}

View File

@ -0,0 +1,50 @@
package ai.chat2db.server.web.api.http;
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
import ai.chat2db.server.tools.common.config.Chat2dbProperties;
import ai.chat2db.server.web.api.http.response.ApiKeyResponse;
import ai.chat2db.server.web.api.http.response.QrCodeResponse;
import com.dtflys.forest.Forest;
import com.dtflys.forest.annotation.Get;
import com.dtflys.forest.annotation.Query;
import com.dtflys.forest.annotation.Var;
import com.dtflys.forest.utils.TypeReference;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
/**
* Gateway 的http 服务
*
* @author Jiaju Zhuang
*/
@Component
public class GatewayClientServiceV2 {
@Resource
private Chat2dbProperties chat2dbProperties;
/**
* 获取公众号的二维码
*
* @param token
* @return
*/
@Get("/api/client/getQrCode")
public DataResult<QrCodeResponse> getQrCode(@Query("token") String token) {
return Forest.get(chat2dbProperties.getGateway().getBaseUrl() + "/api/client/getQrCode")
.addQuery("token", token)
.execute(new TypeReference<>() {});
}
/**
* 返回剩余次数
*
* @param key
* @return
*/
@Get("/api/client/remaininguses/{key}")
public DataResult<ApiKeyResponse> remaininguses(@Var("key") String key) {
return Forest.get(chat2dbProperties.getGateway().getBaseUrl() + "/api/client/remaininguses/" + key)
.execute(new TypeReference<>() {});
}
}

View File

@ -0,0 +1,43 @@
package ai.chat2db.server.web.api.http.response;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
/**
* apikey
*
* @author Jiaju Zhuang
*/
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class ApiKeyResponse {
/**
* key
*/
private String key;
/**
* Expiration time
*/
private Long expiry;
/**
* Number of uses
*/
private Long remainingUses;
/**
* WeChat official account url
*/
private String wechatMpUrl;
/**
* https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET
*/
private String wechatQrCodeUrl;
}

View File

@ -0,0 +1,24 @@
package ai.chat2db.server.web.api.http.response;
import lombok.Data;
@Data
public class QrCodeResponse {
/**
* When logging in for the first time, the token will be returned, and subsequent services need to poll the token to
* check if the user is logged in
*/
private String token;
/**
* Return the QR code used by the user to log in
*/
private String wechatQrCodeUrl;
/**
* If the user logs in successfully, it will return apiKey. If the front-end detects the presence of apiKey, it
* indicates successful login
*/
private String apiKey;
}