router.go 4.74 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
package debugapi

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

12
	"github.com/ethersphere/bee/pkg/jsonhttp"
13
	"github.com/ethersphere/bee/pkg/logging"
14
	"github.com/gorilla/handlers"
15
	"github.com/gorilla/mux"
Janos Guljas's avatar
Janos Guljas committed
16
	"github.com/prometheus/client_golang/prometheus/promhttp"
17
	"github.com/sirupsen/logrus"
18 19 20 21
	"resenje.org/web"
)

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

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

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

35 36 37 38 39
	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)
	}))
40 41 42 43
	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))
44
	router.PathPrefix("/debug/pprof/").Handler(http.HandlerFunc(pprof.Index))
45

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

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

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

61 62 63
	router.Handle("/addresses", jsonhttp.MethodHandler{
		"GET": http.HandlerFunc(s.addressesHandler),
	})
64
	router.Handle("/connect/{multi-address:.+}", jsonhttp.MethodHandler{
65 66
		"POST": http.HandlerFunc(s.peerConnectHandler),
	})
67 68 69
	router.Handle("/peers", jsonhttp.MethodHandler{
		"GET": http.HandlerFunc(s.peersHandler),
	})
70
	router.Handle("/peers/{address}", jsonhttp.MethodHandler{
71 72
		"DELETE": http.HandlerFunc(s.peerDisconnectHandler),
	})
73
	router.Handle("/chunks/{address}", jsonhttp.MethodHandler{
74 75
		"GET":    http.HandlerFunc(s.hasChunkHandler),
		"DELETE": http.HandlerFunc(s.removeChunk),
76
	})
77 78 79
	router.Handle("/topology", jsonhttp.MethodHandler{
		"GET": http.HandlerFunc(s.topologyHandler),
	})
80 81 82 83 84 85 86
	router.Handle("/welcome-message", jsonhttp.MethodHandler{
		"GET": http.HandlerFunc(s.getWelcomeMessageHandler),
		"POST": web.ChainHandlers(
			jsonhttp.NewMaxBodyBytesHandler(welcomeMessageMaxRequestSize),
			web.FinalHandlerFunc(s.setWelcomeMessageHandler),
		),
	})
87

88
	router.Handle("/balances", jsonhttp.MethodHandler{
89
		"GET": http.HandlerFunc(s.compensatedBalancesHandler),
90
	})
91

92
	router.Handle("/balances/{peer}", jsonhttp.MethodHandler{
93 94 95 96 97 98 99 100
		"GET": http.HandlerFunc(s.compensatedPeerBalanceHandler),
	})

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

	router.Handle("/consumed/{peer}", jsonhttp.MethodHandler{
101 102
		"GET": http.HandlerFunc(s.peerBalanceHandler),
	})
103

104 105 106
	router.Handle("/settlements", jsonhttp.MethodHandler{
		"GET": http.HandlerFunc(s.settlementsHandler),
	})
107

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

112 113 114 115 116 117 118 119 120
	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),
		})

121 122
		router.Handle("/chequebook/deposit", jsonhttp.MethodHandler{
			"POST": http.HandlerFunc(s.chequebookDepositHandler),
123 124
		})

125 126
		router.Handle("/chequebook/withdraw", jsonhttp.MethodHandler{
			"POST": http.HandlerFunc(s.chequebookWithdrawHandler),
Ralph Pichler's avatar
Ralph Pichler committed
127
		})
128
	}
129 130 131 132 133 134 135 136 137 138 139 140 141 142

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

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

	router.Handle("/chequebook/cashout/{peer}", jsonhttp.MethodHandler{
		"GET":  http.HandlerFunc(s.swapCashoutStatusHandler),
		"POST": http.HandlerFunc(s.swapCashoutHandler),
	})

143 144 145 146 147 148 149 150 151
	baseRouter.Handle("/", web.ChainHandlers(
		logging.NewHTTPAccessLogHandler(s.Logger, logrus.InfoLevel, "debug api access"),
		handlers.CompressHandler,
		// todo: add recovery handler
		web.NoCacheHeadersHandler,
		web.FinalHandler(router),
	))

	s.Handler = baseRouter
152
}