@@ -37,6 +37,7 @@ import (
3737
3838 "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
3939 "github.com/linuxsuren/api-testing/pkg/logging"
40+ "github.com/linuxsuren/api-testing/pkg/mock"
4041 "github.com/linuxsuren/api-testing/pkg/oauth"
4142 template "github.com/linuxsuren/api-testing/pkg/render"
4243 "github.com/linuxsuren/api-testing/pkg/server"
@@ -88,6 +89,7 @@ func createServerCmd(execer fakeruntime.Execer, httpServer server.HTTPServer) (c
8889 flags .StringVarP (& opt .clientID , "client-id" , "" , os .Getenv ("OAUTH_CLIENT_ID" ), "ClientID is the application's ID" )
8990 flags .StringVarP (& opt .clientSecret , "client-secret" , "" , os .Getenv ("OAUTH_CLIENT_SECRET" ), "ClientSecret is the application's secret" )
9091 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" )
9193
9294 // gc related flags
9395 flags .IntVarP (& opt .gcPercent , "gc-percent" , "" , 100 , "The GC percent of Go" )
@@ -121,6 +123,8 @@ type serverOption struct {
121123 oauthSkipTls bool
122124 oauthGroup []string
123125
126+ mockConfig []string
127+
124128 gcPercent int
125129
126130 dryRun bool
@@ -283,10 +287,24 @@ func (o *serverOption) runE(cmd *cobra.Command, args []string) (err error) {
283287 promhttp .HandlerFor (reg , promhttp.HandlerOpts {Registry : reg }).ServeHTTP (w , r )
284288 })
285289
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+
286303 debugHandler (mux , remoteServer )
287- o .httpServer .WithHandler (mux )
304+ o .httpServer .WithHandler (combineHandlers . GetHandler () )
288305 serverLogger .Info ("HTTP server listening at" , "addr" , httplis .Addr ())
289306 serverLogger .Info ("Server is running." )
307+
290308 err = o .httpServer .Serve (httplis )
291309 err = util .IgnoreErrServerClosed (err )
292310 }
0 commit comments