-
Notifications
You must be signed in to change notification settings - Fork 158
/
domains.go
148 lines (116 loc) · 2.89 KB
/
domains.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package empire
import (
"golang.org/x/net/context"
"time"
"github.com/jinzhu/gorm"
"github.com/remind101/empire/pkg/timex"
)
type Domain struct {
ID string
Hostname string
CreatedAt *time.Time
AppID string
App *App
}
func (d *Domain) BeforeCreate() error {
t := timex.Now()
d.CreatedAt = &t
return nil
}
type domainsService struct {
*Empire
}
func (s *domainsService) DomainsCreate(ctx context.Context, db *gorm.DB, domain *Domain) (*Domain, error) {
d, err := domainsFind(db, DomainsQuery{Hostname: &domain.Hostname})
if err != nil && err != gorm.RecordNotFound {
return domain, err
}
if err != gorm.RecordNotFound {
if d.AppID == domain.AppID {
return domain, ErrDomainAlreadyAdded
} else {
return domain, ErrDomainInUse
}
}
_, err = domainsCreate(db, domain)
if err != nil {
return domain, err
}
if err := makePublic(db, domain.AppID); err != nil {
return domain, err
}
return domain, err
}
func (s *domainsService) DomainsDestroy(ctx context.Context, db *gorm.DB, domain *Domain) error {
if err := domainsDestroy(db, domain); err != nil {
return err
}
// If app has no domains associated, make it private
d, err := domains(db, DomainsQuery{App: domain.App})
if err != nil {
return err
}
if len(d) == 0 {
if err := makePrivate(db, domain.AppID); err != nil {
return err
}
}
return nil
}
// DomainsQuery is a scope implementation for common things to filter releases
// by.
type DomainsQuery struct {
// If provided, finds domains matching the given hostname.
Hostname *string
// If provided, filters domains belonging to the given app.
App *App
}
// scope implements the scope interface.
func (q DomainsQuery) scope(db *gorm.DB) *gorm.DB {
var scope composedScope
if q.Hostname != nil {
scope = append(scope, fieldEquals("hostname", *q.Hostname))
}
if q.App != nil {
scope = append(scope, forApp(q.App))
}
return scope.scope(db)
}
// domainsFind returns the first matching domain.
func domainsFind(db *gorm.DB, scope scope) (*Domain, error) {
var domain Domain
return &domain, first(db, scope, &domain)
}
// domains returns all domains matching the scope.
func domains(db *gorm.DB, scope scope) ([]*Domain, error) {
var domains []*Domain
return domains, find(db, scope, &domains)
}
func domainsCreate(db *gorm.DB, domain *Domain) (*Domain, error) {
return domain, db.Create(domain).Error
}
func domainsDestroy(db *gorm.DB, domain *Domain) error {
return db.Delete(domain).Error
}
func makePublic(db *gorm.DB, appID string) error {
a, err := appsFind(db, AppsQuery{ID: &appID})
if err != nil {
return err
}
a.Exposure = "public"
if err := appsUpdate(db, a); err != nil {
return err
}
return nil
}
func makePrivate(db *gorm.DB, appID string) error {
a, err := appsFind(db, AppsQuery{ID: &appID})
if err != nil {
return err
}
a.Exposure = "private"
if err := appsUpdate(db, a); err != nil {
return err
}
return nil
}