-
Notifications
You must be signed in to change notification settings - Fork 5
/
api.go
38 lines (31 loc) · 1010 Bytes
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package api
import (
"encoding/json"
"net/http"
"github.com/moov-io/ach-web-viewer/pkg/filelist"
"github.com/moov-io/base/log"
"github.com/gorilla/mux"
)
func AppendRoutes(logger log.Logger, router *mux.Router, listers filelist.Listers) {
router.Methods("GET").Path("/files").HandlerFunc(listFiles(logger, listers))
}
type wrapper struct {
Sources map[string]filelist.Files
}
func listFiles(logger log.Logger, listers filelist.Listers) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
opts, err := filelist.ReadListOptions(r)
if err != nil {
logger.Set("service", log.String("api")).Error().LogErrorf("problem reading list params: %v", err)
w.WriteHeader(http.StatusInternalServerError)
}
resp, err := listers.GetFiles(opts)
if err != nil {
logger.Set("service", log.String("api")).Error().LogErrorf("problem listing files: %v", err)
w.WriteHeader(http.StatusInternalServerError)
}
json.NewEncoder(w).Encode(wrapper{
Sources: resp,
})
}
}