|
1 | 1 | package model |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "github.com/0xJacky/Nginx-UI/server/pkg/nginx" |
5 | | - "github.com/lib/pq" |
6 | | - "os" |
| 4 | + "github.com/0xJacky/Nginx-UI/server/pkg/nginx" |
| 5 | + "github.com/lib/pq" |
| 6 | + "os" |
7 | 7 | ) |
8 | 8 |
|
9 | 9 | const ( |
10 | | - AutoCertEnabled = 1 |
11 | | - AutoCertDisabled = -1 |
| 10 | + AutoCertEnabled = 1 |
| 11 | + AutoCertDisabled = -1 |
12 | 12 | ) |
13 | 13 |
|
14 | 14 | type CertDomains []string |
15 | 15 |
|
16 | 16 | type Cert struct { |
17 | | - Model |
18 | | - Name string `json:"name"` |
19 | | - Domains pq.StringArray `json:"domains" gorm:"type:text[]"` |
20 | | - Filename string `json:"filename"` |
21 | | - SSLCertificatePath string `json:"ssl_certificate_path"` |
22 | | - SSLCertificateKeyPath string `json:"ssl_certificate_key_path"` |
23 | | - AutoCert int `json:"auto_cert"` |
24 | | - Log string `json:"log"` |
| 17 | + Model |
| 18 | + Name string `json:"name"` |
| 19 | + Domains pq.StringArray `json:"domains" gorm:"type:text[]"` |
| 20 | + Filename string `json:"filename"` |
| 21 | + SSLCertificatePath string `json:"ssl_certificate_path"` |
| 22 | + SSLCertificateKeyPath string `json:"ssl_certificate_key_path"` |
| 23 | + AutoCert int `json:"auto_cert"` |
| 24 | + Log string `json:"log"` |
25 | 25 | } |
26 | 26 |
|
27 | 27 | func FirstCert(confName string) (c Cert, err error) { |
28 | | - err = db.First(&c, &Cert{ |
29 | | - Filename: confName, |
30 | | - }).Error |
| 28 | + err = db.First(&c, &Cert{ |
| 29 | + Filename: confName, |
| 30 | + }).Error |
31 | 31 |
|
32 | | - return |
| 32 | + return |
33 | 33 | } |
34 | 34 |
|
35 | 35 | func FirstOrCreateCert(confName string) (c Cert, err error) { |
36 | | - err = db.FirstOrCreate(&c, &Cert{Filename: confName}).Error |
37 | | - return |
| 36 | + err = db.FirstOrCreate(&c, &Cert{Filename: confName}).Error |
| 37 | + return |
38 | 38 | } |
39 | 39 |
|
40 | 40 | func (c *Cert) Insert() error { |
41 | | - return db.Create(c).Error |
| 41 | + return db.Create(c).Error |
42 | 42 | } |
43 | 43 |
|
44 | 44 | func GetAutoCertList() (c []*Cert) { |
45 | | - var t []*Cert |
46 | | - db.Where("auto_cert", AutoCertEnabled).Find(&t) |
| 45 | + var t []*Cert |
| 46 | + db.Where("auto_cert", AutoCertEnabled).Find(&t) |
47 | 47 |
|
48 | | - // check if this domain is enabled |
49 | | - enabledConfig, err := os.ReadDir(nginx.GetConfPath("sites-enabled")) |
| 48 | + // check if this domain is enabled |
| 49 | + enabledConfig, err := os.ReadDir(nginx.GetConfPath("sites-enabled")) |
50 | 50 |
|
51 | | - if err != nil { |
52 | | - return |
53 | | - } |
| 51 | + if err != nil { |
| 52 | + return |
| 53 | + } |
54 | 54 |
|
55 | | - enabledConfigMap := make(map[string]bool) |
56 | | - for i := range enabledConfig { |
57 | | - enabledConfigMap[enabledConfig[i].Name()] = true |
58 | | - } |
| 55 | + enabledConfigMap := make(map[string]bool) |
| 56 | + for i := range enabledConfig { |
| 57 | + enabledConfigMap[enabledConfig[i].Name()] = true |
| 58 | + } |
59 | 59 |
|
60 | | - for _, v := range t { |
61 | | - if enabledConfigMap[v.Filename] == true { |
62 | | - c = append(c, v) |
63 | | - } |
64 | | - } |
| 60 | + for _, v := range t { |
| 61 | + if enabledConfigMap[v.Filename] == true { |
| 62 | + c = append(c, v) |
| 63 | + } |
| 64 | + } |
65 | 65 |
|
66 | | - return |
| 66 | + return |
67 | 67 | } |
68 | 68 |
|
69 | 69 | func GetCertList(name, domain string) (c []Cert) { |
70 | | - tx := db |
71 | | - if name != "" { |
72 | | - tx = tx.Where("name LIKE ? or domain LIKE ?", "%"+name+"%", "%"+name+"%") |
73 | | - } |
74 | | - if domain != "" { |
75 | | - tx = tx.Where("domain LIKE ?", "%"+domain+"%") |
76 | | - } |
77 | | - tx.Find(&c) |
78 | | - return |
| 70 | + tx := db |
| 71 | + if name != "" { |
| 72 | + tx = tx.Where("name LIKE ? or domain LIKE ?", "%"+name+"%", "%"+name+"%") |
| 73 | + } |
| 74 | + if domain != "" { |
| 75 | + tx = tx.Where("domain LIKE ?", "%"+domain+"%") |
| 76 | + } |
| 77 | + tx.Find(&c) |
| 78 | + return |
79 | 79 | } |
80 | 80 |
|
81 | 81 | func FirstCertByID(id int) (c Cert, err error) { |
82 | | - err = db.First(&c, id).Error |
| 82 | + err = db.First(&c, id).Error |
83 | 83 |
|
84 | | - return |
| 84 | + return |
85 | 85 | } |
86 | 86 |
|
87 | 87 | func (c *Cert) Updates(n *Cert) error { |
88 | | - return db.Model(&Cert{}).Where("id", c.ID).Updates(n).Error |
| 88 | + return db.Model(&Cert{}).Where("id", c.ID).Updates(n).Error |
89 | 89 | } |
90 | 90 |
|
91 | 91 | func (c *Cert) Remove() error { |
92 | | - return db.Where("filename", c.Filename).Delete(c).Error |
| 92 | + if c.Filename == "" { |
| 93 | + return db.Delete(c).Error |
| 94 | + } |
| 95 | + |
| 96 | + return db.Where("filename", c.Filename).Delete(c).Error |
93 | 97 | } |
0 commit comments