refact:openteam
This commit is contained in:
19
team/model/Token.go
Normal file
19
team/model/Token.go
Normal file
@@ -0,0 +1,19 @@
|
||||
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"
|
||||
}
|
||||
49
team/model/apikey.go
Normal file
49
team/model/apikey.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/lib/pq"
|
||||
)
|
||||
|
||||
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"`
|
||||
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"`
|
||||
}
|
||||
|
||||
func (ApiKey_PG) TableName() string {
|
||||
return "apikeys"
|
||||
}
|
||||
|
||||
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"`
|
||||
}
|
||||
|
||||
func (ApiKey) TableName() string {
|
||||
return "apikeys"
|
||||
}
|
||||
72
team/model/usage.go
Normal file
72
team/model/usage.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"opencatd-open/store"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type DailyUsage 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:模型能力"`
|
||||
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"`
|
||||
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"
|
||||
}
|
||||
|
||||
func HandleUsage(c *gin.Context) {
|
||||
fromStr := c.Query("from")
|
||||
toStr := c.Query("to")
|
||||
getMonthStartAndEnd := func() (start, end string) {
|
||||
loc, _ := time.LoadLocation("Local")
|
||||
now := time.Now().In(loc)
|
||||
|
||||
year, month, _ := now.Date()
|
||||
|
||||
startOfMonth := time.Date(year, month, 1, 0, 0, 0, 0, loc)
|
||||
endOfMonth := startOfMonth.AddDate(0, 1, 0)
|
||||
|
||||
start = startOfMonth.Format("2006-01-02")
|
||||
end = endOfMonth.Format("2006-01-02")
|
||||
return
|
||||
}
|
||||
if fromStr == "" || toStr == "" {
|
||||
fromStr, toStr = getMonthStartAndEnd()
|
||||
}
|
||||
|
||||
usage, err := store.QueryUsage(fromStr, toStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, usage)
|
||||
}
|
||||
113
team/model/user.go
Normal file
113
team/model/user.go
Normal file
@@ -0,0 +1,113 @@
|
||||
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)"`
|
||||
|
||||
CreatedAt time.Time `json:"created_at,omitempty" gorm:"autoCreateTime"`
|
||||
UpdatedAt time.Time `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)
|
||||
}
|
||||
|
||||
type Session struct {
|
||||
ID int64 `json:"id" gorm:"primaryKey;autoIncrement"`
|
||||
UserID int64 `json:"user_id" gorm:"index:idx_user_id"`
|
||||
Token string `json:"token" gorm:"type:varchar(64);uniqueIndex"`
|
||||
DeviceType string `json:"device_type" gorm:"type:varchar(100);default:''"`
|
||||
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"`
|
||||
}
|
||||
|
||||
func (Session) TableName() string {
|
||||
return "sessions"
|
||||
}
|
||||
Reference in New Issue
Block a user