@@ -25,11 +25,69 @@ type httpTransportConfigurable interface {
2525// HTTPTransportOption is a function that configures an httpTransportConfigurable.
2626type HTTPTransportOption func (httpTransportConfigurable )
2727
28+ // Option interfaces and wrappers for server configuration
29+ // Base option interface
30+ type HTTPServerOption interface {
31+ isHTTPServerOption ()
32+ }
33+
34+ // SSE-specific option interface
35+ type SSEOption interface {
36+ HTTPServerOption
37+ applyToSSE (* SSEServer )
38+ }
39+
40+ // StreamableHTTP-specific option interface
41+ type StreamableHTTPOption interface {
42+ HTTPServerOption
43+ applyToStreamableHTTP (* StreamableHTTPServer )
44+ }
45+
46+ // Common options that work with both server types
47+ type CommonHTTPServerOption interface {
48+ SSEOption
49+ StreamableHTTPOption
50+ }
51+
52+ // Wrapper for SSE-specific functional options
53+ type sseOption func (* SSEServer )
54+
55+ func (o sseOption ) isHTTPServerOption () {}
56+ func (o sseOption ) applyToSSE (s * SSEServer ) { o (s ) }
57+
58+ // Wrapper for StreamableHTTP-specific functional options
59+ type streamableHTTPOption func (* StreamableHTTPServer )
60+
61+ func (o streamableHTTPOption ) isHTTPServerOption () {}
62+ func (o streamableHTTPOption ) applyToStreamableHTTP (s * StreamableHTTPServer ) { o (s ) }
63+
64+ // Refactor commonOption to use a single apply func(httpTransportConfigurable)
65+ type commonOption struct {
66+ apply func (httpTransportConfigurable )
67+ }
68+
69+ func (o commonOption ) isHTTPServerOption () {}
70+ func (o commonOption ) applyToSSE (s * SSEServer ) { o .apply (s ) }
71+ func (o commonOption ) applyToStreamableHTTP (s * StreamableHTTPServer ) { o .apply (s ) }
72+
73+ // TODO: Implement StreamableHTTPServer in the future
74+ type StreamableHTTPServer struct {}
75+
76+ // Add stub methods to satisfy httpTransportConfigurable
77+
78+ func (s * StreamableHTTPServer ) setBasePath (string ) {}
79+ func (s * StreamableHTTPServer ) setDynamicBasePath (DynamicBasePathFunc ) {}
80+ func (s * StreamableHTTPServer ) setKeepAliveInterval (time.Duration ) {}
81+ func (s * StreamableHTTPServer ) setKeepAlive (bool ) {}
82+ func (s * StreamableHTTPServer ) setContextFunc (HTTPContextFunc ) {}
83+
2884// WithStaticBasePath adds a new option for setting a static base path.
2985// This is useful for mounting the server at a known, fixed path.
30- func WithStaticBasePath (basePath string ) HTTPTransportOption {
31- return func (c httpTransportConfigurable ) {
32- c .setBasePath (basePath )
86+ func WithStaticBasePath (basePath string ) CommonHTTPServerOption {
87+ return commonOption {
88+ apply : func (c httpTransportConfigurable ) {
89+ c .setBasePath (basePath )
90+ },
3391 }
3492}
3593
@@ -43,71 +101,80 @@ type DynamicBasePathFunc func(r *http.Request, sessionID string) string
43101// WithDynamicBasePath accepts a function for generating the base path.
44102// This is useful for cases where the base path is not known at the time of server creation,
45103// such as when using a reverse proxy or when the server is mounted at a dynamic path.
46- func WithDynamicBasePath (fn DynamicBasePathFunc ) HTTPTransportOption {
47- return func (c httpTransportConfigurable ) {
48- c .setDynamicBasePath (fn )
104+ func WithDynamicBasePath (fn DynamicBasePathFunc ) CommonHTTPServerOption {
105+ return commonOption {
106+ apply : func (c httpTransportConfigurable ) {
107+ c .setDynamicBasePath (fn )
108+ },
49109 }
50110}
51111
52112// WithKeepAliveInterval sets the keep-alive interval for the transport.
53113// When enabled, the server will periodically send ping events to keep the connection alive.
54- func WithKeepAliveInterval (interval time.Duration ) HTTPTransportOption {
55- return func (c httpTransportConfigurable ) {
56- c .setKeepAliveInterval (interval )
114+ func WithKeepAliveInterval (interval time.Duration ) CommonHTTPServerOption {
115+ return commonOption {
116+ apply : func (c httpTransportConfigurable ) {
117+ c .setKeepAliveInterval (interval )
118+ },
57119 }
58120}
59121
60122// WithKeepAlive enables or disables keep-alive for the transport.
61123// When enabled, the server will send periodic keep-alive events to clients.
62- func WithKeepAlive (keepAlive bool ) HTTPTransportOption {
63- return func (c httpTransportConfigurable ) {
64- c .setKeepAlive (keepAlive )
124+ func WithKeepAlive (keepAlive bool ) CommonHTTPServerOption {
125+ return commonOption {
126+ apply : func (c httpTransportConfigurable ) {
127+ c .setKeepAlive (keepAlive )
128+ },
65129 }
66130}
67131
68132// WithHTTPContextFunc sets a function that will be called to customize the context
69133// for the server using the incoming request. This is useful for injecting
70134// context values from headers or other request properties.
71- func WithHTTPContextFunc (fn HTTPContextFunc ) HTTPTransportOption {
72- return func (c httpTransportConfigurable ) {
73- c .setContextFunc (fn )
135+ func WithHTTPContextFunc (fn HTTPContextFunc ) CommonHTTPServerOption {
136+ return commonOption {
137+ apply : func (c httpTransportConfigurable ) {
138+ c .setContextFunc (fn )
139+ },
74140 }
75141}
76142
77143// WithBaseURL sets the base URL for the HTTP transport server.
78144// This is useful for configuring the externally visible base URL for clients.
79- func WithBaseURL (baseURL string ) HTTPTransportOption {
80- return func (c httpTransportConfigurable ) {
81- if s , ok := c .(* SSEServer ); ok {
82- if baseURL != "" {
83- u , err := url .Parse (baseURL )
84- if err != nil {
85- return
86- }
87- if u .Scheme != "http" && u .Scheme != "https" {
88- return
89- }
90- if u .Host == "" || strings .HasPrefix (u .Host , ":" ) {
91- return
92- }
93- if len (u .Query ()) > 0 {
94- return
145+ func WithBaseURL (baseURL string ) CommonHTTPServerOption {
146+ return commonOption {
147+ apply : func (c httpTransportConfigurable ) {
148+ if s , ok := c .(* SSEServer ); ok {
149+ if baseURL != "" {
150+ u , err := url .Parse (baseURL )
151+ if err != nil {
152+ return
153+ }
154+ if u .Scheme != "http" && u .Scheme != "https" {
155+ return
156+ }
157+ if u .Host == "" || strings .HasPrefix (u .Host , ":" ) {
158+ return
159+ }
160+ if len (u .Query ()) > 0 {
161+ return
162+ }
95163 }
164+ s .baseURL = strings .TrimSuffix (baseURL , "/" )
96165 }
97- s .baseURL = strings .TrimSuffix (baseURL , "/" )
98- }
99- // For future protocols, add similar logic here
166+ },
100167 }
101168}
102169
103170// WithHTTPServer sets the HTTP server instance for the transport.
104171// This is useful for advanced scenarios where you want to provide your own http.Server.
105- func WithHTTPServer (srv * http.Server ) HTTPTransportOption {
106- return func (c httpTransportConfigurable ) {
107- if s , ok := c .(* SSEServer ); ok {
108- s .srv = srv
109- }
110- // For future protocols, add similar logic here
172+ func WithHTTPServer (srv * http.Server ) CommonHTTPServerOption {
173+ return commonOption {
174+ apply : func (c httpTransportConfigurable ) {
175+ if s , ok := c .(* SSEServer ); ok {
176+ s .srv = srv
177+ }
178+ },
111179 }
112180}
113-
0 commit comments