/
reverseproxy.go
91 lines (88 loc) · 2.34 KB
/
reverseproxy.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
package routing
import (
"log"
"docker.io/go-docker/api/types/container"
"docker.io/go-docker/api/types/network"
"github.com/docker/go-connections/nat"
"github.com/magrandera/SPaaS/common"
"github.com/magrandera/SPaaS/config"
"github.com/magrandera/SPaaS/server/controller"
)
// InitReverseProxy initializes the reverse proxy
func InitReverseProxy() {
list, err := controller.ListContainers()
if err != nil {
log.Fatal(err)
}
for _, container := range list {
for _, element := range container.Names {
if element == "/"+common.SpaasName("traefik") {
return
}
}
}
log.Println("Traefik is not installed but installing now")
if err := controller.PullImage("traefik:1.7-alpine"); err != nil {
log.Fatal(err.Error())
}
cmd := []string{
"--docker", "--docker.watch",
"--defaultEntryPoints=http",
"--entryPoints=Name:http Address::80 Compress:off",
"--docker.domain=granderath.tech",
"--debug",
"--logLevel=DEBUG",
}
letsencrypt := []string{
"--acme",
"--acme.email=" + config.Cfg.Config.GetString("letsencryptEmail"),
"--acme.storage=/var/acme/acme.json",
"--acme.httpchallenge.entrypoint=http",
"--acme.entrypoint=https",
"--acme.onhostrule=true",
"--accesslogsfile=/var/acme/access.log",
"--entryPoints=Name:https Address::443 TLS Compress:off",
"--entryPoints=Name:http Address::80 Redirect.EntryPoint:https Compress:off",
"--defaultEntryPoints=https,http",
}
if config.Cfg.Config.GetBool("letsencrypt") {
cmd = append(cmd, letsencrypt...)
}
containerID, err := controller.CreateContainer(
container.Config{
Image: "traefik:1.7-alpine",
ExposedPorts: nat.PortSet{
"80/tcp": struct{}{},
"8080/tcp": struct{}{},
"443/tcp": struct{}{},
},
Cmd: cmd,
},
container.HostConfig{
Binds: []string{
"/var/run/docker.sock:/var/run/docker.sock",
config.Cfg.Config.GetString("acmePath") + ":/var/acme",
},
PortBindings: nat.PortMap{
"80/tcp": []nat.PortBinding{
{HostPort: "80"},
},
"8080/tcp": []nat.PortBinding{
{HostPort: "8080"},
},
"443/tcp": []nat.PortBinding{
{HostPort: "443"},
},
},
},
network.NetworkingConfig{},
common.SpaasName("traefik"),
)
if err != nil {
log.Fatal(err.Error())
}
if err := controller.StartContainer(containerID.ID); err != nil {
log.Fatal(err.Error())
}
log.Println("Traefik is now installed")
}