getConfig returns the request configuration.
(w http.ResponseWriter, r *http.Request)
| 111 | |
| 112 | // getConfig returns the request configuration. |
| 113 | func (a *API) getConfig(w http.ResponseWriter, r *http.Request) { |
| 114 | userID, _, err := users.ExtractTenantIDFromHTTPRequest(r) |
| 115 | if err != nil { |
| 116 | http.Error(w, err.Error(), http.StatusUnauthorized) |
| 117 | return |
| 118 | } |
| 119 | logger := util_log.WithContext(r.Context(), util_log.Logger) |
| 120 | |
| 121 | cfg, err := a.db.GetConfig(r.Context(), userID) |
| 122 | if err == sql.ErrNoRows { |
| 123 | http.Error(w, "No configuration", http.StatusNotFound) |
| 124 | return |
| 125 | } else if err != nil { |
| 126 | // XXX: Untested |
| 127 | level.Error(logger).Log("msg", "error getting config", "err", err) |
| 128 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 129 | return |
| 130 | } |
| 131 | |
| 132 | switch parseConfigFormat(r.Header.Get("Accept"), FormatJSON) { |
| 133 | case FormatJSON: |
| 134 | w.Header().Set("Content-Type", "application/json") |
| 135 | err = json.NewEncoder(w).Encode(cfg) |
| 136 | case FormatYAML: |
| 137 | w.Header().Set("Content-Type", "application/yaml") |
| 138 | err = yaml.NewEncoder(w).Encode(cfg) |
| 139 | default: |
| 140 | // should never reach this point |
| 141 | level.Error(logger).Log("msg", "unexpected error detecting the config format") |
| 142 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 143 | } |
| 144 | if err != nil { |
| 145 | // XXX: Untested |
| 146 | level.Error(logger).Log("msg", "error encoding config", "err", err) |
| 147 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | func (a *API) setConfig(w http.ResponseWriter, r *http.Request) { |
| 152 | userID, _, err := users.ExtractTenantIDFromHTTPRequest(r) |
nothing calls this directly
no test coverage detected