1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
14 "git.curoverse.com/arvados.git/sdk/go/arvados"
15 "git.curoverse.com/arvados.git/sdk/go/health"
16 "git.curoverse.com/arvados.git/sdk/go/httpserver"
20 Cluster *arvados.Cluster
21 NodeProfile *arvados.NodeProfile
24 handlerStack http.Handler
25 proxyClient *arvados.Client
28 func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
29 h.setupOnce.Do(h.setup)
30 h.handlerStack.ServeHTTP(w, req)
33 func (h *Handler) CheckHealth() error {
34 h.setupOnce.Do(h.setup)
35 _, err := findRailsAPI(h.Cluster, h.NodeProfile)
39 func (h *Handler) setup() {
40 mux := http.NewServeMux()
41 mux.Handle("/_health/", &health.Handler{
42 Token: h.Cluster.ManagementToken,
45 hs := http.NotFoundHandler()
46 hs = prepend(hs, h.proxyRailsAPI)
47 hs = prepend(hs, h.proxyRemoteCluster)
52 type middlewareFunc func(http.ResponseWriter, *http.Request, http.Handler)
54 func prepend(next http.Handler, middleware middlewareFunc) http.Handler {
55 return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
56 middleware(w, req, next)
60 func (h *Handler) proxyRailsAPI(w http.ResponseWriter, req *http.Request, next http.Handler) {
61 urlOut, err := findRailsAPI(h.Cluster, h.NodeProfile)
63 httpserver.Error(w, err.Error(), http.StatusInternalServerError)
67 Scheme: urlOut.Scheme,
70 RawPath: req.URL.RawPath,
71 RawQuery: req.URL.RawQuery,
73 h.proxy(w, req, urlOut)
76 // For now, findRailsAPI always uses the rails API running on this
78 func findRailsAPI(cluster *arvados.Cluster, np *arvados.NodeProfile) (*url.URL, error) {
79 hostport := np.RailsAPI.Listen
80 if len(hostport) > 1 && hostport[0] == ':' && strings.TrimRight(hostport[1:], "0123456789") == "" {
81 // ":12345" => connect to indicated port on localhost
82 hostport = "localhost" + hostport
83 } else if _, _, err := net.SplitHostPort(hostport); err == nil {
84 // "[::1]:12345" => connect to indicated address & port
92 return url.Parse(proto + "://" + hostport)