projectMiddleware resolves ?project= and injects the DataProvider into request context.
(resolver *ProjectResolver, next http.Handler)
| 81 | |
| 82 | // projectMiddleware resolves ?project=<id> and injects the DataProvider into request context. |
| 83 | func projectMiddleware(resolver *ProjectResolver, next http.Handler) http.Handler { |
| 84 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 85 | projectID := r.URL.Query().Get("project") |
| 86 | if projectID == "" || resolver == nil { |
| 87 | next.ServeHTTP(w, r) |
| 88 | return |
| 89 | } |
| 90 | |
| 91 | pctx, err := resolver.get(projectID) |
| 92 | if err != nil { |
| 93 | status := http.StatusInternalServerError |
| 94 | if errors.Is(err, ErrProjectNotFound) { |
| 95 | status = http.StatusNotFound |
| 96 | } |
| 97 | writeError(w, status, fmt.Sprintf("project %q: %s", projectID, err.Error()), nil) |
| 98 | return |
| 99 | } |
| 100 | |
| 101 | ctx := context.WithValue(r.Context(), projectDPKey, pctx.dp) |
| 102 | ctx = context.WithValue(ctx, projectPhasesKey, pctx.phases) |
| 103 | next.ServeHTTP(w, r.WithContext(ctx)) |
| 104 | }) |
| 105 | } |
| 106 | |
| 107 | // effectiveDP returns the project-scoped DataProvider from request context, |
| 108 | // falling back to the default. |