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.

127 lines
4.0 KiB

3 years ago
package main
import (
3 years ago
"api2gpt-mid/common"
"api2gpt-mid/middleware"
"api2gpt-mid/model"
"api2gpt-mid/router"
3 years ago
"encoding/json"
"github.com/gin-gonic/gin"
3 years ago
"os"
3 years ago
"strconv"
3 years ago
)
3 years ago
func test_redis() {
3 years ago
//添加reids测试数据
var serverInfo model.ServerInfo = model.ServerInfo{
ServerAddress: "https://gptp.any-door.cn",
AvailableKey: "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",
}
3 years ago
// var serverInfo2 ServerInfo = ServerInfo{
// ServerAddress: "https://azure.any-door.cn",
// AvailableKey: "6c4d2c65970b40e482e7cd27adb0d119",
// }
serverInfoStr, _ := json.Marshal(&serverInfo)
3 years ago
// serverInfoStr2, _ := json.Marshal(&serverInfo2)
common.RedisSet("server:0", string(serverInfoStr), 0)
common.RedisSet("server:1", string(serverInfoStr), 0)
3 years ago
// Redis.Set(context.Background(), "server:2", serverInfoStr2, 0)
// var modelInfo ModelInfo = ModelInfo{
// ModelName: "gpt-3.5-turbo-0613",
3 years ago
// ModelPrice: 0.0001,
// ModelPrepayment: 4000,
// }
// modelInfoStr, _ := json.Marshal(&modelInfo)
// Redis.Set(context.Background(), "model:gpt-3.5-turbo-0613", modelInfoStr, 0)
// 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)
3 years ago
// 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)
3 years ago
var userInfo model.UserInfo = model.UserInfo{
3 years ago
UID: "0",
SID: "1",
}
3 years ago
// var userInfo2 UserInfo = UserInfo{
// UID: "2",
// SID: "2",
// }
3 years ago
userInfoStr, _ := json.Marshal(&userInfo)
3 years ago
// userInfoStr2, _ := json.Marshal(&userInfo2)
3 years ago
common.RedisSet("user:key0", string(userInfoStr), 0)
//Redis.Set(context.Background(), "user:key0", userInfoStr, 0)
3 years ago
// Redis.Set(context.Background(), "user:AK-7d8ab782-a152-4cc1-9972-568713465c96", userInfoStr2, 0)
3 years ago
common.RedisIncrByFloat("user:0:balance", 1000)
//Redis.IncrByFloat(context.Background(), "user:0:balance", 1000).Result()
3 years ago
// Redis.IncrByFloat(context.Background(), "user:2:balance", 1000).Result()
3 years ago
}
func main() {
3 years ago
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")
}
3 years ago
3 years ago
// Initialize Redis
err := common.InitRedisClient()
if err != nil {
common.FatalLog("failed to initialize Redis: " + err.Error())
}
3 years ago
3 years ago
// Initialize HTTP server
server := gin.Default()
server.Use(middleware.CORS())
3 years ago
router.SetRouter(server)
3 years ago
//添加测试数据
test_redis()
3 years ago
3 years ago
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())
}
3 years ago
}