@@ -37,6 +37,7 @@ import (
37
37
38
38
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
39
39
"github.com/linuxsuren/api-testing/pkg/logging"
40
+ "github.com/linuxsuren/api-testing/pkg/mock"
40
41
"github.com/linuxsuren/api-testing/pkg/oauth"
41
42
template "github.com/linuxsuren/api-testing/pkg/render"
42
43
"github.com/linuxsuren/api-testing/pkg/server"
@@ -88,6 +89,7 @@ func createServerCmd(execer fakeruntime.Execer, httpServer server.HTTPServer) (c
88
89
flags .StringVarP (& opt .clientID , "client-id" , "" , os .Getenv ("OAUTH_CLIENT_ID" ), "ClientID is the application's ID" )
89
90
flags .StringVarP (& opt .clientSecret , "client-secret" , "" , os .Getenv ("OAUTH_CLIENT_SECRET" ), "ClientSecret is the application's secret" )
90
91
flags .BoolVarP (& opt .dryRun , "dry-run" , "" , false , "Do not really start a gRPC server" )
92
+ flags .StringArrayVarP (& opt .mockConfig , "mock-config" , "" , nil , "The mock config files" )
91
93
92
94
// gc related flags
93
95
flags .IntVarP (& opt .gcPercent , "gc-percent" , "" , 100 , "The GC percent of Go" )
@@ -121,6 +123,8 @@ type serverOption struct {
121
123
oauthSkipTls bool
122
124
oauthGroup []string
123
125
126
+ mockConfig []string
127
+
124
128
gcPercent int
125
129
126
130
dryRun bool
@@ -283,10 +287,24 @@ func (o *serverOption) runE(cmd *cobra.Command, args []string) (err error) {
283
287
promhttp .HandlerFor (reg , promhttp.HandlerOpts {Registry : reg }).ServeHTTP (w , r )
284
288
})
285
289
290
+ combineHandlers := server .NewDefaultCombineHandler ()
291
+ combineHandlers .PutHandler ("" , mux )
292
+
293
+ if len (o .mockConfig ) > 0 {
294
+ cmd .Println ("currently only one mock config is supported, will take the first one" )
295
+ var mockServerHandler http.Handler
296
+ if mockServerHandler , err = mock .NewInMemoryServer (0 ).
297
+ SetupHandler (mock .NewLocalFileReader (o .mockConfig [0 ])); err != nil {
298
+ return
299
+ }
300
+ combineHandlers .PutHandler ("/mock" , mockServerHandler )
301
+ }
302
+
286
303
debugHandler (mux , remoteServer )
287
- o .httpServer .WithHandler (mux )
304
+ o .httpServer .WithHandler (combineHandlers . GetHandler () )
288
305
serverLogger .Info ("HTTP server listening at" , "addr" , httplis .Addr ())
289
306
serverLogger .Info ("Server is running." )
307
+
290
308
err = o .httpServer .Serve (httplis )
291
309
err = util .IgnoreErrServerClosed (err )
292
310
}
0 commit comments