-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into paolo/feat/client
- Loading branch information
Showing
93 changed files
with
1,292 additions
and
795 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) 2024 The VeChainThor developers | ||
|
||
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying | ||
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> | ||
|
||
package api | ||
|
||
import ( | ||
"log/slog" | ||
"net/http" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/vechain/thor/v2/api/utils" | ||
"github.com/vechain/thor/v2/log" | ||
) | ||
|
||
type logLevelRequest struct { | ||
Level string `json:"level"` | ||
} | ||
|
||
type logLevelResponse struct { | ||
CurrentLevel string `json:"currentLevel"` | ||
} | ||
|
||
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(), | ||
}) | ||
} | ||
} | ||
|
||
func postLogLevelHandler(logLevel *slog.LevelVar) utils.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) error { | ||
var req logLevelRequest | ||
|
||
if err := utils.ParseJSON(r.Body, &req); err != nil { | ||
return utils.BadRequest(errors.WithMessage(err, "Invalid request body")) | ||
} | ||
|
||
switch req.Level { | ||
case "debug": | ||
logLevel.Set(log.LevelDebug) | ||
case "info": | ||
logLevel.Set(log.LevelInfo) | ||
case "warn": | ||
logLevel.Set(log.LevelWarn) | ||
case "error": | ||
logLevel.Set(log.LevelError) | ||
case "trace": | ||
logLevel.Set(log.LevelTrace) | ||
case "crit": | ||
logLevel.Set(log.LevelCrit) | ||
default: | ||
return utils.BadRequest(errors.New("Invalid verbosity level")) | ||
} | ||
|
||
return utils.WriteJSON(w, logLevelResponse{ | ||
CurrentLevel: logLevel.Level().String(), | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) 2024 The VeChainThor developers | ||
|
||
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying | ||
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> | ||
|
||
package api | ||
|
||
import ( | ||
"log/slog" | ||
"net" | ||
"net/http" | ||
"time" | ||
|
||
"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" | ||
) | ||
|
||
func HTTPHandler(logLevel *slog.LevelVar) http.Handler { | ||
router := mux.NewRouter() | ||
sub := router.PathPrefix("/admin").Subrouter() | ||
sub.Path("/loglevel"). | ||
Methods(http.MethodGet). | ||
Name("get-log-level"). | ||
HandlerFunc(utils.WrapHandlerFunc(getLogLevelHandler(logLevel))) | ||
|
||
sub.Path("/loglevel"). | ||
Methods(http.MethodPost). | ||
Name("post-log-level"). | ||
HandlerFunc(utils.WrapHandlerFunc(postLogLevelHandler(logLevel))) | ||
|
||
return handlers.CompressHandler(router) | ||
} | ||
|
||
func StartAdminServer(addr string, logLevel *slog.LevelVar) (string, func(), error) { | ||
listener, err := net.Listen("tcp", addr) | ||
if err != nil { | ||
return "", nil, errors.Wrapf(err, "listen admin API addr [%v]", addr) | ||
} | ||
|
||
router := mux.NewRouter() | ||
router.PathPrefix("/admin").Handler(HTTPHandler(logLevel)) | ||
handler := handlers.CompressHandler(router) | ||
|
||
srv := &http.Server{Handler: handler, ReadHeaderTimeout: time.Second, ReadTimeout: 5 * time.Second} | ||
var goes co.Goes | ||
goes.Go(func() { | ||
srv.Serve(listener) | ||
}) | ||
return "http://" + listener.Addr().String() + "/admin", func() { | ||
srv.Close() | ||
goes.Wait() | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright (c) 2024 The VeChainThor developers | ||
|
||
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying | ||
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> | ||
|
||
package api | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"log/slog" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type TestCase struct { | ||
name string | ||
method string | ||
body interface{} | ||
expectedStatus int | ||
expectedLevel string | ||
expectedErrorMsg string | ||
} | ||
|
||
func marshalBody(tt TestCase, t *testing.T) []byte { | ||
var reqBody []byte | ||
var err error | ||
if tt.body != nil { | ||
reqBody, err = json.Marshal(tt.body) | ||
if err != nil { | ||
t.Fatalf("could not marshal request body: %v", err) | ||
} | ||
} | ||
return reqBody | ||
} | ||
|
||
func TestLogLevelHandler(t *testing.T) { | ||
tests := []TestCase{ | ||
{ | ||
name: "Valid POST input - set level to DEBUG", | ||
method: "POST", | ||
body: map[string]string{"level": "debug"}, | ||
expectedStatus: http.StatusOK, | ||
expectedLevel: "DEBUG", | ||
}, | ||
{ | ||
name: "Invalid POST input - invalid level", | ||
method: "POST", | ||
body: map[string]string{"level": "invalid_body"}, | ||
expectedStatus: http.StatusBadRequest, | ||
expectedErrorMsg: "Invalid verbosity level", | ||
}, | ||
{ | ||
name: "GET request - get current level INFO", | ||
method: "GET", | ||
body: nil, | ||
expectedStatus: http.StatusOK, | ||
expectedLevel: "INFO", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var logLevel slog.LevelVar | ||
logLevel.Set(slog.LevelInfo) | ||
|
||
reqBodyBytes := marshalBody(tt, t) | ||
|
||
req, err := http.NewRequest(tt.method, "/admin/loglevel", bytes.NewBuffer(reqBodyBytes)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
rr := httptest.NewRecorder() | ||
handler := http.HandlerFunc(HTTPHandler(&logLevel).ServeHTTP) | ||
handler.ServeHTTP(rr, req) | ||
|
||
if status := rr.Code; status != tt.expectedStatus { | ||
t.Errorf("handler returned wrong status code: got %v want %v", status, tt.expectedStatus) | ||
} | ||
|
||
if tt.expectedLevel != "" { | ||
var response logLevelResponse | ||
if err := json.NewDecoder(rr.Body).Decode(&response); err != nil { | ||
t.Fatalf("could not decode response: %v", err) | ||
} | ||
if response.CurrentLevel != tt.expectedLevel { | ||
t.Errorf("handler returned unexpected log level: got %v want %v", response.CurrentLevel, tt.expectedLevel) | ||
} | ||
} else { | ||
assert.Equal(t, tt.expectedErrorMsg, strings.Trim(rr.Body.String(), "\n")) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.