mirror of
https://github.com/amir20/dozzle.git
synced 2026-01-01 02:27:25 +01:00
chore: changes APIs to be more restful and prepare for services (#2869)
This commit is contained in:
@@ -12,7 +12,7 @@
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a :href="`${base}/api/logs/download/${container.host}/${container.id}`" download>
|
||||
<a :href="`${base}/api/hosts/${container.host}/containers/${container.id}/logs/download`" download>
|
||||
<octicon:download-24 /> {{ $t("toolbar.download") }}
|
||||
</a>
|
||||
</li>
|
||||
|
||||
@@ -90,7 +90,7 @@ describe("<LogEventSource />", () => {
|
||||
});
|
||||
}
|
||||
|
||||
const sourceUrl = "/api/logs/stream/localhost/abc?stdout=1&stderr=1";
|
||||
const sourceUrl = "/api/hosts/localhost/containers/abc/logs/stream?stdout=1&stderr=1";
|
||||
|
||||
test("renders correctly", async () => {
|
||||
const wrapper = createLogEventSource();
|
||||
|
||||
@@ -85,7 +85,9 @@ export function useLogStream() {
|
||||
console.debug(`Connecting to ${containerId} with params`, params);
|
||||
|
||||
es = new EventSource(
|
||||
withBase(`/api/logs/stream/${container.value.host}/${containerId}?${new URLSearchParams(params).toString()}`),
|
||||
withBase(
|
||||
`/api/hosts/${container.value.host}/containers/${containerId}/logs/stream?${new URLSearchParams(params).toString()}`,
|
||||
),
|
||||
);
|
||||
es.addEventListener("container-stopped", () => {
|
||||
close();
|
||||
@@ -118,7 +120,9 @@ export function useLogStream() {
|
||||
|
||||
const logs = await (
|
||||
await fetch(
|
||||
withBase(`/api/logs/${container.value.host}/${containerId}?${new URLSearchParams(params).toString()}`),
|
||||
withBase(
|
||||
`/api/hosts/${container.value.host}/containers/${containerId}/logs?${new URLSearchParams(params).toString()}`,
|
||||
),
|
||||
)
|
||||
).text();
|
||||
if (logs) {
|
||||
|
||||
@@ -34,58 +34,58 @@ func Test_handler_containerActions_stop(t *testing.T) {
|
||||
mockedClient := get_mocked_client()
|
||||
|
||||
handler := createHandler(mockedClient, nil, Config{Base: "/", EnableActions: true, Authorization: Authorization{Provider: NONE}})
|
||||
req, err := http.NewRequest("POST", "/api/actions/stop/localhost/123", nil)
|
||||
req, err := http.NewRequest("POST", "/api/hosts/localhost/containers/123/actions/stop", nil)
|
||||
require.NoError(t, err, "Request should not return an error.")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rr, req)
|
||||
assert.Equal(t, rr.Code, 200)
|
||||
assert.Equal(t, 200, rr.Code)
|
||||
}
|
||||
|
||||
func Test_handler_containerActions_restart(t *testing.T) {
|
||||
mockedClient := get_mocked_client()
|
||||
|
||||
handler := createHandler(mockedClient, nil, Config{Base: "/", EnableActions: true, Authorization: Authorization{Provider: NONE}})
|
||||
req, err := http.NewRequest("POST", "/api/actions/restart/localhost/123", nil)
|
||||
req, err := http.NewRequest("POST", "/api/hosts/localhost/containers/123/actions/restart", nil)
|
||||
require.NoError(t, err, "Request should not return an error.")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rr, req)
|
||||
assert.Equal(t, rr.Code, 200)
|
||||
assert.Equal(t, 200, rr.Code)
|
||||
}
|
||||
|
||||
func Test_handler_containerActions_unknown_action(t *testing.T) {
|
||||
mockedClient := get_mocked_client()
|
||||
|
||||
handler := createHandler(mockedClient, nil, Config{Base: "/", EnableActions: true, Authorization: Authorization{Provider: NONE}})
|
||||
req, err := http.NewRequest("POST", "/api/actions/something-else/localhost/123", nil)
|
||||
req, err := http.NewRequest("POST", "/api/hosts/localhost/containers/123/actions/something-else", nil)
|
||||
require.NoError(t, err, "Request should not return an error.")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rr, req)
|
||||
assert.Equal(t, rr.Code, 500)
|
||||
assert.Equal(t, 500, rr.Code)
|
||||
}
|
||||
|
||||
func Test_handler_containerActions_unknown_container(t *testing.T) {
|
||||
mockedClient := get_mocked_client()
|
||||
|
||||
handler := createHandler(mockedClient, nil, Config{Base: "/", EnableActions: true, Authorization: Authorization{Provider: NONE}})
|
||||
req, err := http.NewRequest("POST", "/api/actions/start/localhost/456", nil)
|
||||
req, err := http.NewRequest("POST", "/api/hosts/localhost/containers/456/actions/start", nil)
|
||||
require.NoError(t, err, "Request should not return an error.")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rr, req)
|
||||
assert.Equal(t, rr.Code, 404)
|
||||
assert.Equal(t, 404, rr.Code)
|
||||
}
|
||||
|
||||
func Test_handler_containerActions_start(t *testing.T) {
|
||||
mockedClient := get_mocked_client()
|
||||
|
||||
handler := createHandler(mockedClient, nil, Config{Base: "/", EnableActions: true, Authorization: Authorization{Provider: NONE}})
|
||||
req, err := http.NewRequest("POST", "/api/actions/start/localhost/123", nil)
|
||||
req, err := http.NewRequest("POST", "/api/hosts/localhost/containers/123/actions/start", nil)
|
||||
require.NoError(t, err, "Request should not return an error.")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rr, req)
|
||||
assert.Equal(t, rr.Code, 200)
|
||||
assert.Equal(t, 200, rr.Code)
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ import (
|
||||
|
||||
func Test_handler_download_logs(t *testing.T) {
|
||||
id := "123456"
|
||||
req, err := http.NewRequest("GET", "/api/logs/download/localhost/"+id, nil)
|
||||
req, err := http.NewRequest("GET", "/api/hosts/localhost/containers/"+id+"/logs/download", nil)
|
||||
require.NoError(t, err, "NewRequest should not return an error.")
|
||||
|
||||
mockedClient := new(MockedClient)
|
||||
|
||||
@@ -20,7 +20,7 @@ import (
|
||||
|
||||
func Test_handler_streamLogs_happy(t *testing.T) {
|
||||
id := "123456"
|
||||
req, err := http.NewRequest("GET", "/api/logs/stream/localhost/"+id, nil)
|
||||
req, err := http.NewRequest("GET", "/api/hosts/localhost/containers/"+id+"/logs/stream", nil)
|
||||
q := req.URL.Query()
|
||||
q.Add("stdout", "true")
|
||||
q.Add("stderr", "true")
|
||||
@@ -44,7 +44,7 @@ func Test_handler_streamLogs_happy(t *testing.T) {
|
||||
|
||||
func Test_handler_streamLogs_happy_with_id(t *testing.T) {
|
||||
id := "123456"
|
||||
req, err := http.NewRequest("GET", "/api/logs/stream/localhost/"+id, nil)
|
||||
req, err := http.NewRequest("GET", "/api/hosts/localhost/containers/"+id+"/logs/stream", nil)
|
||||
q := req.URL.Query()
|
||||
q.Add("stdout", "true")
|
||||
q.Add("stderr", "true")
|
||||
@@ -68,7 +68,7 @@ func Test_handler_streamLogs_happy_with_id(t *testing.T) {
|
||||
|
||||
func Test_handler_streamLogs_happy_container_stopped(t *testing.T) {
|
||||
id := "123456"
|
||||
req, err := http.NewRequest("GET", "/api/logs/stream/localhost/"+id, nil)
|
||||
req, err := http.NewRequest("GET", "/api/hosts/localhost/containers/"+id+"/logs/stream", nil)
|
||||
q := req.URL.Query()
|
||||
q.Add("stdout", "true")
|
||||
q.Add("stderr", "true")
|
||||
@@ -89,7 +89,7 @@ func Test_handler_streamLogs_happy_container_stopped(t *testing.T) {
|
||||
|
||||
func Test_handler_streamLogs_error_finding_container(t *testing.T) {
|
||||
id := "123456"
|
||||
req, err := http.NewRequest("GET", "/api/logs/stream/localhost/"+id, nil)
|
||||
req, err := http.NewRequest("GET", "/api/hosts/localhost/containers/"+id+"/logs/stream", nil)
|
||||
q := req.URL.Query()
|
||||
q.Add("stdout", "true")
|
||||
q.Add("stderr", "true")
|
||||
@@ -109,7 +109,7 @@ func Test_handler_streamLogs_error_finding_container(t *testing.T) {
|
||||
|
||||
func Test_handler_streamLogs_error_reading(t *testing.T) {
|
||||
id := "123456"
|
||||
req, err := http.NewRequest("GET", "/api/logs/stream/localhost/"+id, nil)
|
||||
req, err := http.NewRequest("GET", "/api/hosts/localhost/containers/"+id+"/logs/stream", nil)
|
||||
q := req.URL.Query()
|
||||
q.Add("stdout", "true")
|
||||
q.Add("stderr", "true")
|
||||
@@ -130,7 +130,7 @@ func Test_handler_streamLogs_error_reading(t *testing.T) {
|
||||
|
||||
func Test_handler_streamLogs_error_std(t *testing.T) {
|
||||
id := "123456"
|
||||
req, err := http.NewRequest("GET", "/api/logs/stream/localhost/"+id, nil)
|
||||
req, err := http.NewRequest("GET", "/api/hosts/localhost/containers/"+id+"/logs/stream", nil)
|
||||
|
||||
require.NoError(t, err, "NewRequest should not return an error.")
|
||||
|
||||
@@ -143,10 +143,9 @@ func Test_handler_streamLogs_error_std(t *testing.T) {
|
||||
mockedClient.AssertExpectations(t)
|
||||
}
|
||||
|
||||
// for /api/logs
|
||||
func Test_handler_between_dates(t *testing.T) {
|
||||
id := "123456"
|
||||
req, err := http.NewRequest("GET", "/api/logs/localhost/"+id, nil)
|
||||
req, err := http.NewRequest("GET", "/api/hosts/localhost/containers/"+id+"/logs", nil)
|
||||
require.NoError(t, err, "NewRequest should not return an error.")
|
||||
|
||||
from, _ := time.Parse(time.RFC3339, "2018-01-01T00:00:00Z")
|
||||
|
||||
@@ -90,12 +90,12 @@ func createRouter(h *handler) *chi.Mux {
|
||||
if h.config.Authorization.Provider != NONE {
|
||||
r.Use(auth.RequireAuthentication)
|
||||
}
|
||||
r.Get("/api/logs/stream/{host}/{id}", h.streamLogs)
|
||||
r.Get("/api/logs/download/{host}/{id}", h.downloadLogs)
|
||||
r.Get("/api/logs/{host}/{id}", h.fetchLogsBetweenDates)
|
||||
r.Get("/api/hosts/{host}/containers/{id}/logs/stream", h.streamLogs)
|
||||
r.Get("/api/hosts/{host}/containers/{id}/logs/download", h.downloadLogs)
|
||||
r.Get("/api/hosts/{host}/containers/{id}/logs", h.fetchLogsBetweenDates)
|
||||
r.Get("/api/events/stream", h.streamEvents)
|
||||
if h.config.EnableActions {
|
||||
r.Post("/api/actions/{action}/{host}/{id}", h.containerActions)
|
||||
r.Post("/api/hosts/{host}/containers/{id}/actions/{action}", h.containerActions)
|
||||
}
|
||||
r.Get("/api/releases", h.releases)
|
||||
r.Get("/api/profile/avatar", h.avatar)
|
||||
|
||||
Reference in New Issue
Block a user