/
repo.go
115 lines (108 loc) · 2.74 KB
/
repo.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
package main
import (
"errors"
"fmt"
"github.com/coreos/etcd/client"
"golang.org/x/net/context"
"strings"
)
func RepoGetDomainByName(name string) (Domain, error) {
var domain Domain
key := fmt.Sprintf("/domains/map/%s", name)
meta, err := etcd.Get(context.Background(), key, nil)
if err != nil {
return domain, err
}
hash := meta.Node.Value
domain = Domain{
hash,
name,
}
return domain, nil
}
func RepoGetDomainsByUserId(userId string) (Domains, error) {
domains := make(Domains, 0)
key := fmt.Sprintf("/domains/users/ids/%s", userId)
meta, err := etcd.Get(context.Background(), key, nil)
if err != nil {
return domains, err
}
nodes := meta.Node.Nodes
for _, node := range nodes {
nodePath := strings.Split(node.Key, "/")
name := nodePath[len(nodePath)-1]
hash := node.Value
domain := Domain{
hash,
name,
}
domains = append(domains, domain)
}
return domains, nil
}
func RepoGetUserIdByName(name string) (string, error) {
key := fmt.Sprintf("/domains/users/names/%s", name)
meta, err := etcd.Get(context.Background(), key, nil)
if err != nil {
return "", err
}
return meta.Node.Value, nil
}
func RepoCreateDomainMapping(name, hash, userId string) (Domain, error) {
var domain Domain
if ok, domain, _ := RepoDomainExistsByName(name); ok {
return domain, errors.New("Subdomain is already mapped to a hash!")
}
if domains, _ := RepoGetDomainsByUserId(userId); len(domains) > 2 {
return domain, errors.New("Only 3 Subdomains allowed per user!")
}
key := fmt.Sprintf("domains/map/%s", name)
_, err := etcd.Create(context.Background(), key, hash)
if err != nil {
return domain, err
}
key = fmt.Sprintf("domains/users/ids/%s/%s", userId, name)
_, err = etcd.Create(context.Background(), key, hash)
if err != nil {
return domain, err
}
key = fmt.Sprintf("domains/users/names/%s", name)
_, err = etcd.Create(context.Background(), key, userId)
if err != nil {
return domain, err
}
domain = Domain{
hash,
name,
}
return domain, nil
}
func RepoDeleteDomainByName(name string) Domain {
domain := Domain{
"",
name,
}
key := fmt.Sprintf("domains/map/%s", name)
meta, err := etcd.Delete(context.Background(), key, nil)
if err == nil {
domain.Hash = meta.PrevNode.Value
}
key = fmt.Sprintf("domains/users/names/%s", name)
meta, err = etcd.Delete(context.Background(), key, nil)
if err == nil {
userId := meta.PrevNode.Value
key = fmt.Sprintf("domains/users/ids/%s/%s", userId, name)
etcd.Delete(context.Background(), key, nil)
}
return domain
}
func RepoDomainExistsByName(name string) (bool, Domain, error) {
domain, err := RepoGetDomainByName(name)
if err != nil {
if client.IsKeyNotFound(err) != true {
return false, domain, err
}
return false, domain, nil
}
return true, domain, nil
}