@@ -8,10 +8,10 @@ import (
88 "net/http/httputil"
99 "net/url"
1010 "path/filepath"
11- "time"
1211
1312 "github.com/fsnotify/fsnotify"
1413 "github.com/go-chi/cors"
14+ "github.com/go-logr/stdr"
1515 "github.com/hyprmcp/mcp-gateway/config"
1616 "github.com/hyprmcp/mcp-gateway/log"
1717 "github.com/hyprmcp/mcp-gateway/oauth"
@@ -23,17 +23,21 @@ type ServeOptions struct {
2323 Config string
2424 Addr string
2525 AuthProxyAddr string
26+ Verbosity int
2627}
2728
2829func BindServeOptions (cmd * cobra.Command , opts * ServeOptions ) {
2930 cmd .Flags ().StringVarP (& opts .Config , "config" , "c" , "config.yaml" , "Path to the configuration file" )
3031 cmd .Flags ().StringVarP (& opts .Addr , "addr" , "a" , ":9000" , "Address to listen on" )
3132 cmd .Flags ().StringVar (& opts .AuthProxyAddr , "auth-proxy-addr" , "" , "Address to listen on with the authentication server proxy (advanced feature)" )
33+ cmd .Flags ().IntVarP (& opts .Verbosity , "verbosity" , "v" , 0 , "Set the logging verbosity; greater number means more logging" )
3234}
3335
3436func runServe (ctx context.Context , opts ServeOptions ) error {
3537 done := make (chan error )
3638
39+ stdr .SetVerbosity (opts .Verbosity )
40+
3741 cfg , err := config .ParseFile (opts .Config )
3842 if err != nil {
3943 return err
@@ -57,7 +61,10 @@ func runServe(ctx context.Context, opts ServeOptions) error {
5761
5862 handler := & delegateHandler {}
5963
60- if h , err := newRouter (ctx , cfg ); err != nil {
64+ routerCtx , routerCancel := context .WithCancel (ctx )
65+ defer func () { routerCancel () }()
66+
67+ if h , err := newRouter (routerCtx , cfg ); err != nil {
6168 return err
6269 } else {
6370 handler .delegate = h
@@ -67,10 +74,15 @@ func runServe(ctx context.Context, opts ServeOptions) error {
6774 err := WatchConfigChanges (
6875 opts .Config ,
6976 func (c * config.Config ) {
77+ newRouterCtx , newRouterCancel := context .WithCancel (ctx )
7078 log .Get (ctx ).Info ("Reconfiguring server after config change..." )
71- if h , err := newRouter (ctx , c ); err != nil {
79+ if h , err := newRouter (newRouterCtx , c ); err != nil {
80+ newRouterCancel ()
7281 log .Get (ctx ).Error (err , "failed to reload server" )
7382 } else {
83+ routerCancel ()
84+ routerCancel = newRouterCancel
85+ routerCtx = newRouterCtx
7486 handler .delegate = h
7587 }
7688 },
@@ -95,9 +107,7 @@ func runServe(ctx context.Context, opts ServeOptions) error {
95107func newRouter (ctx context.Context , config * config.Config ) (http.Handler , error ) {
96108 mux := http .NewServeMux ()
97109
98- newMgrCtx , cancel := context .WithTimeout (ctx , 10 * time .Second )
99- defer cancel ()
100- oauthManager , err := oauth .NewManager (newMgrCtx , config )
110+ oauthManager , err := oauth .NewManager (ctx , config )
101111 if err != nil {
102112 return nil , err
103113 }
0 commit comments