You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
142 lines
4.1 KiB
142 lines
4.1 KiB
package main
|
|
|
|
import (
|
|
"api2gpt-mid/common"
|
|
"api2gpt-mid/middleware"
|
|
"api2gpt-mid/model"
|
|
"api2gpt-mid/router"
|
|
"encoding/json"
|
|
"github.com/gin-gonic/gin"
|
|
"os"
|
|
"strconv"
|
|
)
|
|
|
|
func test_redis() {
|
|
//添加reids测试数据
|
|
var serverInfo model.ServerInfo = model.ServerInfo{
|
|
ServerAddress: "https://gptp.any-door.cn",
|
|
AvailableKey: "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",
|
|
}
|
|
// var serverInfo2 ServerInfo = ServerInfo{
|
|
// ServerAddress: "https://azure.any-door.cn",
|
|
// AvailableKey: "6c4d2c65970b40e482e7cd27adb0d119",
|
|
// }
|
|
serverInfoStr, _ := json.Marshal(&serverInfo)
|
|
// serverInfoStr2, _ := json.Marshal(&serverInfo2)
|
|
err := common.RedisSet("server:0", string(serverInfoStr), 0)
|
|
if err != nil {
|
|
return
|
|
}
|
|
err = common.RedisSet("server:1", string(serverInfoStr), 0)
|
|
if err != nil {
|
|
return
|
|
}
|
|
// Redis.Set(context.Background(), "server:2", serverInfoStr2, 0)
|
|
|
|
var modelInfo model.ModelInfo = model.ModelInfo{
|
|
ModelName: "images-generations",
|
|
ModelPrice: 0.0001,
|
|
ModelPrepayment: 4000,
|
|
}
|
|
modelInfoStr, _ := json.Marshal(&modelInfo)
|
|
err = common.RedisSet("model:images-generations", string(modelInfoStr), 0)
|
|
if err != nil {
|
|
return
|
|
}
|
|
// var modelInfo1 ModelInfo = ModelInfo{
|
|
// ModelName: "gpt-3.5-turbo-16k",
|
|
// ModelPrice: 0.0001,
|
|
// ModelPrepayment: 4000,
|
|
// }
|
|
// modelInfoStr1, _ := json.Marshal(&modelInfo1)
|
|
// Redis.Set(context.Background(), "model:gpt-3.5-turbo-16k", modelInfoStr1, 0)
|
|
// var modelInfo2 ModelInfo = ModelInfo{
|
|
// ModelName: "gpt-3.5-turbo-16k-061",
|
|
// ModelPrice: 0.0001,
|
|
// ModelPrepayment: 4000,
|
|
// }
|
|
// modelInfoStr2, _ := json.Marshal(&modelInfo2)
|
|
// Redis.Set(context.Background(), "model:gpt-3.5-turbo-16k-0613", modelInfoStr2, 0)
|
|
// var modelInfo2 ModelInfo = ModelInfo{
|
|
// ModelName: "text-davinci-003",
|
|
// ModelPrice: 0.001,
|
|
// ModelPrepayment: 4000,
|
|
// }
|
|
// modelInfoStr2, _ := json.Marshal(&modelInfo2)
|
|
// var modelInfo3 ModelInfo = ModelInfo{
|
|
// ModelName: "text-davinci-003",
|
|
// ModelPrice: 0.001,
|
|
// ModelPrepayment: 4000,
|
|
// }
|
|
// modelInfoStr3, _ := json.Marshal(&modelInfo3)
|
|
// Redis.Set(context.Background(), "model:gpt-3.5-turbo", modelInfoStr, 0)
|
|
// Redis.Set(context.Background(), "model:gpt-3.5-turbo-0301", modelInfoStr, 0)
|
|
// Redis.Set(context.Background(), "model:text-davinci-003", modelInfoStr2, 0)
|
|
// Redis.Set(context.Background(), "model:text-embedding-ada-002", modelInfoStr3, 0)
|
|
// var modelInfo2 ModelInfo = ModelInfo{
|
|
// ModelName: "images-generations",
|
|
// ModelPrice: 0.01,
|
|
// ModelPrepayment: 1000,
|
|
// }
|
|
// modelInfoStr2, _ := json.Marshal(&modelInfo2)
|
|
// Redis.Set(context.Background(), "model:images-generations", modelInfoStr2, 0)
|
|
|
|
var userInfo model.UserInfo = model.UserInfo{
|
|
UID: "0",
|
|
SID: "1",
|
|
}
|
|
// var userInfo2 UserInfo = UserInfo{
|
|
// UID: "2",
|
|
// SID: "2",
|
|
// }
|
|
userInfoStr, _ := json.Marshal(&userInfo)
|
|
// userInfoStr2, _ := json.Marshal(&userInfo2)
|
|
err = common.RedisSet("user:key0", string(userInfoStr), 0)
|
|
if err != nil {
|
|
return
|
|
}
|
|
//Redis.Set(context.Background(), "user:key0", userInfoStr, 0)
|
|
// Redis.Set(context.Background(), "user:AK-7d8ab782-a152-4cc1-9972-568713465c96", userInfoStr2, 0)
|
|
_, err = common.RedisIncrByFloat("user:0:balance", 1000)
|
|
if err != nil {
|
|
return
|
|
}
|
|
//Redis.IncrByFloat(context.Background(), "user:0:balance", 1000).Result()
|
|
// Redis.IncrByFloat(context.Background(), "user:2:balance", 1000).Result()
|
|
}
|
|
|
|
func main() {
|
|
common.SetupGinLog()
|
|
common.SysLog("Api2gpt Mid Service " + common.Version + " started")
|
|
if os.Getenv("GIN_MODE") != "debug" {
|
|
gin.SetMode(gin.ReleaseMode)
|
|
}
|
|
if common.DebugEnabled {
|
|
common.SysLog("running in debug mode")
|
|
}
|
|
|
|
// Initialize Redis
|
|
err := common.InitRedisClient()
|
|
if err != nil {
|
|
common.FatalLog("failed to initialize Redis: " + err.Error())
|
|
}
|
|
|
|
// Initialize HTTP server
|
|
server := gin.Default()
|
|
server.Use(middleware.CORS())
|
|
|
|
router.SetRouter(server)
|
|
|
|
//添加测试数据
|
|
//test_redis()
|
|
|
|
var port = os.Getenv("PORT")
|
|
if port == "" {
|
|
port = strconv.Itoa(*common.Port)
|
|
}
|
|
err = server.Run(":" + port)
|
|
if err != nil {
|
|
common.FatalLog("failed to start HTTP server: " + err.Error())
|
|
}
|
|
}
|