-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
156 lines (131 loc) · 3.58 KB
/
main.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
149
150
151
152
153
154
155
156
package main
import (
"encoding/json"
"fmt"
"hash/crc32"
"log"
"math/rand"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
)
const (
membersCount = 10
emailDomain = "example.com"
)
var (
nameFirstLetters = []byte("abcdefghijklmnopqrstuvwz")
lastNames = []string{
"frerichs", "hengst", "kloth", "orth", "popp", "steigerwald", "kley", "voegele", "ostermeier",
"schley", "eckel", "kaeser", "holzner", "klostermann", "ostertag", "weishaupt", "gotte", "feiler", "mast",
"hielscher", "haertel", "moeller", "kramer", "stief", "kissel", "gottschlich", "drechsler", "lucas", "till",
"koehne", "pfitzner", "sydow", "liedtke", "franz", "flick", "menz", "hertel", "kreuzer", "kleinert", "zeidler",
}
)
type myHandler struct{}
type InfrastructureAccount struct {
Id string `json:"id"`
Name string `json:"name"`
Provider string `json:"provider"`
Type string `json:"type"`
Description string `json:"description"`
Owner string `json:"owner"`
OwnerDn string `json:"owner_dn"`
Disabled bool `json:"disabled"`
}
type Team struct {
Dn string `json:"dn"`
Id string `json:"id"`
TeamName string `json:"id_name"`
TeamId string `json:"team_id"`
Type string `json:"type"`
FullName string `json:"name"`
Aliases []string `json:"alias,omitempty"`
Mails []string `json:"mail"`
Members []string `json:"member"`
CostCenter string `json:"cost_center,omitempty"`
DeliveryLead string `json:"delivery_lead,omitempty"`
ParentTeamId string `json:"parent_team_id,omitempty"`
InfrastructureAccounts []InfrastructureAccount `json:"infrastructure-accounts"`
}
func main() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM) // Push signals into channel
if len(os.Args) < 2 {
fmt.Printf("Usage %s {port}\n", os.Args[0])
os.Exit(1)
}
port, err := strconv.Atoi(os.Args[1])
if err != nil {
fmt.Printf("Port must be numeric\n")
os.Exit(1)
}
server := http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: &myHandler{},
}
go func() {
err := server.ListenAndServe()
if err != nil {
log.Fatal(err)
}
}()
log.Printf("Listening on %d\n", port)
sig := <-sigs
log.Printf("Got %+v signal. Shutting down\n", sig)
}
func members() []string {
res := make([]string, membersCount)
for i := 0; i < membersCount; i++ {
name := nameFirstLetters[rand.Intn(len(nameFirstLetters))]
res[i] = string(name) + lastNames[rand.Intn(len(lastNames))]
}
return res
}
func testTeam(teamName string, w http.ResponseWriter) {
id := crc32.ChecksumIEEE([]byte(teamName))
rand.Seed(int64(id))
teamType := "official"
if id%2 == 0 {
teamType = "virtual"
}
team := Team{
Id: teamName,
TeamId: strconv.Itoa(int(id)),
TeamName: strings.ToUpper(teamName),
Members: members(),
FullName: strings.Title(teamName),
Mails: []string{fmt.Sprintf("%s@%s", teamName, emailDomain)},
Type: teamType,
}
m, err := json.Marshal(team)
if err != nil {
fmt.Fprintf(w, "Can't marshal: %s", err)
}
w.Header().Set("Content-Type", "application/json")
w.Write(m)
}
func badRequest(w http.ResponseWriter) {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Bad request"))
}
func (*myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("Request: %s\n", r.URL.Path)
if !strings.HasPrefix(r.URL.Path, "/teams") {
badRequest(w)
return
}
var teamName string
parts := strings.Split(r.URL.Path, "/")
if len(parts) > 1 {
teamName = parts[2]
}
if teamName == "" {
badRequest(w)
return
}
testTeam(teamName, w)
}