2023-01-04 05:36:56 +00:00
|
|
|
package upstream
|
|
|
|
|
|
|
|
import (
|
2023-01-10 02:50:46 +00:00
|
|
|
"strings"
|
2023-01-04 05:36:56 +00:00
|
|
|
|
|
|
|
"npm/internal/database"
|
2023-05-26 01:04:43 +00:00
|
|
|
"npm/internal/entity"
|
2023-01-04 05:36:56 +00:00
|
|
|
"npm/internal/entity/nginxtemplate"
|
|
|
|
"npm/internal/entity/upstreamserver"
|
2023-01-10 02:57:43 +00:00
|
|
|
"npm/internal/entity/user"
|
2023-01-04 05:36:56 +00:00
|
|
|
"npm/internal/status"
|
|
|
|
"npm/internal/util"
|
2023-02-24 07:19:07 +00:00
|
|
|
|
|
|
|
"github.com/rotisserie/eris"
|
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
|
|
|
// See: http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream
|
|
|
|
type Model struct {
|
2023-05-26 01:04:43 +00:00
|
|
|
entity.ModelBase
|
|
|
|
UserID uint `json:"user_id" gorm:"column:user_id" filter:"user_id,integer"`
|
|
|
|
Name string `json:"name" gorm:"column:name" filter:"name,string"`
|
|
|
|
NginxTemplateID uint `json:"nginx_template_id" gorm:"column:nginx_template_id" filter:"nginx_template_id,integer"`
|
|
|
|
IPHash bool `json:"ip_hash" gorm:"column:ip_hash" filter:"ip_hash,boolean"`
|
|
|
|
NTLM bool `json:"ntlm" gorm:"column:ntlm" filter:"ntlm,boolean"`
|
|
|
|
Keepalive int `json:"keepalive" gorm:"column:keepalive" filter:"keepalive,integer"`
|
|
|
|
KeepaliveRequests int `json:"keepalive_requests" gorm:"column:keepalive_requests" filter:"keepalive_requests,integer"`
|
|
|
|
KeepaliveTime string `json:"keepalive_time" gorm:"column:keepalive_time" filter:"keepalive_time,string"`
|
|
|
|
KeepaliveTimeout string `json:"keepalive_timeout" gorm:"column:keepalive_timeout" filter:"keepalive_timeout,string"`
|
|
|
|
AdvancedConfig string `json:"advanced_config" gorm:"column:advanced_config" filter:"advanced_config,string"`
|
|
|
|
Status string `json:"status" gorm:"column:status" filter:"status,string"`
|
|
|
|
ErrorMessage string `json:"error_message" gorm:"column:error_message" filter:"error_message,string"`
|
2023-01-04 05:36:56 +00:00
|
|
|
// Expansions
|
2023-05-26 01:04:43 +00:00
|
|
|
Servers []upstreamserver.Model `json:"servers" gorm:"-"`
|
|
|
|
NginxTemplate *nginxtemplate.Model `json:"nginx_template,omitempty" gorm:"-"`
|
|
|
|
User *user.Model `json:"user,omitempty" gorm:"-"`
|
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"
|
2023-01-04 05:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadByID will load from an ID
|
2023-05-26 01:04:43 +00:00
|
|
|
func (m *Model) LoadByID(id uint) error {
|
|
|
|
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(skipConfiguration bool) error {
|
|
|
|
if m.UserID == 0 {
|
2023-02-24 07:19:07 +00:00
|
|
|
return eris.Errorf("User ID must be specified")
|
2023-01-04 05:36:56 +00:00
|
|
|
}
|
|
|
|
|
2023-01-10 02:50:46 +00:00
|
|
|
// ensure name is trimmed of whitespace
|
|
|
|
m.Name = strings.TrimSpace(m.Name)
|
|
|
|
|
2023-01-04 05:36:56 +00:00
|
|
|
if !skipConfiguration {
|
|
|
|
// Set this upstream as requiring reconfiguration
|
|
|
|
m.Status = status.StatusReady
|
|
|
|
}
|
|
|
|
|
2023-05-26 01:04:43 +00:00
|
|
|
db := database.GetDB()
|
|
|
|
if result := db.Save(m); result.Error != nil {
|
|
|
|
return result.Error
|
2023-01-04 05:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save Servers
|
2023-05-26 01:04:43 +00:00
|
|
|
var err error
|
|
|
|
for idx := range m.Servers {
|
|
|
|
// Continue if previous iteration didn't cause an error
|
|
|
|
if err == nil {
|
|
|
|
m.Servers[idx].UpstreamID = m.ID
|
|
|
|
err = m.Servers[idx].Save()
|
2023-01-04 05:36:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Expand will fill in more properties
|
|
|
|
func (m *Model) Expand(items []string) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Always expand servers, if not done already
|
|
|
|
if len(m.Servers) == 0 {
|
|
|
|
m.Servers, err = upstreamserver.GetByUpstreamID(m.ID)
|
|
|
|
}
|
|
|
|
|
2023-01-10 02:57:43 +00:00
|
|
|
if util.SliceContainsItem(items, "user") && m.ID > 0 {
|
|
|
|
var usr user.Model
|
|
|
|
usr, err = user.GetByID(m.UserID)
|
|
|
|
m.User = &usr
|
|
|
|
}
|
|
|
|
|
2023-01-04 05:36:56 +00:00
|
|
|
if util.SliceContainsItem(items, "nginxtemplate") && m.NginxTemplateID > 0 {
|
|
|
|
var templ nginxtemplate.Model
|
|
|
|
templ, err = nginxtemplate.GetByID(m.NginxTemplateID)
|
|
|
|
m.NginxTemplate = &templ
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|