/
router.go
65 lines (56 loc) · 1.89 KB
/
router.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"net/http"
"os"
"strings"
log "github.com/sirupsen/logrus"
"github.com/CMSgov/bcda-app/bcda/auth"
"github.com/CMSgov/bcda-app/bcda/logging"
"github.com/go-chi/chi"
)
func NewAPIRouter() http.Handler {
r := chi.NewRouter()
r.Use(auth.ParseToken, logging.NewStructuredLogger())
// Serve up the swagger ui folder
FileServer(r, "/api/v1/swagger", http.Dir("./swaggerui"))
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("Hello world!"))
if err != nil {
log.Error(err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
})
r.Route("/api/v1", func(r chi.Router) {
r.With(auth.RequireTokenAuth, ValidateBulkRequestHeaders).Get("/ExplanationOfBenefit/$export", bulkRequest)
r.With(auth.RequireTokenAuth).Get("/jobs/{jobId}", jobStatus)
r.Get("/metadata", metadata)
if os.Getenv("DEBUG") == "true" {
r.Get("/token", getToken)
r.Get("/bb_metadata", blueButtonMetadata)
}
})
r.Get("/_version", getVersion)
return r
}
func NewDataRouter() http.Handler {
r := chi.NewRouter()
r.With(auth.ParseToken, logging.NewStructuredLogger(), auth.RequireTokenAuth, auth.RequireTokenACOMatch).Get("/data/{jobID}/{acoID}.ndjson", serveData)
return r
}
// FileServer conveniently sets up a http.FileServer handler to serve
// static files from a http.FileSystem.
// stolen from https://github.com/go-chi/chi/blob/master/_examples/fileserver/main.go
func FileServer(r chi.Router, path string, root http.FileSystem) {
if strings.ContainsAny(path, "{}*") {
panic("FileServer does not permit URL parameters.")
}
fs := http.StripPrefix(path, http.FileServer(root))
if path != "/" && path[len(path)-1] != '/' {
r.Get(path, http.RedirectHandler(path+"/", 301).ServeHTTP)
path += "/"
}
path += "*"
r.Get(path, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fs.ServeHTTP(w, r)
}))
}