Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/missionmatch / internal/modules/awards/handler.go |
| Date | Not specified in source |
| Repository | Janady13/missionmatch |
| Revision | 720d47fae476b0d38477ae4401697d372b1ea2bc |
| Branch | feature/opportunity-to-outcome |
| Path or archive member | internal/modules/awards/handler.go |
| Original SHA-256 | 655fb796eee9b43d53f47afe07158168c745e08a0995fc7cfffdacba257ff506 |
| Git blob SHA | c995402d0704859ca1f7d444432c9eecf8078f40 |
| Reading-copy SHA-256 | 655fb796eee9b43d53f47afe07158168c745e08a0995fc7cfffdacba257ff506 |
Every recorded occurrence
These 2 records contain identical original source bytes. Their paths and revisions remain part of the archive.
| Original record | Branch or archive | Revision / identifier | Original location |
|---|---|---|---|
| Janady13/missionmatch / internal/modules/awards/handler.go | feature/opportunity-to-outcome | 720d47fae476b0d38477ae4401697d372b1ea2bc | Open original |
| Janady13/missionmatch / internal/modules/awards/handler.go | feature/aioe-upgrade | 2178e7298d0e5d9af83afa12402fd587f0ba6ed8 | Open original |
Recovered source transcript
Download complete source text ↓84 lines · 2.3 KB
package awards
import (
"encoding/json"
"errors"
"net/http"
"strconv"
"strings"
"github.com/go-chi/chi/v5"
"github.com/missionworkforce/missionmatch/internal/platform/security"
)
// Handler serves the cached awards read API.
type Handler struct{ svc *Service }
func NewHandler(svc *Service) *Handler { return &Handler{svc: svc} }
// Routes mounts the awards endpoints under the /api group.
// Routes mounts the cached federal award feed. Gated for the reason set out on the opportunity routes:
// an endpoint should name its own authority rather than inherit it from where it is mounted.
func (h *Handler) Routes(r chi.Router) {
read := security.RequireCapability(security.CapCandidateRead)
r.With(read).Get("/awards", h.incumbents)
r.With(read).Get("/awards/galapagos", h.galapagos)
}
// incumbents returns the largest comparable awards for one or more NAICS codes.
func (h *Handler) incumbents(w http.ResponseWriter, r *http.Request) {
naics := multiVals(r, "naics")
items, err := h.svc.Incumbents(r.Context(), naics, intParam(r, "limit", 0))
if err != nil {
writeErr(w, err)
return
}
writeJSON(w, http.StatusOK, items)
}
// galapagos returns Galapagos own past performance.
func (h *Handler) galapagos(w http.ResponseWriter, r *http.Request) {
items, err := h.svc.Galapagos(r.Context(), intParam(r, "limit", 0))
if err != nil {
writeErr(w, err)
return
}
writeJSON(w, http.StatusOK, items)
}
func multiVals(r *http.Request, key string) []string {
var out []string
for _, v := range r.URL.Query()[key] {
for _, p := range strings.Split(v, ",") {
if p = strings.TrimSpace(p); p != "" {
out = append(out, p)
}
}
}
return out
}
func intParam(r *http.Request, key string, def int) int {
if v := strings.TrimSpace(r.URL.Query().Get(key)); v != "" {
if n, err := strconv.Atoi(v); err == nil {
return n
}
}
return def
}
func writeErr(w http.ResponseWriter, err error) {
if errors.Is(err, ErrNoDatabase) {
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "database not configured"})
return
}
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "internal error"})
}
func writeJSON(w http.ResponseWriter, status int, v any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(v)
}