Welcome to mirror list, hosted at ThFree Co, Russian Federation.

domains.go « source « internal - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5e7f113cf594d522656bb388cd3628ba7347c520 (plain)
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
package source

import (
	"strings"
	"sync"
	"time"

	"gitlab.com/gitlab-org/gitlab-pages/internal/domain"
	"gitlab.com/gitlab-org/gitlab-pages/internal/source/groups"
)

// Domains struct represents a map of all domains supported by pages. It is
// currently reading them from disk.
type Domains struct {
	dm   groups.Map
	lock sync.RWMutex
}

// GetDomain returns a domain from the domains map
func (d *Domains) GetDomain(host string) *domain.Domain {
	host = strings.ToLower(host)
	d.lock.RLock()
	defer d.lock.RUnlock()
	domain, _ := d.dm[host]

	return domain
}

// HasDomain checks for presence of a domain in the domains map
func (d *Domains) HasDomain(host string) bool {
	d.lock.RLock()
	defer d.lock.RUnlock()

	host = strings.ToLower(host)
	_, isPresent := d.dm[host]

	return isPresent
}

// Ready checks if the domains source is ready for work
func (d *Domains) Ready() bool {
	return d.dm != nil
}

// Watch starts the domain source, in this case it is reading domains from
// groups on disk concurrently
func (d *Domains) Watch(rootDomain string) {
	go groups.Watch(rootDomain, d.updateDomains, time.Second)
}

func (d *Domains) updateDomains(dm groups.Map) {
	d.lock.Lock()
	defer d.lock.Unlock()

	d.dm = dm
}