-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactored admin server and api + health endpoint tests
- Loading branch information
Showing
14 changed files
with
127 additions
and
141 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package admin | ||
|
||
import ( | ||
"log/slog" | ||
"net/http" | ||
|
||
"github.com/gorilla/handlers" | ||
"github.com/gorilla/mux" | ||
"github.com/vechain/thor/v2/api/admin/loglevel" | ||
"github.com/vechain/thor/v2/health" | ||
|
||
healthAPI "github.com/vechain/thor/v2/api/admin/health" | ||
) | ||
|
||
func New(logLevel *slog.LevelVar, health *health.Health) http.HandlerFunc { | ||
router := mux.NewRouter() | ||
router.PathPrefix("/admin") | ||
|
||
loglevel.New(logLevel).Mount(router, "/loglevel") | ||
healthAPI.New(health).Mount(router, "/health") | ||
|
||
handler := handlers.CompressHandler(router) | ||
|
||
return handler.ServeHTTP | ||
} |
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,52 @@ | ||
// 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 health | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/vechain/thor/v2/health" | ||
) | ||
|
||
var ts *httptest.Server | ||
|
||
func TestHealth(t *testing.T) { | ||
initAPIServer(t) | ||
|
||
var healthStatus health.Status | ||
respBody, statusCode := httpGet(t, ts.URL+"/health") | ||
require.NoError(t, json.Unmarshal(respBody, &healthStatus)) | ||
assert.False(t, healthStatus.Healthy) | ||
assert.Equal(t, http.StatusServiceUnavailable, statusCode) | ||
} | ||
|
||
func initAPIServer(_ *testing.T) { | ||
router := mux.NewRouter() | ||
New(&health.Health{}).Mount(router, "/health") | ||
|
||
ts = httptest.NewServer(router) | ||
} | ||
|
||
func httpGet(t *testing.T, url string) ([]byte, int) { | ||
res, err := http.Get(url) //#nosec G107 | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer res.Body.Close() | ||
|
||
r, err := io.ReadAll(res.Body) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
return r, res.StatusCode | ||
} |
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package loglevel | ||
|
||
type Request struct { | ||
Level string `json:"level"` | ||
} | ||
|
||
type Response struct { | ||
CurrentLevel string `json:"currentLevel"` | ||
} |
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
Oops, something went wrong.