建立仓库

This commit is contained in:
2025-08-05 10:51:35 +08:00
commit 4bbc05fe1f
36 changed files with 1946 additions and 0 deletions

20
model/city.go Normal file
View File

@@ -0,0 +1,20 @@
package model
type City struct {
Id int `gorm:"column:id;primaryKey"`
Macaddr string `gorm:"column:macaddr"`
Name string `gorm:"column:city"`
Num int `gorm:"column:num"`
Hash string `gorm:"column:hash"`
Label string `gorm:"column:label"`
Count int `gorm:"column:count"`
Offset int `gorm:"column:offset"`
CreateTime string `gorm:"column:createtime"`
UpdateTime string `gorm:"column:updatetime"`
EdgesCount int // 用于查询时统计边缘节点数量
}
func (City) TableName() string {
return "cityhash"
}

55
model/common.go Normal file
View File

@@ -0,0 +1,55 @@
package model
import (
"fmt"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"os"
"strconv"
)
var DB *gorm.DB
func Init() {
var err error
host := os.Getenv("MYSQL_HOST")
if host == "" {
host = "localhost"
}
portStr := os.Getenv("MYSQL_PORT")
var port int
if portStr == "" {
port = 3306
} else {
port, err = strconv.Atoi(portStr)
if err != nil {
panic(fmt.Sprintf("Invalid MYSQL_PORT environment variable: %s", portStr))
}
}
username := os.Getenv("MYSQL_USERNAME")
if username == "" {
panic("MYSQL_USERNAME environment variable is not set")
}
password := os.Getenv("MYSQL_PASSWORD")
if password == "" {
panic("MYSQL_PASSWORD environment variable is not set")
}
database := os.Getenv("MYSQL_DATABASE")
if database == "" {
panic("MYSQL_DATABASE environment variable is not set")
}
dsn := fmt.Sprintf(
"%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
username, password, host, port, database,
)
DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
panic(err)
}
}

41
model/config.go Normal file
View File

@@ -0,0 +1,41 @@
package model
type Config struct {
Id int `gorm:"column:id;primaryKey"`
GatewayMac string `gorm:"column:macaddr"`
Table string `gorm:"column:table"`
Macaddr string `gorm:"column:edge"`
Network string `gorm:"column:network"`
Cityhash string `gorm:"column:cityhash"`
CityLabel string `gorm:"column:label"`
User string `gorm:"column:user"`
InnerIp string `gorm:"column:inner_ip"`
IsChange int `gorm:"column:ischange"`
IsOnline int `gorm:"column:isonline"`
OnlineNum int `gorm:"column:onlinenum"`
CreateTime string `gorm:"column:createtime"`
UpdateTime string `gorm:"column:updatetime"`
}
func (Config) TableName() string {
return "gateway"
}
type ConfigUpdate struct {
Id int `gorm:"column:id;primaryKey"`
GatewayMac *string `gorm:"column:macaddr"`
Table *string `gorm:"column:table"`
Macaddr *string `gorm:"column:edge"`
Network *string `gorm:"column:network"`
Cityhash *string `gorm:"column:cityhash"`
CityLabel *string `gorm:"column:label"`
User *string `gorm:"column:user"`
InnerIp *string `gorm:"column:inner_ip"`
IsChange *int `gorm:"column:ischange"`
IsOnline *int `gorm:"column:isonline"`
OnlineNum *int `gorm:"column:onlinenum"`
}
func (ConfigUpdate) TableName() string {
return "gateway"
}

19
model/edge.go Normal file
View File

@@ -0,0 +1,19 @@
package model
type Edge struct {
Id int `gorm:"column:id;primaryKey"`
CityId int `gorm:"column:city_id"`
Macaddr string `gorm:"column:macaddr"`
Public string `gorm:"column:public"`
Isp string `gorm:"column:isp"`
Single int `gorm:"column:single"`
Sole bool `gorm:"column:sole"`
Arch int `gorm:"column:arch"`
Online int `gorm:"column:online"`
Active bool `gorm:"column:active"`
}
func (Edge) TableName() string {
return "edge"
}

19
model/gateway.go Normal file
View File

@@ -0,0 +1,19 @@
package model
type Gateway struct {
Id int `gorm:"column:id;primaryKey"`
ConfigVersion int `gorm:"column:setid"`
ChangeCount int `gorm:"column:change_count"`
LimitCount int `gorm:"column:limit_count"`
Token string `gorm:"column:token"`
Macaddr string `gorm:"column:macaddr"`
TokenTime string `gorm:"column:token_time"`
PrivateIp string `gorm:"column:inner_ip"`
ProxyIp string `gorm:"column:l2ip"`
CreateTime string `gorm:"column:createtime"`
UpdateTime string `gorm:"column:updatetime"`
}
func (e *Gateway) TableName() string {
return "token"
}