team api
This commit is contained in:
@@ -1,19 +0,0 @@
|
||||
package model
|
||||
|
||||
// 用户的token
|
||||
type Token struct {
|
||||
Id int64 `gorm:"column:id;primaryKey;autoIncrement"`
|
||||
UserId int64 `gorm:"column:user_id;not null;index:idx_token_user_id"`
|
||||
Name string `gorm:"column:name;index:idx_token_name"`
|
||||
Key string `gorm:"column:key;type:char(48);uniqueIndex:idx_token_key"`
|
||||
Status bool `gorm:"column:status;default:true"` // enabled 0, disabled 1
|
||||
Quota int64 `gorm:"column:quota;type:bigint;default:0"` // -1 means unlimited
|
||||
UnlimitedQuota bool `gorm:"column:unlimited_quota;default:true"`
|
||||
UsedQuota int64 `gorm:"column:used_quota;type:bigint;default:0"`
|
||||
CreatedTime int64 `gorm:"column:created_time;type:bigint"`
|
||||
ExpiredTime int64 `gorm:"column:expired_time;type:bigint;default:-1"` // -1 means never expired
|
||||
}
|
||||
|
||||
func (Token) TableName() string {
|
||||
return "token"
|
||||
}
|
||||
@@ -1,26 +1,22 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/lib/pq"
|
||||
)
|
||||
import "github.com/lib/pq" //pq.StringArray
|
||||
|
||||
type ApiKey_PG struct {
|
||||
ID int64 `gorm:"column:id;primaryKey;autoIncrement"`
|
||||
Name string `gorm:"column:name;not null;unique;index:idx_apikey_name"`
|
||||
ApiType string `gorm:"column:apitype;not null;unique;index:idx_apikey_apitype"`
|
||||
ApiKey string `gorm:"column:apikey;not null;unique;index:idx_apikey_apikey"`
|
||||
Status int `gorm:"type:int;default:0"` // enabled 0, disabled 1
|
||||
Endpoint string `gorm:"column:endpoint"`
|
||||
ResourceNmae string `gorm:"column:resource_name"`
|
||||
DeploymentName string `gorm:"column:deployment_name"`
|
||||
ApiKey string `gorm:"column:apikey;not null;unique;uniqueIndex:idx_apikey"`
|
||||
Status int `gorm:"type:int;default:1"` // enabled 1, disabled 0
|
||||
Endpoint string `gorm:"column:endpoint;comment:接入点"`
|
||||
ResourceNmae string `gorm:"column:resource_name;comment:azure资源名称"`
|
||||
DeploymentName string `gorm:"column:deployment_name;comment:azure部署名称"`
|
||||
ApiSecret string `gorm:"column:api_secret"`
|
||||
ModelPrefix string `gorm:"column:model_prefix"`
|
||||
ModelAlias string `gorm:"column:model_alias"`
|
||||
SupportModels pq.StringArray `gorm:"type:text[]"`
|
||||
UpdatedAt time.Time `json:"updatedAt,omitempty"`
|
||||
CreatedAt time.Time `json:"createdAt,omitempty"`
|
||||
ModelPrefix string `gorm:"column:model_prefix;comment:模型前缀"`
|
||||
ModelAlias string `gorm:"column:model_alias;comment:模型别名"`
|
||||
SupportModels pq.StringArray `gorm:"column:support_models;type:text[]"`
|
||||
CreatedAt int64 `gorm:"column:created_at;autoUpdateTime" json:"created_at,omitempty"`
|
||||
UpdatedAt int64 `gorm:"column:updated_at;autoCreateTime" json:"updated_at,omitempty"`
|
||||
}
|
||||
|
||||
func (ApiKey_PG) TableName() string {
|
||||
@@ -28,20 +24,20 @@ func (ApiKey_PG) TableName() string {
|
||||
}
|
||||
|
||||
type ApiKey struct {
|
||||
ID int64 `gorm:"column:id;primaryKey;autoIncrement"`
|
||||
Name string `gorm:"column:name;not null;unique;index:idx_apikey_name"`
|
||||
ApiType string `gorm:"column:apitype;not null;unique;index:idx_apikey_apitype"`
|
||||
ApiKey string `gorm:"column:apikey;not null;unique;index:idx_apikey_apikey"`
|
||||
Status int `json:"status" gorm:"type:int;default:0"` // enabled 0, disabled 1
|
||||
Endpoint string `gorm:"column:endpoint"`
|
||||
ResourceNmae string `gorm:"column:resource_name"`
|
||||
DeploymentName string `gorm:"column:deployment_name"`
|
||||
ApiSecret string `gorm:"column:api_secret"`
|
||||
ModelPrefix string `gorm:"column:model_prefix"`
|
||||
ModelAlias string `gorm:"column:model_alias"`
|
||||
SupportModels []string `gorm:"type:json"`
|
||||
CreatedAt time.Time `json:"created_at,omitempty" gorm:"autoUpdateTime"`
|
||||
UpdatedAt time.Time `json:"updated_at,omitempty" gorm:"autoCreateTime"`
|
||||
ID int64 `gorm:"column:id;primaryKey;autoIncrement"`
|
||||
Name string `gorm:"column:name;not null;unique;index:idx_apikey_name"`
|
||||
ApiType string `gorm:"column:apitype;not null;unique;index:idx_apikey_apitype"`
|
||||
ApiKey string `gorm:"column:apikey;not null;unique;index:idx_apikey_apikey"`
|
||||
Status int `gorm:"type:int;default:1"` // enabled 1, disabled 0
|
||||
Endpoint string `gorm:"column:endpoint"`
|
||||
ResourceNmae string `gorm:"column:resource_name"`
|
||||
DeploymentName string `gorm:"column:deployment_name"`
|
||||
ApiSecret string `gorm:"column:api_secret"`
|
||||
ModelPrefix string `gorm:"column:model_prefix"`
|
||||
ModelAlias string `gorm:"column:model_alias"`
|
||||
SupportModels []string `gorm:"column:support_models;type:json"`
|
||||
CreatedAt int64 `gorm:"column:created_at;autoUpdateTime" json:"created_at,omitempty"`
|
||||
UpdatedAt int64 `gorm:"column:updated_at;autoCreateTime" json:"updated_at,omitempty"`
|
||||
}
|
||||
|
||||
func (ApiKey) TableName() string {
|
||||
|
||||
20
team/model/token.go
Normal file
20
team/model/token.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package model
|
||||
|
||||
// 用户的token
|
||||
type Token struct {
|
||||
ID int64 `gorm:"column:id;primaryKey;autoIncrement"`
|
||||
UserID int64 `gorm:"column:user_id;not null;index:idx_token_user_id"`
|
||||
Name string `gorm:"column:name;index:idx_token_name"`
|
||||
Key string `gorm:"column:key;not null;uniqueIndex:idx_token_key;comment:token key"`
|
||||
Status int64 `gorm:"column:status;default:1;check:status IN (0,1)"` // enabled 1, disabled 0
|
||||
Quota int64 `gorm:"column:quota;type:bigint;default:0"` // default 0
|
||||
UnlimitedQuota bool `gorm:"column:unlimited_quota;default:true"` // set Quota 1 unlimited
|
||||
UsedQuota int64 `gorm:"column:used_quota;type:bigint;default:0"`
|
||||
CreatedAt int64 `gorm:"column:created_at;type:bigint;autoCreateTime"`
|
||||
ExpiredAt int64 `gorm:"column:expired_at;type:bigint;default:-1"` // -1 means never expired
|
||||
User User `gorm:"foreignKey:UserID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE" json:"user"`
|
||||
}
|
||||
|
||||
func (Token) TableName() string {
|
||||
return "tokens"
|
||||
}
|
||||
@@ -8,40 +8,42 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type DailyUsage struct {
|
||||
type Usage struct {
|
||||
ID int64 `gorm:"column:id;primaryKey;autoIncrement"`
|
||||
UserID int64 `gorm:"column:user_id;index:idx_user_id"`
|
||||
TokenId int64 `gorm:"column:token_id;index:idx_token_id"`
|
||||
Capability string `gorm:"column:capability;index:idx_capability;comment:模型能力"`
|
||||
TokenID int64 `gorm:"column:token_id;index:idx_token_id"`
|
||||
Capability string `gorm:"column:capability;index:idx_usage_capability;comment:模型能力"`
|
||||
Date time.Time `gorm:"column:date;autoCreateTime;index:idx_date"`
|
||||
Model string `gorm:"column:model"`
|
||||
Stream bool `gorm:"column:stream"`
|
||||
PromptTokens int `gorm:"column:prompt_tokens"`
|
||||
CompletionTokens int `gorm:"column:completion_tokens"`
|
||||
TotalTokens int `gorm:"column:total_tokens"`
|
||||
PromptTokens float64 `gorm:"column:prompt_tokens"`
|
||||
CompletionTokens float64 `gorm:"column:completion_tokens"`
|
||||
TotalTokens float64 `gorm:"column:total_tokens"`
|
||||
Cost string `gorm:"column:cost"`
|
||||
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime"`
|
||||
}
|
||||
|
||||
func (DailyUsage) TableName() string {
|
||||
return "daily_usages"
|
||||
}
|
||||
|
||||
type Usage struct {
|
||||
ID int `gorm:"column:id"`
|
||||
UserID int `gorm:"column:user_id"`
|
||||
SKU string `gorm:"column:sku"`
|
||||
PromptUnits int `gorm:"column:prompt_units"`
|
||||
CompletionUnits int `gorm:"column:completion_units"`
|
||||
TotalUnit int `gorm:"column:total_unit"`
|
||||
Cost string `gorm:"column:cost"`
|
||||
Date time.Time `gorm:"column:date"`
|
||||
}
|
||||
|
||||
func (Usage) TableName() string {
|
||||
return "usages"
|
||||
}
|
||||
|
||||
type DailyUsage struct {
|
||||
ID int64 `gorm:"column:id;primaryKey;autoIncrement"`
|
||||
UserID int64 `gorm:"column:user_id;uniqueIndex:idx_daily_unique,priority:1"`
|
||||
TokenID int64 `gorm:"column:token_id;index:idx_daily_token_id"`
|
||||
Capability string `gorm:"column:capability;uniqueIndex:idx_daily_unique,priority:2;comment:模型能力"`
|
||||
Date time.Time `gorm:"column:date;autoCreateTime;uniqueIndex:idx_daily_unique,priority:3"`
|
||||
Model string `gorm:"column:model"`
|
||||
Stream bool `gorm:"column:stream"`
|
||||
PromptTokens float64 `gorm:"column:prompt_tokens"`
|
||||
CompletionTokens float64 `gorm:"column:completion_tokens"`
|
||||
TotalTokens float64 `gorm:"column:total_tokens"`
|
||||
Cost string `gorm:"column:cost"`
|
||||
}
|
||||
|
||||
func (DailyUsage) TableName() string {
|
||||
return "daily_usages"
|
||||
}
|
||||
|
||||
func HandleUsage(c *gin.Context) {
|
||||
fromStr := c.Query("from")
|
||||
toStr := c.Query("to")
|
||||
|
||||
@@ -1,99 +1,34 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"opencatd-open/store"
|
||||
"time"
|
||||
|
||||
"github.com/Sakurasan/to"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID int64 `json:"id" gorm:"primaryKey;autoIncrement"`
|
||||
Name string `json:"name" gorm:"unique;index" validate:"max=12"`
|
||||
UserName string `json:"username" gorm:"unique;index" validate:"max=12"`
|
||||
Password string `json:"password" gorm:"not null;" validate:"min=8,max=20"`
|
||||
Role int `json:"role" gorm:"type:int;default:1"` // default user
|
||||
Status int `json:"status" gorm:"type:int;default:0"` // enabled 0, disabled 1 deleted 2
|
||||
Nickname string `json:"nickname" gorm:"type:varchar(50)"`
|
||||
AvatarURL string `json:"avatar_url" gorm:"type:varchar(255)"`
|
||||
Email string `json:"email" gorm:"type:varchar(255);unique;index"`
|
||||
Quota int64 `json:"quota" gorm:"bigint;default:-1"` // default unlimited
|
||||
Token string `json:"token,omitempty"`
|
||||
Timezone string `json:"timezone" gorm:"type:varchar(50)"`
|
||||
Language string `json:"language" gorm:"type:varchar(50)"`
|
||||
ID int64 `json:"id" gorm:"column:id;primaryKey;autoIncrement"`
|
||||
Name string `json:"name" gorm:"column:name;not null;unique;index"`
|
||||
Username string `json:"username" gorm:"column:username;unique;index"`
|
||||
Password string `json:"password" gorm:"column:password;"`
|
||||
Role int `json:"role" gorm:"column:role;type:int;default:0"` // default user 0-10-20
|
||||
Status int `json:"status" gorm:"column:status;type:int;default:1"` // disabled 0, enabled 1, deleted 2
|
||||
Nickname string `json:"nickname" gorm:"column:nickname;type:varchar(50)"`
|
||||
AvatarURL string `json:"avatar_url" gorm:"column:avatar_url;type:varchar(255)"`
|
||||
Email string `json:"email" gorm:"column:email;type:varchar(255);index"`
|
||||
Quota int64 `json:"quota" gorm:"column:quota;bigint;default:0"` // default unlimited
|
||||
UnlimitedQuota int `json:"unlimited_quota" gorm:"column:unlimited_quota;default:1;check:(unlimited_quota IN (0,1))"` // 0 limited , 1 unlimited
|
||||
Timezone string `json:"timezone" gorm:"column:timezone;type:varchar(50)"`
|
||||
Language string `json:"language" gorm:"column:language;type:varchar(50)"`
|
||||
|
||||
CreatedAt time.Time `json:"created_at,omitempty" gorm:"autoCreateTime"`
|
||||
UpdatedAt time.Time `json:"updated_at,omitempty" gorm:"autoUpdateTime"`
|
||||
// 添加一对多关系
|
||||
// Token string `json:"-" gorm:"column:token;type:varchar(64);unique;index"`
|
||||
Tokens []Token `json:"-" gorm:"foreignKey:UserID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
|
||||
|
||||
CreatedAt int64 `json:"created_at,omitempty" gorm:"autoCreateTime"`
|
||||
UpdatedAt int64 `json:"updated_at,omitempty" gorm:"autoUpdateTime"`
|
||||
}
|
||||
|
||||
func HandleUsers(c *gin.Context) {
|
||||
users, err := store.GetAllUsers()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, users)
|
||||
}
|
||||
|
||||
func HandleAddUser(c *gin.Context) {
|
||||
var body User
|
||||
if err := c.BindJSON(&body); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if len(body.Name) == 0 {
|
||||
c.JSON(http.StatusOK, gin.H{"error": "invalid user name"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := store.AddUser(body.Name, uuid.NewString()); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
u, err := store.GetUserByName(body.Name)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, u)
|
||||
}
|
||||
|
||||
func HandleDelUser(c *gin.Context) {
|
||||
id := to.Int(c.Param("id"))
|
||||
if id <= 1 {
|
||||
c.JSON(http.StatusOK, gin.H{"error": "invalid user id"})
|
||||
return
|
||||
}
|
||||
if err := store.DeleteUser(uint(id)); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "ok"})
|
||||
}
|
||||
|
||||
func HandleResetUserToken(c *gin.Context) {
|
||||
id := to.Int(c.Param("id"))
|
||||
newtoken := c.Query("token")
|
||||
if newtoken == "" {
|
||||
newtoken = uuid.NewString()
|
||||
}
|
||||
|
||||
if err := store.UpdateUser(uint(id), newtoken); err != nil {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
u, err := store.GetUserByID(uint(id))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, u)
|
||||
func (User) TableName() string {
|
||||
return "users"
|
||||
}
|
||||
|
||||
type Session struct {
|
||||
@@ -104,8 +39,9 @@ type Session struct {
|
||||
DeviceName string `json:"device_name" gorm:"type:varchar(100);default:''"`
|
||||
LastActiveAt time.Time `json:"last_active_at" gorm:"type:timestamp;default:CURRENT_TIMESTAMP"`
|
||||
LogoutAt time.Time `json:"logout_at" gorm:"type:timestamp;null"`
|
||||
CreatedAt time.Time `json:"created_at" gorm:"type:timestamp;not null;default:CURRENT_TIMESTAMP"`
|
||||
UpdatedAt time.Time `json:"updated_at" gorm:"type:timestamp;not null;default:CURRENT_TIMESTAMP;update:CURRENT_TIMESTAMP"`
|
||||
|
||||
CreatedAt time.Time `json:"created_at" gorm:"type:timestamp;not null;default:CURRENT_TIMESTAMP"`
|
||||
UpdatedAt time.Time `json:"updated_at" gorm:"type:timestamp;not null;default:CURRENT_TIMESTAMP;update:CURRENT_TIMESTAMP"`
|
||||
}
|
||||
|
||||
func (Session) TableName() string {
|
||||
|
||||
Reference in New Issue
Block a user