(w http.ResponseWriter, r *http.Request)
| 140 | } |
| 141 | |
| 142 | func (p *HTTPPool) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 143 | // Parse request. |
| 144 | if !strings.HasPrefix(r.URL.Path, p.opts.BasePath) { |
| 145 | panic("HTTPPool serving unexpected path: " + r.URL.Path) |
| 146 | } |
| 147 | parts := strings.SplitN(r.URL.Path[len(p.opts.BasePath):], "/", 2) |
| 148 | if len(parts) != 2 { |
| 149 | http.Error(w, "bad request", http.StatusBadRequest) |
| 150 | return |
| 151 | } |
| 152 | groupName := parts[0] |
| 153 | key := parts[1] |
| 154 | |
| 155 | // Fetch the value for this group/key. |
| 156 | group := GetGroup(groupName) |
| 157 | if group == nil { |
| 158 | http.Error(w, "no such group: "+groupName, http.StatusNotFound) |
| 159 | return |
| 160 | } |
| 161 | var ctx context.Context |
| 162 | if p.Context != nil { |
| 163 | ctx = p.Context(r) |
| 164 | } else { |
| 165 | ctx = r.Context() |
| 166 | } |
| 167 | |
| 168 | group.Stats.ServerRequests.Add(1) |
| 169 | var value []byte |
| 170 | err := group.Get(ctx, key, AllocatingByteSliceSink(&value)) |
| 171 | if err != nil { |
| 172 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 173 | return |
| 174 | } |
| 175 | |
| 176 | // Write the value to the response body as a proto message. |
| 177 | body, err := proto.Marshal(&pb.GetResponse{Value: value}) |
| 178 | if err != nil { |
| 179 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 180 | return |
| 181 | } |
| 182 | w.Header().Set("Content-Type", "application/x-protobuf") |
| 183 | w.Write(body) |
| 184 | } |
| 185 | |
| 186 | type httpGetter struct { |
| 187 | transport func(context.Context) http.RoundTripper |
nothing calls this directly
no test coverage detected