- Notifications
You must be signed in to change notification settings - Fork1k
feat: add MCP HTTP server experiment and improve experiment middleware#18712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -3,21 +3,59 @@ package httpmw | ||
import ( | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
"github.com/coder/coder/v2/buildinfo" | ||
"github.com/coder/coder/v2/coderd/httpapi" | ||
"github.com/coder/coder/v2/codersdk" | ||
) | ||
// RequireExperiment returns middleware that checks if all required experiments are enabled. | ||
// If any experiment is disabled, it returns a 403 Forbidden response with details about the missing experiments. | ||
func RequireExperiment(experiments codersdk.Experiments, requiredExperiments ...codersdk.Experiment) func(next http.Handler) http.Handler { | ||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
for _, experiment := range requiredExperiments { | ||
if !experiments.Enabled(experiment) { | ||
var experimentNames []string | ||
for _, exp := range requiredExperiments { | ||
experimentNames = append(experimentNames, string(exp)) | ||
} | ||
// Print a message that includes the experiment names | ||
// even if some experiments are already enabled. | ||
var message string | ||
if len(requiredExperiments) == 1 { | ||
message = fmt.Sprintf("%s functionality requires enabling the '%s' experiment.", | ||
requiredExperiments[0].DisplayName(), requiredExperiments[0]) | ||
} else { | ||
message = fmt.Sprintf("This functionality requires enabling the following experiments: %s", | ||
strings.Join(experimentNames, ", ")) | ||
} | ||
httpapi.Write(r.Context(), w, http.StatusForbidden, codersdk.Response{ | ||
Message: message, | ||
}) | ||
return | ||
} | ||
} | ||
next.ServeHTTP(w, r) | ||
}) | ||
} | ||
} | ||
// RequireExperimentWithDevBypass checks if ALL the given experiments are enabled, | ||
// but bypasses the check in development mode (buildinfo.IsDev()). | ||
func RequireExperimentWithDevBypass(experiments codersdk.Experiments, requiredExperiments ...codersdk.Experiment) func(next http.Handler) http.Handler { | ||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if buildinfo.IsDev() { | ||
next.ServeHTTP(w, r) | ||
return | ||
} | ||
RequireExperiment(experiments, requiredExperiments...)(next).ServeHTTP(w, r) | ||
}) | ||
} | ||
} | ||
ThomasK33 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. |
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.