Skip to content

Commit

Permalink
add context to rooms.
Browse files Browse the repository at this point in the history
  • Loading branch information
m1k1o committed Oct 4, 2023
1 parent 177483c commit c165c67
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 77 deletions.
35 changes: 18 additions & 17 deletions internal/api/rooms.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"context"
"encoding/json"
"errors"
"io"
Expand All @@ -26,7 +27,7 @@ func (manager *ApiManagerCtx) roomsList(w http.ResponseWriter, r *http.Request)
labelsMap[key] = value[0]
}

response, err := manager.rooms.List(labelsMap)
response, err := manager.rooms.List(r.Context(), labelsMap)
if err != nil {
http.Error(w, err.Error(), 500)
return
Expand All @@ -50,20 +51,20 @@ func (manager *ApiManagerCtx) roomCreate(w http.ResponseWriter, r *http.Request)
return
}

ID, err := manager.rooms.Create(request)
ID, err := manager.rooms.Create(r.Context(), request)
if err != nil {
manager.logger.Error().Err(err).Msg("create: failed to create room")
http.Error(w, err.Error(), 500)
return
}

if err := manager.rooms.Start(ID); err != nil {
if err := manager.rooms.Start(r.Context(), ID); err != nil {
manager.logger.Error().Err(err).Msg("create: failed to start room")
http.Error(w, err.Error(), 500)
return
}

response, err := manager.rooms.GetEntry(ID)
response, err := manager.rooms.GetEntry(r.Context(), ID)
if err != nil {
manager.logger.Error().Err(err).Msg("create: failed to get room entry")
http.Error(w, err.Error(), 500)
Expand All @@ -77,7 +78,7 @@ func (manager *ApiManagerCtx) roomCreate(w http.ResponseWriter, r *http.Request)
func (manager *ApiManagerCtx) roomRecreate(w http.ResponseWriter, r *http.Request) {
roomId := chi.URLParam(r, "roomId")

entry, err := manager.rooms.GetEntry(roomId)
entry, err := manager.rooms.GetEntry(r.Context(), roomId)
if err != nil {
if errors.Is(err, types.ErrRoomNotFound) {
http.Error(w, err.Error(), 404)
Expand All @@ -88,7 +89,7 @@ func (manager *ApiManagerCtx) roomRecreate(w http.ResponseWriter, r *http.Reques
return
}

settings, err := manager.rooms.GetSettings(roomId)
settings, err := manager.rooms.GetSettings(r.Context(), roomId)
if err != nil {
manager.logger.Error().Err(err).Msg("recreate: failed to get room settings")
http.Error(w, err.Error(), 500)
Expand All @@ -101,28 +102,28 @@ func (manager *ApiManagerCtx) roomRecreate(w http.ResponseWriter, r *http.Reques
return
}

if err := manager.rooms.Remove(roomId); err != nil {
if err := manager.rooms.Remove(r.Context(), roomId); err != nil {
manager.logger.Error().Err(err).Msg("recreate: failed to remove room")
http.Error(w, err.Error(), 500)
return
}

ID, err := manager.rooms.Create(*settings)
ID, err := manager.rooms.Create(r.Context(), *settings)
if err != nil {
manager.logger.Error().Err(err).Msg("recreate: failed to create room")
http.Error(w, err.Error(), 500)
return
}

if entry.Running {
if err := manager.rooms.Start(ID); err != nil {
if err := manager.rooms.Start(r.Context(), ID); err != nil {
manager.logger.Error().Err(err).Msg("recreate: failed to start room")
http.Error(w, err.Error(), 500)
return
}
}

response, err := manager.rooms.GetEntry(ID)
response, err := manager.rooms.GetEntry(r.Context(), ID)
if err != nil {
manager.logger.Error().Err(err).Msg("recreate: failed to get room entry")
http.Error(w, err.Error(), 500)
Expand All @@ -136,7 +137,7 @@ func (manager *ApiManagerCtx) roomRecreate(w http.ResponseWriter, r *http.Reques
func (manager *ApiManagerCtx) roomGetEntry(w http.ResponseWriter, r *http.Request) {
roomId := chi.URLParam(r, "roomId")

response, err := manager.rooms.GetEntry(roomId)
response, err := manager.rooms.GetEntry(r.Context(), roomId)
if err != nil {
if errors.Is(err, types.ErrRoomNotFound) {
http.Error(w, err.Error(), 404)
Expand All @@ -154,7 +155,7 @@ func (manager *ApiManagerCtx) roomGetEntryByName(w http.ResponseWriter, r *http.
// roomId is actually room name here
roomName := chi.URLParam(r, "roomId")

response, err := manager.rooms.GetEntryByName(roomName)
response, err := manager.rooms.GetEntryByName(r.Context(), roomName)
if err != nil {
if errors.Is(err, types.ErrRoomNotFound) {
http.Error(w, err.Error(), 404)
Expand All @@ -171,7 +172,7 @@ func (manager *ApiManagerCtx) roomGetEntryByName(w http.ResponseWriter, r *http.
func (manager *ApiManagerCtx) roomGetSettings(w http.ResponseWriter, r *http.Request) {
roomId := chi.URLParam(r, "roomId")

response, err := manager.rooms.GetSettings(roomId)
response, err := manager.rooms.GetSettings(r.Context(), roomId)
if err != nil {
if errors.Is(err, types.ErrRoomNotFound) {
http.Error(w, err.Error(), 404)
Expand All @@ -188,7 +189,7 @@ func (manager *ApiManagerCtx) roomGetSettings(w http.ResponseWriter, r *http.Req
func (manager *ApiManagerCtx) roomGetStats(w http.ResponseWriter, r *http.Request) {
roomId := chi.URLParam(r, "roomId")

response, err := manager.rooms.GetStats(roomId)
response, err := manager.rooms.GetStats(r.Context(), roomId)
if err != nil {
if errors.Is(err, types.ErrRoomNotFound) {
http.Error(w, err.Error(), 404)
Expand All @@ -202,11 +203,11 @@ func (manager *ApiManagerCtx) roomGetStats(w http.ResponseWriter, r *http.Reques
json.NewEncoder(w).Encode(response)
}

func (manager *ApiManagerCtx) roomGenericAction(action func(id string) error) func(http.ResponseWriter, *http.Request) {
func (manager *ApiManagerCtx) roomGenericAction(action func(ctx context.Context, id string) error) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
roomId := chi.URLParam(r, "roomId")

err := action(roomId)
err := action(r.Context(), roomId)
if err != nil {
if errors.Is(err, types.ErrRoomNotFound) {
http.Error(w, err.Error(), 404)
Expand All @@ -221,7 +222,7 @@ func (manager *ApiManagerCtx) roomGenericAction(action func(id string) error) fu
}

func (manager *ApiManagerCtx) dockerCompose(w http.ResponseWriter, r *http.Request) {
response, err := manager.rooms.ExportAsDockerCompose()
response, err := manager.rooms.ExportAsDockerCompose(r.Context())
if err != nil {
http.Error(w, err.Error(), 500)
return
Expand Down
26 changes: 13 additions & 13 deletions internal/room/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (manager *RoomManagerCtx) containerToEntry(container dockerTypes.Container)
return entry, nil
}

func (manager *RoomManagerCtx) listContainers(labels map[string]string) ([]dockerTypes.Container, error) {
func (manager *RoomManagerCtx) listContainers(ctx context.Context, labels map[string]string) ([]dockerTypes.Container, error) {
args := filters.NewArgs(
filters.Arg("label", fmt.Sprintf("m1k1o.neko_rooms.instance=%s", manager.config.InstanceName)),
)
Expand All @@ -48,16 +48,16 @@ func (manager *RoomManagerCtx) listContainers(labels map[string]string) ([]docke
args.Add("label", fmt.Sprintf("m1k1o.neko_rooms.x-%s=%s", key, val))
}

return manager.client.ContainerList(context.Background(), dockerTypes.ContainerListOptions{
return manager.client.ContainerList(ctx, dockerTypes.ContainerListOptions{
All: true,
Filters: args,
})
}

func (manager *RoomManagerCtx) containerFilter(args filters.Args) (*dockerTypes.Container, error) {
func (manager *RoomManagerCtx) containerFilter(ctx context.Context, args filters.Args) (*dockerTypes.Container, error) {
args.Add("label", fmt.Sprintf("m1k1o.neko_rooms.instance=%s", manager.config.InstanceName))

containers, err := manager.client.ContainerList(context.Background(), dockerTypes.ContainerListOptions{
containers, err := manager.client.ContainerList(ctx, dockerTypes.ContainerListOptions{
All: true,
Filters: args,
})
Expand All @@ -74,20 +74,20 @@ func (manager *RoomManagerCtx) containerFilter(args filters.Args) (*dockerTypes.
return &container, nil
}

func (manager *RoomManagerCtx) containerById(id string) (*dockerTypes.Container, error) {
return manager.containerFilter(filters.NewArgs(
func (manager *RoomManagerCtx) containerById(ctx context.Context, id string) (*dockerTypes.Container, error) {
return manager.containerFilter(ctx, filters.NewArgs(
filters.Arg("id", id),
))
}

func (manager *RoomManagerCtx) containerByName(name string) (*dockerTypes.Container, error) {
return manager.containerFilter(filters.NewArgs(
func (manager *RoomManagerCtx) containerByName(ctx context.Context, name string) (*dockerTypes.Container, error) {
return manager.containerFilter(ctx, filters.NewArgs(
filters.Arg("name", manager.config.InstanceName+"-"+name),
))
}

func (manager *RoomManagerCtx) inspectContainer(id string) (*dockerTypes.ContainerJSON, error) {
container, err := manager.client.ContainerInspect(context.Background(), id)
func (manager *RoomManagerCtx) inspectContainer(ctx context.Context, id string) (*dockerTypes.ContainerJSON, error) {
container, err := manager.client.ContainerInspect(ctx, id)
if err != nil {
if dockerClient.IsErrNotFound(err) {
return nil, types.ErrRoomNotFound
Expand All @@ -103,8 +103,8 @@ func (manager *RoomManagerCtx) inspectContainer(id string) (*dockerTypes.Contain
return &container, nil
}

func (manager *RoomManagerCtx) containerExec(id string, cmd []string) (string, error) {
exec, err := manager.client.ContainerExecCreate(context.Background(), id, dockerTypes.ExecConfig{
func (manager *RoomManagerCtx) containerExec(ctx context.Context, id string, cmd []string) (string, error) {
exec, err := manager.client.ContainerExecCreate(ctx, id, dockerTypes.ExecConfig{
AttachStderr: true,
AttachStdin: true,
AttachStdout: true,
Expand All @@ -119,7 +119,7 @@ func (manager *RoomManagerCtx) containerExec(id string, cmd []string) (string, e
return "", err
}

conn, err := manager.client.ContainerExecAttach(context.Background(), exec.ID, dockerTypes.ExecStartCheck{
conn, err := manager.client.ContainerExecAttach(ctx, exec.ID, dockerTypes.ExecStartCheck{
Detach: false,
Tty: true,
})
Expand Down
Loading

0 comments on commit c165c67

Please sign in to comment.