router.go 5 KB
Newer Older
1 2 3 4
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

5 6 7 8 9 10 11 12
package debugapi

import (
	"expvar"
	"net/http"
	"net/http/pprof"

	"github.com/gorilla/handlers"
13
	"github.com/gorilla/mux"
Janos Guljas's avatar
Janos Guljas committed
14
	"github.com/prometheus/client_golang/prometheus/promhttp"
15
	"github.com/sirupsen/logrus"
16
	"resenje.org/web"
17 18 19

	"github.com/ethersphere/bee/pkg/jsonhttp"
	"github.com/ethersphere/bee/pkg/logging/httpaccess"
20 21 22
)

func (s *server) setupRouting() {
23
	baseRouter := http.NewServeMux()
24

25
	baseRouter.Handle("/metrics", web.ChainHandlers(
26
		httpaccess.SetAccessLogLevelHandler(0), // suppress access log messages
27 28 29 30
		web.FinalHandler(promhttp.InstrumentMetricHandler(
			s.metricsRegistry,
			promhttp.HandlerFor(s.metricsRegistry, promhttp.HandlerOpts{}),
		)),
Janos Guljas's avatar
Janos Guljas committed
31 32
	))

33
	router := mux.NewRouter()
34
	router.NotFoundHandler = http.HandlerFunc(jsonhttp.NotFoundHandler)
35

36 37 38 39 40
	router.Handle("/debug/pprof", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		u := r.URL
		u.Path += "/"
		http.Redirect(w, r, u.String(), http.StatusPermanentRedirect)
	}))
41 42 43 44
	router.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
	router.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
	router.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
	router.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
45
	router.PathPrefix("/debug/pprof/").Handler(http.HandlerFunc(pprof.Index))
46

47
	router.Handle("/debug/vars", expvar.Handler())
48

49
	router.Handle("/health", web.ChainHandlers(
50
		httpaccess.SetAccessLogLevelHandler(0), // suppress access log messages
51 52 53
		web.FinalHandlerFunc(s.statusHandler),
	))
	router.Handle("/readiness", web.ChainHandlers(
54
		httpaccess.SetAccessLogLevelHandler(0), // suppress access log messages
55 56
		web.FinalHandlerFunc(s.statusHandler),
	))
57

58 59 60 61
	router.Handle("/pingpong/{peer-id}", jsonhttp.MethodHandler{
		"POST": http.HandlerFunc(s.pingpongHandler),
	})

62 63 64
	router.Handle("/addresses", jsonhttp.MethodHandler{
		"GET": http.HandlerFunc(s.addressesHandler),
	})
65
	router.Handle("/connect/{multi-address:.+}", jsonhttp.MethodHandler{
66 67
		"POST": http.HandlerFunc(s.peerConnectHandler),
	})
68 69 70
	router.Handle("/peers", jsonhttp.MethodHandler{
		"GET": http.HandlerFunc(s.peersHandler),
	})
71 72 73 74
	router.Handle("/blocklist", jsonhttp.MethodHandler{
		"GET": http.HandlerFunc(s.blocklistedPeersHandler),
	})

75
	router.Handle("/peers/{address}", jsonhttp.MethodHandler{
76 77
		"DELETE": http.HandlerFunc(s.peerDisconnectHandler),
	})
78
	router.Handle("/chunks/{address}", jsonhttp.MethodHandler{
79 80
		"GET":    http.HandlerFunc(s.hasChunkHandler),
		"DELETE": http.HandlerFunc(s.removeChunk),
81
	})
82 83 84
	router.Handle("/topology", jsonhttp.MethodHandler{
		"GET": http.HandlerFunc(s.topologyHandler),
	})
85 86 87 88 89 90 91
	router.Handle("/welcome-message", jsonhttp.MethodHandler{
		"GET": http.HandlerFunc(s.getWelcomeMessageHandler),
		"POST": web.ChainHandlers(
			jsonhttp.NewMaxBodyBytesHandler(welcomeMessageMaxRequestSize),
			web.FinalHandlerFunc(s.setWelcomeMessageHandler),
		),
	})
92

93
	router.Handle("/balances", jsonhttp.MethodHandler{
94
		"GET": http.HandlerFunc(s.compensatedBalancesHandler),
95
	})
96

97
	router.Handle("/balances/{peer}", jsonhttp.MethodHandler{
98 99 100 101 102 103 104 105
		"GET": http.HandlerFunc(s.compensatedPeerBalanceHandler),
	})

	router.Handle("/consumed", jsonhttp.MethodHandler{
		"GET": http.HandlerFunc(s.balancesHandler),
	})

	router.Handle("/consumed/{peer}", jsonhttp.MethodHandler{
106 107
		"GET": http.HandlerFunc(s.peerBalanceHandler),
	})
108

109 110 111
	router.Handle("/settlements", jsonhttp.MethodHandler{
		"GET": http.HandlerFunc(s.settlementsHandler),
	})
112

113 114 115 116
	router.Handle("/settlements/{peer}", jsonhttp.MethodHandler{
		"GET": http.HandlerFunc(s.peerSettlementsHandler),
	})

117 118 119 120 121 122 123 124 125
	if s.ChequebookEnabled {
		router.Handle("/chequebook/balance", jsonhttp.MethodHandler{
			"GET": http.HandlerFunc(s.chequebookBalanceHandler),
		})

		router.Handle("/chequebook/address", jsonhttp.MethodHandler{
			"GET": http.HandlerFunc(s.chequebookAddressHandler),
		})

126 127
		router.Handle("/chequebook/deposit", jsonhttp.MethodHandler{
			"POST": http.HandlerFunc(s.chequebookDepositHandler),
128 129
		})

130 131
		router.Handle("/chequebook/withdraw", jsonhttp.MethodHandler{
			"POST": http.HandlerFunc(s.chequebookWithdrawHandler),
Ralph Pichler's avatar
Ralph Pichler committed
132
		})
133

134 135 136
		router.Handle("/chequebook/cheque/{peer}", jsonhttp.MethodHandler{
			"GET": http.HandlerFunc(s.chequebookLastPeerHandler),
		})
137

138 139 140
		router.Handle("/chequebook/cheque", jsonhttp.MethodHandler{
			"GET": http.HandlerFunc(s.chequebookAllLastHandler),
		})
141

142 143 144 145 146
		router.Handle("/chequebook/cashout/{peer}", jsonhttp.MethodHandler{
			"GET":  http.HandlerFunc(s.swapCashoutStatusHandler),
			"POST": http.HandlerFunc(s.swapCashoutHandler),
		})
	}
147

148 149 150 151
	router.Handle("/tags/{id}", jsonhttp.MethodHandler{
		"GET": http.HandlerFunc(s.getTagHandler),
	})

152
	baseRouter.Handle("/", web.ChainHandlers(
153
		httpaccess.NewHTTPAccessLogHandler(s.Logger, logrus.InfoLevel, s.Tracer, "debug api access"),
154 155 156 157 158 159 160
		handlers.CompressHandler,
		// todo: add recovery handler
		web.NoCacheHeadersHandler,
		web.FinalHandler(router),
	))

	s.Handler = baseRouter
161
}