Skip to content

Commit

Permalink
leverage api uitls in admin server (#839)
Browse files Browse the repository at this point in the history
  • Loading branch information
libotony authored Sep 11, 2024
1 parent b8253e1 commit 9bc09fc
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 96 deletions.
46 changes: 14 additions & 32 deletions api/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
package api

import (
"encoding/json"
"log/slog"
"net/http"

"github.com/pkg/errors"
"github.com/vechain/thor/v2/api/utils"
"github.com/vechain/thor/v2/log"
)

Expand All @@ -21,35 +22,20 @@ type logLevelResponse struct {
CurrentLevel string `json:"currentLevel"`
}

type errorResponse struct {
ErrorMessage string `json:"errorMessage"`
}

func writeError(w http.ResponseWriter, errCode int, errMsg string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(errCode)
json.NewEncoder(w).Encode(errorResponse{
ErrorMessage: errMsg,
})
}

func getLogLevelHandler(logLevel *slog.LevelVar) http.HandlerFunc {
return func(w http.ResponseWriter, _ *http.Request) {
response := logLevelResponse{
func getLogLevelHandler(logLevel *slog.LevelVar) utils.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) error {
return utils.WriteJSON(w, logLevelResponse{
CurrentLevel: logLevel.Level().String(),
}
if err := json.NewEncoder(w).Encode(response); err != nil {
writeError(w, http.StatusInternalServerError, "Failed to encode response")
}
})
}
}

func postLogLevelHandler(logLevel *slog.LevelVar) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
func postLogLevelHandler(logLevel *slog.LevelVar) utils.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) error {
var req logLevelRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "Invalid request body")
return

if err := utils.ParseJSON(r.Body, &req); err != nil {
return utils.BadRequest(errors.WithMessage(err, "Invalid request body"))
}

switch req.Level {
Expand All @@ -66,15 +52,11 @@ func postLogLevelHandler(logLevel *slog.LevelVar) http.HandlerFunc {
case "crit":
logLevel.Set(log.LevelCrit)
default:
writeError(w, http.StatusBadRequest, "Invalid verbosity level")
return
return utils.BadRequest(errors.New("Invalid verbosity level"))
}

w.Header().Set("Content-Type", "application/json")
response := logLevelResponse{
return utils.WriteJSON(w, logLevelResponse{
CurrentLevel: logLevel.Level().String(),
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(response)
})
}
}
5 changes: 3 additions & 2 deletions api/admin_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/vechain/thor/v2/api/utils"
"github.com/vechain/thor/v2/co"
)

Expand All @@ -23,12 +24,12 @@ func HTTPHandler(logLevel *slog.LevelVar) http.Handler {
sub.Path("/loglevel").
Methods(http.MethodGet).
Name("get-log-level").
HandlerFunc(getLogLevelHandler(logLevel))
HandlerFunc(utils.WrapHandlerFunc(getLogLevelHandler(logLevel)))

sub.Path("/loglevel").
Methods(http.MethodPost).
Name("post-log-level").
HandlerFunc(postLogLevelHandler(logLevel))
HandlerFunc(utils.WrapHandlerFunc(postLogLevelHandler(logLevel)))

return handlers.CompressHandler(router)
}
Expand Down
11 changes: 4 additions & 7 deletions api/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import (
"log/slog"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

type TestCase struct {
Expand Down Expand Up @@ -89,13 +92,7 @@ func TestLogLevelHandler(t *testing.T) {
t.Errorf("handler returned unexpected log level: got %v want %v", response.CurrentLevel, tt.expectedLevel)
}
} else {
var response errorResponse
if err := json.NewDecoder(rr.Body).Decode(&response); err != nil {
t.Fatalf("could not decode response: %v", err)
}
if response.ErrorMessage != tt.expectedErrorMsg {
t.Errorf("handler returned unexpected error message: got %v want %v", response.ErrorMessage, tt.expectedErrorMsg)
}
assert.Equal(t, tt.expectedErrorMsg, strings.Trim(rr.Body.String(), "\n"))
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/thor/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ var (

enableAdminFlag = cli.BoolFlag{
Name: "enable-admin",
Usage: "enables admin service",
Usage: "enables admin server",
}
adminAddrFlag = cli.StringFlag{
Name: "admin-addr",
Expand Down
8 changes: 4 additions & 4 deletions cmd/thor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,6 @@ func defaultAction(ctx *cli.Context) error {
}
defer func() { log.Info("closing main database..."); mainDB.Close() }()

skipLogs := ctx.Bool(skipLogsFlag.Name)

logDB, err := openLogDB(instanceDir)
if err != nil {
return err
Expand All @@ -224,6 +222,7 @@ func defaultAction(ctx *cli.Context) error {

printStartupMessage1(gene, repo, master, instanceDir, forkConfig)

skipLogs := ctx.Bool(skipLogsFlag.Name)
if !skipLogs {
if err := syncLogDB(exitSignal, repo, logDB, ctx.Bool(verifyLogsFlag.Name)); err != nil {
return err
Expand Down Expand Up @@ -377,8 +376,9 @@ func soloAction(ctx *cli.Context) error {
return err
}

skipLogs := ctx.Bool(skipLogsFlag.Name)
printStartupMessage1(gene, repo, nil, instanceDir, forkConfig)

skipLogs := ctx.Bool(skipLogsFlag.Name)
if !skipLogs {
if err := syncLogDB(exitSignal, repo, logDB, ctx.Bool(verifyLogsFlag.Name)); err != nil {
return err
Expand Down Expand Up @@ -435,7 +435,7 @@ func soloAction(ctx *cli.Context) error {
return errors.New("block-interval cannot be zero")
}

printSoloStartupMessage(gene, repo, instanceDir, apiURL, forkConfig, metricsURL, adminURL)
printStartupMessage2(gene, apiURL, "", metricsURL, adminURL)

optimizer := optimizer.New(mainDB, repo, !ctx.Bool(disablePrunerFlag.Name))
defer func() { log.Info("stopping optimizer..."); optimizer.Stop() }()
Expand Down
50 changes: 0 additions & 50 deletions cmd/thor/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,56 +664,6 @@ func printStartupMessage2(
)
}

func printSoloStartupMessage(
gene *genesis.Genesis,
repo *chain.Repository,
dataDir string,
apiURL string,
forkConfig thor.ForkConfig,
metricsURL string,
adminURL string,
) {
bestBlock := repo.BestBlockSummary()

info := fmt.Sprintf(`Starting %v
Network [ %v %v ]
Best block [ %v #%v @%v ]
Forks [ %v ]
Data dir [ %v ]
API portal [ %v ]
Metrics [ %v ]
Admin [ %v ]
`,
common.MakeName("Thor solo", fullVersion()),
gene.ID(), gene.Name(),
bestBlock.Header.ID(), bestBlock.Header.Number(), time.Unix(int64(bestBlock.Header.Timestamp()), 0),
forkConfig,
dataDir,
apiURL,
func() string {
if metricsURL == "" {
return "Disabled"
}
return metricsURL
}(),
func() string {
if adminURL == "" {
return "Disabled"
}
return adminURL
}(),
)

if gene.ID() == devNetGenesisID {
info += `┌──────────────────┬───────────────────────────────────────────────────────────────────────────────┐
│ Mnemonic Words │ denial kitchen pet squirrel other broom bar gas better priority spoil cross │
└──────────────────┴───────────────────────────────────────────────────────────────────────────────┘
`
}

fmt.Print(info)
}

func openMemMainDB() *muxdb.MuxDB {
return muxdb.NewMem()
}
Expand Down

0 comments on commit 9bc09fc

Please sign in to comment.