4
4
package config
5
5
6
6
import (
7
+ "errors"
7
8
"fmt"
8
9
"net/http"
10
+ "os"
11
+ "path"
12
+ "strconv"
9
13
10
14
"github.com/daytonaio/daytona/pkg/types"
15
+ "github.com/google/uuid"
16
+
17
+ log "github.com/sirupsen/logrus"
11
18
)
12
19
13
20
const defaultRegistryUrl = "https://download.daytona.io/daytona"
@@ -28,6 +35,24 @@ var eu_defaultFrpsConfig = types.FRPSConfig{
28
35
}
29
36
30
37
func getDefaultFRPSConfig () * types.FRPSConfig {
38
+ frpsDomain := os .Getenv ("DEFAULT_FRPS_DOMAIN" )
39
+ fprsProtocol := os .Getenv ("DEFAULT_FRPS_PROTOCOL" )
40
+ frpsPort := os .Getenv ("DEFAULT_FRPS_PORT" )
41
+ if frpsDomain != "" && fprsProtocol != "" && frpsPort != "" {
42
+ port , err := parsePort (frpsPort )
43
+ if err != nil {
44
+ log .Error (fmt .Printf ("%s. Using default" , err ))
45
+ } else {
46
+ return & types.FRPSConfig {
47
+ Domain : frpsDomain ,
48
+ Port : port ,
49
+ Protocol : fprsProtocol ,
50
+ }
51
+ }
52
+ } else {
53
+ log .Info ("Using default FRPS config" )
54
+ }
55
+
31
56
// Return config which responds fastest to a ping
32
57
usReturnChan := make (chan bool )
33
58
euReturnChan := make (chan bool )
@@ -51,3 +76,93 @@ func getDefaultFRPSConfig() *types.FRPSConfig {
51
76
return & eu_defaultFrpsConfig
52
77
}
53
78
}
79
+
80
+ func getDefaultConfig () (* types.ServerConfig , error ) {
81
+ providersDir , err := getDefaultProvidersDir ()
82
+ if err != nil {
83
+ return nil , errors .New ("failed to get default providers dir" )
84
+ }
85
+
86
+ targetsPath , err := getDefaultTargetsPath ()
87
+ if err != nil {
88
+ return nil , errors .New ("failed to get default targets path" )
89
+ }
90
+
91
+ c := types.ServerConfig {
92
+ Id : generateUuid (),
93
+ GitProviders : []types.GitProvider {},
94
+ RegistryUrl : defaultRegistryUrl ,
95
+ ProvidersDir : providersDir ,
96
+ ServerDownloadUrl : defaultServerDownloadUrl ,
97
+ ApiPort : defaultApiPort ,
98
+ HeadscalePort : defaultHeadscalePort ,
99
+ TargetsFilePath : targetsPath ,
100
+ Frps : getDefaultFRPSConfig (),
101
+ }
102
+
103
+ if os .Getenv ("DEFAULT_REGISTRY_URL" ) != "" {
104
+ c .RegistryUrl = os .Getenv ("DEFAULT_REGISTRY_URL" )
105
+ }
106
+ if os .Getenv ("DEFAULT_SERVER_DOWNLOAD_URL" ) != "" {
107
+ c .ServerDownloadUrl = os .Getenv ("DEFAULT_SERVER_DOWNLOAD_URL" )
108
+ }
109
+ if os .Getenv ("DEFAULT_PROVIDERS_DIR" ) != "" {
110
+ c .ProvidersDir = os .Getenv ("DEFAULT_PROVIDERS_DIR" )
111
+ }
112
+ if os .Getenv ("DEFAULT_TARGETS_FILE_PATH" ) != "" {
113
+ c .TargetsFilePath = os .Getenv ("DEFAULT_TARGETS_FILE_PATH" )
114
+ }
115
+ if os .Getenv ("DEFAULT_API_PORT" ) != "" {
116
+ apiPort , err := parsePort (os .Getenv ("DEFAULT_API_PORT" ))
117
+ if err != nil {
118
+ log .Error (fmt .Printf ("%s. Using %d" , err , defaultApiPort ))
119
+ } else {
120
+ c .ApiPort = apiPort
121
+ }
122
+ }
123
+ if os .Getenv ("DEFAULT_HEADSCALE_PORT" ) != "" {
124
+ headscalePort , err := parsePort (os .Getenv ("DEFAULT_HEADSCALE_PORT" ))
125
+ if err != nil {
126
+ log .Error (fmt .Printf ("%s. Using %d" , err , defaultHeadscalePort ))
127
+ } else {
128
+ c .HeadscalePort = headscalePort
129
+ }
130
+ }
131
+
132
+ return & c , nil
133
+ }
134
+
135
+ func parsePort (port string ) (uint32 , error ) {
136
+ p , err := strconv .Atoi (port )
137
+ if err != nil {
138
+ return 0 , errors .New ("failed to parse port" )
139
+ }
140
+ if p < 0 || p > 65535 {
141
+ return 0 , errors .New ("port out of range" )
142
+ }
143
+
144
+ return uint32 (p ), nil
145
+ }
146
+
147
+ func getDefaultProvidersDir () (string , error ) {
148
+ userConfigDir , err := os .UserConfigDir ()
149
+ if err != nil {
150
+ return "" , err
151
+ }
152
+
153
+ return path .Join (userConfigDir , "daytona" , "providers" ), nil
154
+ }
155
+
156
+ func getDefaultTargetsPath () (string , error ) {
157
+ configDir , err := GetConfigDir ()
158
+ if err != nil {
159
+ return "" , err
160
+ }
161
+
162
+ return path .Join (configDir , "targets.json" ), nil
163
+ }
164
+
165
+ func generateUuid () string {
166
+ uuid := uuid .New ()
167
+ return uuid .String ()
168
+ }
0 commit comments