-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathconfig.go
170 lines (153 loc) · 3.84 KB
/
config.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
169
170
package utils
import (
_ "embed"
"io/fs"
"net"
"net/url"
"os"
"strconv"
"github.com/go-errors/errors"
"github.com/spf13/afero"
"github.com/spf13/viper"
"github.com/supabase/cli/pkg/config"
)
var (
NetId string
DbId string
ConfigId string
KongId string
GotrueId string
InbucketId string
RealtimeId string
RestId string
StorageId string
ImgProxyId string
DifferId string
PgmetaId string
StudioId string
EdgeRuntimeId string
LogflareId string
VectorId string
PoolerId string
DbAliases = []string{"db", "db.supabase.internal"}
KongAliases = []string{"kong", "api.supabase.internal"}
GotrueAliases = []string{"auth"}
InbucketAliases = []string{"inbucket"}
RealtimeAliases = []string{"realtime", Config.Realtime.TenantId}
RestAliases = []string{"rest"}
StorageAliases = []string{"storage"}
ImgProxyAliases = []string{"imgproxy"}
PgmetaAliases = []string{"pg_meta"}
StudioAliases = []string{"studio"}
EdgeRuntimeAliases = []string{"edge_runtime"}
LogflareAliases = []string{"analytics"}
VectorAliases = []string{"vector"}
PoolerAliases = []string{"pooler"}
//go:embed templates/initial_schemas/13.sql
InitialSchemaPg13Sql string
//go:embed templates/initial_schemas/14.sql
InitialSchemaPg14Sql string
)
func GetId(name string) string {
return "supabase_" + name + "_" + Config.ProjectId
}
func UpdateDockerIds() {
if NetId = viper.GetString("network-id"); len(NetId) == 0 {
NetId = GetId("network")
}
DbId = GetId(DbAliases[0])
ConfigId = GetId("config")
KongId = GetId(KongAliases[0])
GotrueId = GetId(GotrueAliases[0])
InbucketId = GetId(InbucketAliases[0])
RealtimeId = GetId(RealtimeAliases[0])
RestId = GetId(RestAliases[0])
StorageId = GetId(StorageAliases[0])
ImgProxyId = GetId(ImgProxyAliases[0])
DifferId = GetId("differ")
PgmetaId = GetId(PgmetaAliases[0])
StudioId = GetId(StudioAliases[0])
EdgeRuntimeId = GetId(EdgeRuntimeAliases[0])
LogflareId = GetId(LogflareAliases[0])
VectorId = GetId(VectorAliases[0])
PoolerId = GetId(PoolerAliases[0])
}
func GetDockerIds() []string {
return []string{
KongId,
GotrueId,
InbucketId,
RealtimeId,
RestId,
StorageId,
ImgProxyId,
PgmetaId,
StudioId,
EdgeRuntimeId,
LogflareId,
VectorId,
PoolerId,
}
}
var Config = config.NewConfig(config.WithHostname(GetHostname()))
// Adapts fs.FS to support absolute paths
type rootFS struct {
fsys afero.Fs
}
func (f *rootFS) Open(name string) (fs.File, error) {
return f.fsys.Open(name)
}
func NewRootFS(fsys afero.Fs) fs.FS {
return &rootFS{fsys: fsys}
}
func ToRealtimeEnv(addr config.AddressFamily) string {
if addr == config.AddressIPv6 {
return "-proto_dist inet6_tcp"
}
return "-proto_dist inet_tcp"
}
type InitParams struct {
ProjectId string
UseOrioleDB bool
Overwrite bool
}
func InitConfig(params InitParams, fsys afero.Fs) error {
c := config.NewConfig()
c.ProjectId = params.ProjectId
if params.UseOrioleDB {
c.Experimental.OrioleDBVersion = "15.1.0.150"
}
// Create config file
if err := MkdirIfNotExistFS(fsys, SupabaseDirPath); err != nil {
return err
}
flag := os.O_WRONLY | os.O_CREATE
if params.Overwrite {
flag |= os.O_TRUNC
} else {
flag |= os.O_EXCL
}
f, err := fsys.OpenFile(ConfigPath, flag, 0644)
if err != nil {
return errors.Errorf("failed to create config file: %w", err)
}
defer f.Close()
return c.Eject(f)
}
func WriteConfig(fsys afero.Fs, _test bool) error {
return InitConfig(InitParams{}, fsys)
}
func GetApiUrl(path string) string {
if len(Config.Api.ExternalUrl) > 0 {
return Config.Api.ExternalUrl + path
}
hostPort := net.JoinHostPort(Config.Hostname,
strconv.FormatUint(uint64(Config.Api.Port), 10),
)
apiUrl := url.URL{
Scheme: "http",
Host: hostPort,
Path: path,
}
return apiUrl.String()
}