-
-
Notifications
You must be signed in to change notification settings - Fork 213
/
server.go
51 lines (42 loc) · 1.28 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package models
import (
"github.com/pufferpanel/pufferpanel/v3"
"gopkg.in/go-playground/validator.v9"
"gorm.io/gorm"
"time"
)
type Server struct {
Name string `gorm:"size:40;NOT NULL" json:"-" validate:"required,printascii"`
Identifier string `gorm:"UNIQUE;NOT NULL;primaryKey;size:8" json:"-" validate:"required,printascii"`
RawNodeID *uint `gorm:"column:node_id" json:"-"`
NodeID uint `gorm:"-" json:"-" validate:"-"`
Node Node `gorm:"ASSOCIATION_SAVE_REFERENCE:false;foreignKey:RawNodeID" json:"-" validate:"-"`
IP string `gorm:"" json:"-" validate:"omitempty,ip|fqdn"`
Port uint16 `gorm:"" json:"-" validate:"omitempty"`
Type string `gorm:"NOT NULL;default='generic'" json:"-" validate:"required,printascii"`
Icon string `gorm:"" json:"-"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
}
func (s *Server) IsValid() (err error) {
err = validator.New().Struct(s)
if err != nil {
err = pufferpanel.GenerateValidationMessage(err)
}
return
}
func (s *Server) BeforeSave(*gorm.DB) (err error) {
err = s.IsValid()
if s.NodeID == 0 {
s.RawNodeID = nil
} else {
s.RawNodeID = &s.NodeID
}
return
}
func (s *Server) AfterFind(*gorm.DB) (err error) {
if s.RawNodeID == nil || *s.RawNodeID == LocalNode.ID {
s.Node = *LocalNode
}
return
}