@@ -10,13 +10,14 @@ import (
10
10
"github.com/larisgo/laravel-echo-server/errors"
11
11
"github.com/larisgo/laravel-echo-server/log"
12
12
"github.com/larisgo/laravel-echo-server/options"
13
+ "github.com/larisgo/laravel-echo-server/std"
13
14
"github.com/larisgo/laravel-echo-server/types"
14
- "github.com/tcnksm/go-input"
15
15
"io/ioutil"
16
16
"os"
17
17
"os/signal"
18
18
"path"
19
19
"path/filepath"
20
+ "regexp"
20
21
"strings"
21
22
"syscall"
22
23
"time"
@@ -143,143 +144,105 @@ func (this *Cli) resolveEnvFileOptions(config options.Config, args *Args) option
143
144
func (this * Cli ) setupConfig (defaultFile string ) (options.Config , string ) {
144
145
config := this .defaultOptions
145
146
146
- ui := & input.UI {
147
- Writer : os .Stdout ,
148
- Reader : os .Stdin ,
149
- }
150
- if devMode , err := ui .Select ("Do you want to run this server in development mode?" , []string {"true" , "false" }, & input.Options {
151
- Default : "false" ,
152
- Loop : true ,
153
- }); err != nil {
154
- log .Fatal (err )
155
- } else {
156
- config .DevMode = map [string ]bool {
157
- "true" : true ,
158
- "false" : false ,
159
- }[devMode ]
160
- }
161
- if port , err := ui .Ask ("Which port would you like to serve from?" , & input.Options {
162
- Default : "6001" ,
163
- Loop : true ,
164
- }); err != nil {
165
- log .Fatal (err )
166
- } else {
167
- config .Port = port
168
- }
169
-
170
- if database , err := ui .Select ("Which database would you like to use to store presence channel members?" , []string {"redis" , "sqlite" }, & input.Options {
171
- Default : "sqlite" ,
172
- Loop : true ,
173
- }); err != nil {
174
- log .Fatal (err )
175
- } else {
176
- config .Database = database
177
- }
178
-
179
- if authHost , err := ui .Ask ("Enter the host of your Laravel authentication server." , & input.Options {
180
- Default : "http://localhost" ,
181
- Loop : true ,
182
- }); err != nil {
183
- log .Fatal (err )
184
- } else {
185
- config .AuthHost = authHost
186
- }
147
+ input := std .NewDefaultInput (os .Stdin , std .NewDefaultOutput (os .Stdout ))
187
148
188
- protocol , err := ui .Select ("Will you be serving on http or https?" , []string {"http" , "https" }, & input.Options {
189
- Default : "http" ,
190
- Loop : true ,
191
- })
192
- if err != nil {
193
- log .Fatal (err )
149
+ if config .DevMode = input .Confirm ("Do you want to run this server in development mode?" , false ); config .DevMode {
150
+ log .Success ("Yes" )
194
151
} else {
195
- config . Protocol = protocol
152
+ log . Success ( "No" )
196
153
}
197
154
198
- if protocol == "https" {
199
- if sslCertPath , err := ui .Ask ("Enter the path to your SSL cert file." , & input.Options {
200
- Required : true ,
201
- Loop : true ,
202
- }); err != nil {
203
- log .Fatal (err )
155
+ config .Port = input .Ask ("Which port would you like to serve from:" , func (v string ) error {
156
+ if (len (v ) == 0 ) || regexp .MustCompile (`^([1-9]|[1-9]\d{1,3}|[1-6][0-5][0-5][0-3][0-5])$` ).MatchString (v ) {
157
+ return nil
204
158
} else {
205
- config . SslCertPath = sslCertPath
159
+ return errors . NewError ( "Port numbers range from 1 to 65535" )
206
160
}
161
+ }, "6001" )
162
+ log .Success (config .Port )
163
+
164
+ config .Database = input .Choose ("Which database would you like to use to store presence channel members?" , map [string ]string {
165
+ "redis" : "Use redis to store." ,
166
+ "sqlite" : "Use sqlite to store." ,
167
+ }, "sqlite" )
168
+ log .Success (config .Database )
169
+
170
+ config .AuthHost = input .Ask ("Enter the host of your Laravel authentication server:" , func (_ string ) error {
171
+ return nil
172
+ }, "http://localhost" )
173
+ log .Success (config .AuthHost )
174
+
175
+ config .Protocol = input .Choose ("Will you be serving on http or https?" , map [string ]string {
176
+ "http" : "Run the service using http." ,
177
+ "https" : "Run the service using https." ,
178
+ }, "http" )
179
+ log .Success (config .Protocol )
180
+
181
+ if config .Protocol == "https" {
182
+ config .SslCertPath = input .Ask ("Enter the path to your SSL cert file:" , func (v string ) error {
183
+ if len (v ) > 0 {
184
+ return nil
185
+ } else {
186
+ return errors .NewError ("Please enter ssl Cert Path." )
187
+ }
188
+ })
189
+ log .Success (config .SslCertPath )
190
+
191
+ config .SslKeyPath = input .Ask ("Enter the path to your SSL key file:" , func (v string ) error {
192
+ if len (v ) > 0 {
193
+ return nil
194
+ } else {
195
+ return errors .NewError ("Please enter ssl Key Path." )
196
+ }
197
+ })
198
+ log .Success (config .SslKeyPath )
199
+ }
207
200
208
- if sslKeyPath , err := ui .Ask ("Enter the path to your SSL key file." , & input.Options {
209
- Required : true ,
210
- Loop : true ,
211
- }); err != nil {
212
- log .Fatal (err )
213
- } else {
214
- config .SslKeyPath = sslKeyPath
201
+ if input .Confirm ("Do you want to generate a client ID/Key for HTTP API?" , false ) {
202
+ log .Success ("Yes" )
203
+ client := options.Client {
204
+ AppId : this .createAppId (),
205
+ Key : this .createApiKey (),
215
206
}
216
- }
207
+ if len (config .Clients ) == 0 {
208
+ config .Clients = []options.Client {}
209
+ }
210
+ config .Clients = append (config .Clients , client )
217
211
218
- if addClient , err := ui .Select ("Do you want to generate a client ID/Key for HTTP API?" , []string {"true" , "false" }, & input.Options {
219
- Default : "false" ,
220
- Loop : true ,
221
- }); err != nil {
222
- log .Fatal (err )
212
+ log .Info (fmt .Sprintf ("appId: %s" , client .AppId ))
213
+ log .Info (fmt .Sprintf ("key: %s" , client .Key ))
223
214
} else {
224
- if map [string ]bool {"true" : true , "false" : false }[addClient ] {
225
- client := options.Client {
226
- AppId : this .createAppId (),
227
- Key : this .createApiKey (),
228
- }
229
- if len (config .Clients ) == 0 {
230
- config .Clients = []options.Client {}
231
- }
232
- config .Clients = append (config .Clients , client )
233
- // log.Info(fmt.Sprintf("appId: %s", client.AppId))
234
- // log.Info(fmt.Sprintf("key: %s", client.Key))
235
- }
215
+ log .Success ("No" )
236
216
}
237
217
238
- if corsAllow , err := ui .Select ("Do you want to setup cross domain access to the API?" , []string {"true" , "false" }, & input.Options {
239
- Default : "false" ,
240
- Loop : true ,
241
- }); err != nil {
242
- log .Fatal (err )
218
+ if config .ApiOriginAllow .AllowCors = input .Confirm ("Do you want to setup cross domain access to the API?" , false ); config .ApiOriginAllow .AllowCors {
219
+ log .Success ("Yes" )
243
220
} else {
244
- config .ApiOriginAllow .AllowCors = map [string ]bool {
245
- "true" : true ,
246
- "false" : false ,
247
- }[corsAllow ]
221
+ log .Success ("No" )
248
222
}
249
223
250
224
if config .ApiOriginAllow .AllowCors {
251
- if allowOrigin , err := ui .Ask ("Specify the URI that may access the API:" , & input.Options {
252
- Default : "http://localhost:80" ,
253
- Loop : true ,
254
- }); err != nil {
255
- log .Fatal (err )
256
- } else {
257
- config .ApiOriginAllow .AllowOrigin = allowOrigin
258
- }
259
- if allowMethods , err := ui .Ask ("Enter the HTTP methods that are allowed for CORS:" , & input.Options {
260
- Default : "GET, POST" ,
261
- Loop : true ,
262
- }); err != nil {
263
- log .Fatal (err )
264
- } else {
265
- config .ApiOriginAllow .AllowMethods = allowMethods
266
- }
267
- if allowHeaders , err := ui .Ask ("Enter the HTTP headers that are allowed for CORS:" , & input.Options {
268
- Default : "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id" ,
269
- Loop : true ,
270
- }); err != nil {
271
- log .Fatal (err )
272
- } else {
273
- config .ApiOriginAllow .AllowHeaders = allowHeaders
274
- }
275
- }
276
- file , err := ui .Ask ("What do you want this config to be saved as?" , & input.Options {
277
- Default : defaultFile ,
278
- Loop : true ,
279
- })
280
- if err != nil {
281
- log .Fatal (err )
225
+ config .ApiOriginAllow .AllowOrigin = input .Ask ("Specify the URI that may access the API:" , func (_ string ) error {
226
+ return nil
227
+ }, "http://localhost:80" )
228
+ log .Success (config .ApiOriginAllow .AllowOrigin )
229
+
230
+ config .ApiOriginAllow .AllowMethods = input .Ask ("Enter the HTTP methods that are allowed for CORS:" , func (_ string ) error {
231
+ return nil
232
+ }, "GET, POST" )
233
+ log .Success (config .ApiOriginAllow .AllowMethods )
234
+
235
+ config .ApiOriginAllow .AllowHeaders = input .Ask ("Enter the HTTP headers that are allowed for CORS:" , func (_ string ) error {
236
+ return nil
237
+ }, "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id" )
238
+ log .Success (config .ApiOriginAllow .AllowHeaders )
282
239
}
240
+
241
+ file := input .Ask ("What do you want this config to be saved as:" , func (_ string ) error {
242
+ return nil
243
+ }, defaultFile )
244
+ log .Success (file )
245
+
283
246
return config , file
284
247
}
285
248
0 commit comments