2023-01-04 05:36:56 +00:00
|
|
|
package upstreamserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"npm/internal/database"
|
2023-05-26 01:04:43 +00:00
|
|
|
"npm/internal/entity"
|
2023-01-04 05:36:56 +00:00
|
|
|
)
|
|
|
|
|
2023-05-26 01:04:43 +00:00
|
|
|
// Model is the model
|
2023-01-04 05:36:56 +00:00
|
|
|
type Model struct {
|
2023-05-26 01:04:43 +00:00
|
|
|
entity.ModelBase
|
|
|
|
UpstreamID uint `json:"upstream_id" gorm:"column:upstream_id" filter:"upstream_id,integer"`
|
|
|
|
Server string `json:"server" gorm:"column:server" filter:"server,string"`
|
|
|
|
Weight int `json:"weight" gorm:"column:weight" filter:"weight,integer"`
|
|
|
|
MaxConns int `json:"max_conns" gorm:"column:max_conns" filter:"max_conns,integer"`
|
|
|
|
MaxFails int `json:"max_fails" gorm:"column:max_fails" filter:"max_fails,integer"`
|
|
|
|
FailTimeout int `json:"fail_timeout" gorm:"column:fail_timeout" filter:"fail_timeout,integer"`
|
|
|
|
Backup bool `json:"backup" gorm:"column:is_backup" filter:"backup,boolean"`
|
2023-01-04 05:36:56 +00:00
|
|
|
}
|
|
|
|
|
2023-05-26 01:04:43 +00:00
|
|
|
// TableName overrides the table name used by gorm
|
|
|
|
func (Model) TableName() string {
|
|
|
|
return "upstream_server"
|
2023-01-04 05:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadByID will load from an ID
|
|
|
|
func (m *Model) LoadByID(id int) error {
|
2023-05-26 01:04:43 +00:00
|
|
|
db := database.GetDB()
|
|
|
|
result := db.First(&m, id)
|
|
|
|
return result.Error
|
2023-01-04 05:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save will save this model to the DB
|
|
|
|
func (m *Model) Save() error {
|
2023-05-26 01:04:43 +00:00
|
|
|
db := database.GetDB()
|
|
|
|
result := db.Save(m)
|
|
|
|
return result.Error
|
2023-01-04 05:36:56 +00:00
|
|
|
}
|
|
|
|
|
2023-05-26 01:04:43 +00:00
|
|
|
// Delete will mark row as deleted
|
2023-01-04 05:36:56 +00:00
|
|
|
func (m *Model) Delete() bool {
|
2023-05-26 01:04:43 +00:00
|
|
|
if m.ID == 0 {
|
|
|
|
// Can't delete a new object
|
2023-01-04 05:36:56 +00:00
|
|
|
return false
|
|
|
|
}
|
2023-05-26 01:04:43 +00:00
|
|
|
db := database.GetDB()
|
|
|
|
result := db.Delete(m)
|
|
|
|
return result.Error == nil
|
2023-01-04 05:36:56 +00:00
|
|
|
}
|