This commit is contained in:
coward
2024-07-05 14:41:35 +08:00
commit 1f7f57ec9f
70 changed files with 4923 additions and 0 deletions

67
model/base.go Normal file
View File

@@ -0,0 +1,67 @@
package model
import (
"database/sql/driver"
"fmt"
"github.com/google/uuid"
"gorm.io/gorm"
"strings"
"time"
)
// Base
// @description: 数据模型基类
type Base struct {
Id string `json:"id" gorm:"primaryKey;type:varchar(36);not null;comment:'主键'"`
Timestamp
}
func (b *Base) BeforeCreate(*gorm.DB) (err error) {
if b.Id == "" {
b.Id = strings.ReplaceAll(uuid.NewString(), "-", "")
}
return
}
type JsonTime struct {
time.Time
}
type Timestamp struct {
CreatedAt JsonTime
UpdatedAt JsonTime
}
func (jt JsonTime) MarshalJSON() ([]byte, error) {
if jt.IsZero() {
return []byte(`""`), nil
}
output := fmt.Sprintf("\"%s\"", jt.Format("2006-01-02 15:04:05"))
return []byte(output), nil
}
func (jt JsonTime) Value() (driver.Value, error) {
var zeroTime time.Time
if jt.Time.UnixNano() == zeroTime.UnixNano() {
return nil, nil
}
return jt.Time.Format("2006-01-02 15:04:05"), nil
}
func (jt *JsonTime) Scan(v any) error {
value, ok := v.(time.Time)
if ok {
*jt = JsonTime{Time: value}
return nil
}
return fmt.Errorf("can not convert %v to timestamp", v)
}
func (jt JsonTime) String() string {
if jt.IsZero() {
return ""
}
output := fmt.Sprintf("%s", jt.Format("2006-01-02 15:04:05"))
return output
}

18
model/other.go Normal file
View File

@@ -0,0 +1,18 @@
package model
type RequestLog struct {
Base
UserId string `json:"userId" gorm:"type:char(40);comment:'用户id'"`
ClientIP string `json:"clientIP" gorm:"type:varchar(60);not null;comment:'客户端IP'"`
Host string `json:"host" gorm:"type:varchar(255);not null;comment:'请求域名'"`
Method string `json:"method" gorm:"type:varchar(20);not null;comment:'请求方法[GET POST DELETE PUT PATCH]'"`
Uri string `json:"uri" gorm:"type:varchar(255);not null;comment:'请求path'"`
Header string `json:"header" gorm:"type:text;comment:'请求头'"`
Body string `json:"body" gorm:"type:text;comment:'请求体'"`
Form string `json:"form" gorm:"type:text;comment:'请求表单'"`
Query string `json:"query" gorm:"type:text;comment:'请求query'"`
UserAgent string `json:"userAgent" gorm:"type:text;comment:'ua信息'"`
Cost int64 `json:"cost" gorm:"type:int(10);comment:'请求耗时'"`
StatusCode int `json:"statusCode" gorm:"type:int(10);comment:'响应状态码'"`
Response string `json:"response" gorm:"type:text;comment:'返回数据'"`
}

12
model/setting.go Normal file
View File

@@ -0,0 +1,12 @@
package model
type Setting struct {
Base
Code string `json:"code" gorm:"type:char(20);not null;index:idx_code; comment:'设定code'"`
Data string `json:"render_data" gorm:"type:text;not null; comment:'值'"`
Describe string `json:"describe" gorm:"type:text;default null;comment:'配置说明'"`
}
func (Setting) TableName() string {
return "t_setting"
}

18
model/user.go Normal file
View File

@@ -0,0 +1,18 @@
package model
import "wireguard-ui/global/constant"
type User struct {
Base
Account string `json:"account" gorm:"type:varchar(50);not null;index:idx_account;comment: '登陆账号'"`
Password string `json:"password" gorm:"type:varchar(255);not null;comment: '密码'"`
Nickname string `json:"nickname" gorm:"type:varchar(50);not null;comment: '昵称'"`
Avatar string `json:"avatar" gorm:"type:varchar(255);not null;comment: '头像'"`
Contact string `json:"contact" gorm:"type:varchar(255);default null;comment: '联系方式(邮箱|电话)'"`
IsAdmin constant.UserType `json:"isAdmin" gorm:"type:tinyint(1);not null;comment: '是否为管理员0 - 否 | 1 - 是)'"`
Status constant.Status `json:"status" gorm:"type:tinyint(1);not null;comment: '用户状态0 - 否 | 1 - 是)'"`
}
func (User) TableName() string {
return "t_user"
}

24
model/wireguard.go Normal file
View File

@@ -0,0 +1,24 @@
package model
import "wireguard-ui/global/constant"
type Client struct {
Base
Name string `json:"name" gorm:"type:varchar(100);not null;comment:'客户端名称'"`
Email string `json:"email" gorm:"type:varchar(100);default null;comment:'联系邮箱'"`
SubnetRange string `json:"subnetRange" gorm:"type:varchar(255);default null;comment:'子网范围'"`
IpAllocation string `json:"ipAllocation" gorm:"type:varchar(255);not null;comment:'客户端ip'"`
AllowedIps string `json:"allowedIps" gorm:"type:varchar(255);not null;comment:'允许访问的ip'"`
ExtraAllowedIps string `json:"extraAllowedIps" gorm:"type:varchar(255);default null;comment:'额外允许的ip范围'"`
Endpoint string `json:"endpoint" gorm:"type:varchar(255);default null;comment:'端点'"`
UseServerDns constant.Status `json:"useServerDns" gorm:"type:tinyint(1);default 1;comment:'是否使用服务端dns'"`
Keys string `json:"keys" gorm:"type:text;default null;comment:'公钥和密钥的json串'"`
UserId string `json:"userId" gorm:"type:char(36);not null;comment:'创建人id'"`
Enabled constant.Status `json:"enabled" gorm:"type:tinyint(1);default 1;comment:'状态0 - 禁用 | 1 - 正常)'"`
OfflineMonitoring constant.Status `json:"offlineMonitoring" gorm:"tinyint(1);default 0;comment:'是否启用离线监听0 - 禁用 | 1 - 启用)"`
User *User `json:"user" gorm:"foreignKey:UserId"`
}
func (Client) TableName() string {
return "t_client"
}