summaryrefslogtreecommitdiff
path: root/daemon/algod/api/server/lib/common.go
blob: e44e624c1b4cf366f630ec08816bc1731f3b2246 (plain)
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
66
67
68
69
70
71
72
// Copyright (C) 2019-2023 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package lib

import (
	"net/http"

	"github.com/labstack/echo/v4"

	"github.com/algorand/go-algorand/crypto"
	"github.com/algorand/go-algorand/logging"
	"github.com/algorand/go-algorand/node"
)

// GenesisJSONText is initialized when the node starts.
var GenesisJSONText string

// NodeInterface defines the node's methods required by the common APIs
type NodeInterface interface {
	GenesisHash() crypto.Digest
	GenesisID() string
	Status() (s node.StatusReport, err error)
}

// HandlerFunc defines a wrapper for http.HandlerFunc that includes a context
type HandlerFunc func(ReqContext, echo.Context)

// Route type description
type Route struct {
	Name        string
	Method      string
	Path        string
	HandlerFunc HandlerFunc
}

// Routes contains all routes
type Routes []Route

// ReqContext is passed to each of the handlers below via wrapCtx, allowing
// handlers to interact with the node
type ReqContext struct {
	Node     NodeInterface
	Log      logging.Logger
	Context  echo.Context
	Shutdown <-chan struct{}
}

// ErrorResponse sets the specified status code (should != 200), and fills in
// a human-readable error.
func ErrorResponse(w http.ResponseWriter, status int, internalErr error, publicErr string, logger logging.Logger) {
	logger.Info(internalErr)

	w.WriteHeader(status)
	_, err := w.Write([]byte(publicErr))
	if err != nil {
		logger.Errorf("algod failed to write response: %v", err)
	}
}