mirror of
https://github.com/amir20/dozzle.git
synced 2025-12-31 10:07:22 +01:00
* chore(deps): remove gorilla mux in favor of using go-chi * fixes broken redirect and tests * chore: clean up * reverts changes
82 lines
2.0 KiB
Go
82 lines
2.0 KiB
Go
package web
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"io"
|
|
"io/fs"
|
|
|
|
"github.com/amir20/dozzle/docker"
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
"github.com/spf13/afero"
|
|
)
|
|
|
|
type MockedClient struct {
|
|
mock.Mock
|
|
docker.Client
|
|
}
|
|
|
|
func (m *MockedClient) FindContainer(id string) (docker.Container, error) {
|
|
args := m.Called(id)
|
|
return args.Get(0).(docker.Container), args.Error(1)
|
|
}
|
|
|
|
func (m *MockedClient) ListContainers() ([]docker.Container, error) {
|
|
args := m.Called()
|
|
return args.Get(0).([]docker.Container), args.Error(1)
|
|
}
|
|
|
|
func (m *MockedClient) ContainerLogs(ctx context.Context, id string, since string, stdType docker.StdType) (io.ReadCloser, error) {
|
|
args := m.Called(ctx, id, since, stdType)
|
|
return args.Get(0).(io.ReadCloser), args.Error(1)
|
|
}
|
|
|
|
func (m *MockedClient) Events(ctx context.Context) (<-chan docker.ContainerEvent, <-chan error) {
|
|
args := m.Called(ctx)
|
|
channel, ok := args.Get(0).(chan docker.ContainerEvent)
|
|
if !ok {
|
|
panic("channel is not of type chan events.Message")
|
|
}
|
|
|
|
err, ok := args.Get(1).(chan error)
|
|
if !ok {
|
|
panic("error is not of type chan error")
|
|
}
|
|
return channel, err
|
|
}
|
|
|
|
func (m *MockedClient) ContainerStats(context.Context, string, chan<- docker.ContainerStat) error {
|
|
return nil
|
|
}
|
|
|
|
func (m *MockedClient) ContainerLogsBetweenDates(ctx context.Context, id string, from time.Time, to time.Time, stdType docker.StdType) (io.ReadCloser, error) {
|
|
args := m.Called(ctx, id, from, to, stdType)
|
|
return args.Get(0).(io.ReadCloser), args.Error(1)
|
|
}
|
|
|
|
func createHandler(client docker.Client, content fs.FS, config Config) *chi.Mux {
|
|
if client == nil {
|
|
client = new(MockedClient)
|
|
client.(*MockedClient).On("ListContainers").Return([]docker.Container{}, nil)
|
|
}
|
|
|
|
if content == nil {
|
|
fs := afero.NewMemMapFs()
|
|
afero.WriteFile(fs, "index.html", []byte("index page"), 0644)
|
|
content = afero.NewIOFS(fs)
|
|
}
|
|
|
|
clients := map[string]docker.Client{
|
|
"localhost": client,
|
|
}
|
|
return createRouter(&handler{
|
|
clients: clients,
|
|
content: content,
|
|
config: &config,
|
|
})
|
|
}
|