This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.go
168 lines (157 loc) · 4.17 KB
/
backend.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
157
158
159
160
161
162
163
164
165
166
167
168
package builder
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
)
const backendModule = "github.com/yaptide/app"
const converterModule = "github.com/yaptide/converter"
func startDevBackend(conf config) (*exec.Cmd, error) {
dockerErr := setupDockerDb(conf)
if dockerErr != nil {
return nil, dockerErr
}
absolutePath, getPackageErr := getPackagePath(backendModule)
if getPackageErr != nil {
return nil, getPackageErr
}
cmd := exec.Command(
"gin",
"--port", fmt.Sprintf("%d", conf.backendPort),
"--appPort", fmt.Sprintf("%d", 15003),
"--immediate",
)
cmd.Dir = absolutePath
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, fmt.Sprintf("YAPTIDE_FRONTEND_PUBLIC_URL=%s", conf.backendPublicUrl))
cmd.Env = append(cmd.Env, fmt.Sprintf("YAPTIDE_BACKEND_PORT=%d", 15003))
cmd.Env = append(cmd.Env, fmt.Sprintf("YAPTIDE_DB_URL=%s", conf.dbUrl()))
cmd.Env = append(cmd.Env, fmt.Sprintf("YAPTIDE_ENV=%s", "DEV"))
return cmd, cmd.Start()
}
func startDevBackendConverter(conf config) (*exec.Cmd, error) {
modulePath, getPackageErr := getPackagePath(backendModule)
if getPackageErr != nil {
return nil, getPackageErr
}
depPath := filepath.Join(modulePath, "vendor", converterModule)
removeErr := os.RemoveAll(depPath)
if removeErr != nil {
return nil, removeErr
}
return startDevBackend(conf)
}
func startDevBackendOnly(conf config) (*exec.Cmd, error) {
ensureErr := ensureDeps(backendModule)
if ensureErr != nil {
return nil, ensureErr
}
return startDevBackend(conf)
}
func createFile(path string, content string) error {
file, createErr := os.Create(path)
if createErr != nil {
return createErr
}
_, writeErr := file.Write([]byte(content))
return writeErr
}
func setupDockerDb(conf config) error {
if conf.dbHost != "localhost" {
return fmt.Errorf("unable to setup docker iamge on remote host")
}
cli, err := client.NewEnvClient()
if err != nil {
panic(err)
}
containers, listErr := cli.ContainerList(context.Background(), types.ContainerListOptions{All: true})
if listErr != nil {
return listErr
}
for _, container := range containers {
for _, name := range container.Names {
if name == "/yaptide_db_local" {
fmt.Println("Docker exists")
if container.State != "running" {
startErr := cli.ContainerStart(context.Background(), container.ID, types.ContainerStartOptions{})
if startErr != nil {
return startErr
}
}
return nil
}
}
}
fmt.Println("Docker create")
body, createErr := cli.ContainerCreate(
context.Background(),
&container.Config{
Image: "mongo",
},
&container.HostConfig{
PortBindings: nat.PortMap(
map[nat.Port][]nat.PortBinding{
nat.Port(fmt.Sprintf("%d/tcp", 27017)): []nat.PortBinding{
nat.PortBinding{HostPort: fmt.Sprintf("%d", conf.dbPort)},
},
},
),
},
&network.NetworkingConfig{},
"yaptide_db_local",
)
if createErr != nil {
return createErr
}
startErr := cli.ContainerStart(
context.Background(),
body.ID,
types.ContainerStartOptions{},
)
if startErr != nil {
return startErr
}
time.Sleep(time.Second * 4)
setupCmd := fmt.Sprintf("docker exec yaptide_db_local mongo admin --eval 'db.createUser({ user: \"root\", pwd: \"password\", roles: [ { role: \"root\", db: \"admin\" } ] });'")
userCmd := fmt.Sprintf("%s '%s'",
"docker exec yaptide_db_local mongo admin -u root -p password --eval",
fmt.Sprintf(
"db.getSiblingDB(\"%s\").createUser({ user: \"%s\", pwd: \"%s\", roles: [ { role: \"readWrite\", db: \"%s\" }] });",
conf.dbName,
conf.dbUser,
conf.dbPassword,
conf.dbName,
),
)
setupCommand := exec.Command(
"bash",
"-c",
setupCmd,
)
setupCommand.Stdout = os.Stdout
setupCommand.Stderr = os.Stderr
if err := setupCommand.Run(); err != nil {
return nil
}
userCommand := exec.Command(
"bash",
"-c",
userCmd,
)
userCommand.Stdout = os.Stdout
userCommand.Stderr = os.Stderr
if err := userCommand.Run(); err != nil {
return nil
}
return nil
}