This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
reception.go
130 lines (110 loc) · 2.55 KB
/
reception.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
package main
import (
"flag"
"fmt"
"github.com/cenkalti/backoff"
miekg_dns "github.com/miekg/dns"
"github.com/ninech/reception/common"
"github.com/ninech/reception/dns"
"github.com/ninech/reception/docker"
"github.com/ninech/reception/http"
net_http "net/http"
"runtime"
)
var config = &common.Config{
Projects: common.NewProjects(),
}
var (
Tag string = "SNAPSHOT"
BuildDate string
Commit string
Branch string
ShowVersion bool
)
func init() {
flag.StringVar(
&config.HTTPBindAddress,
"http.address",
"localhost:80",
"Defines on which address and port the HTTP daemon listens.")
flag.StringVar(
&config.DNSBindAddress,
"dns.address",
"localhost:53",
"Defines on which address and port the HTTP daemon listens.")
flag.StringVar(
&config.TLD,
"tld",
"docker.",
"Defines on which TLD to react for HTTP and DNS requests. Should end with a \".\" .")
flag.StringVar(
&config.DockerEndpoint,
"docker.endpoint",
"unix:///var/run/docker.sock",
"How reception talks to Docker.")
BoolFlag(&ShowVersion, "version", false, "Show version.")
}
func BoolFlag(p *bool, name string, value bool, usage string) {
flag.BoolVar(p, name, value, usage)
flag.BoolVar(p, name[:1], value, usage)
}
func main() {
fmt.Println("(c) 2017-2018 Nine Internet Solutions AG")
fmt.Println("(c) 2018-2019 nxt Engineering GmbH")
flag.Parse()
if ShowVersion {
showVersionInfo()
return
}
go runHttpFrontend()
go runDns()
err := backoff.Retry(runDockerClient, backoff.NewExponentialBackOff())
if err != nil {
panic(err)
}
}
func runDns() {
handler := dns.Handler{
Config: config,
}
tld := config.TLD
if "." != tld[len(tld)-1:] {
tld += "."
}
miekg_dns.HandleFunc(tld, handler.ServeDns)
addr := config.DNSBindAddress
fmt.Printf("Listening on '%v' for DNS requests.\n", addr)
srv := &miekg_dns.Server{Addr: addr, Net: "udp"}
err := srv.ListenAndServe()
if err != nil {
panic(err)
}
}
func runDockerClient() error {
client := docker.Client{
Config: config,
}
return client.Launch()
}
func runHttpFrontend() {
frontend := &net_http.Server{
Addr: config.HTTPBindAddress,
Handler: http.BackendHandler{
Config: config,
},
}
fmt.Printf("Listening on '%v' for HTTP traffic.\n", frontend.Addr)
err := frontend.ListenAndServe()
if err != nil {
panic(err)
} else {
defer frontend.Close()
}
}
func showVersionInfo() {
fmt.Println("Version: ", Tag)
fmt.Println("Build Date: ", BuildDate)
fmt.Println("Commit: ", Commit)
fmt.Println("Branch: ", Branch)
fmt.Println("Go Version: ", runtime.Version())
}