From 74292a473a5341e629decc44fce02bc0e3753d4a Mon Sep 17 00:00:00 2001 From: a-wing <1@233.email> Date: Thu, 28 Dec 2023 02:40:40 +0800 Subject: [PATCH] refactor(server): create room stream --- server/api/api.go | 2 +- server/api/v1/helper.go | 64 ++++++++++++++++++++++++++++++++++++ server/api/v1/room.go | 54 +++++------------------------- server/api/v1/stream.go | 25 ++++++++++++++ webapp/components/join.tsx | 4 +-- webapp/components/layout.tsx | 2 +- webapp/components/member.tsx | 2 +- webapp/lib/storage.ts | 6 ++-- 8 files changed, 105 insertions(+), 54 deletions(-) create mode 100644 server/api/v1/helper.go create mode 100644 server/api/v1/stream.go diff --git a/server/api/api.go b/server/api/api.go index 8fe0cfb..8a71eab 100644 --- a/server/api/api.go +++ b/server/api/api.go @@ -38,7 +38,7 @@ func NewApi(rdb *redis.Client, live777Url string, live777Token string) http.Hand r.Post("/room/", handle.CreateRoom) r.Get("/room/{roomId}", handle.ShowRoom) //r.Patch("/room/{roomId}", handle.UpdateRoom) - //r.Post("/room/{roomId}/stream", handle.CreateRoomStream) + r.Post("/room/{roomId}/stream", handle.CreateRoomStream) //r.Patch("/room/{roomId}/stream/{streamId}", handle.UpdateRoomStream) //r.Post("/room/{roomId}/message", handle.CreateMessage) diff --git a/server/api/v1/helper.go b/server/api/v1/helper.go new file mode 100644 index 0000000..b073d21 --- /dev/null +++ b/server/api/v1/helper.go @@ -0,0 +1,64 @@ +package v1 + +import ( + "context" + "net/http" + + "woom/server/helper" + "woom/server/model" + + "github.com/go-chi/chi/v5" + "github.com/gofrs/uuid/v5" +) + +func (h *Handler) helperCreateStreamId() (string, error) { + id, err := uuid.NewV4() + return id.String(), err +} + +func (h *Handler) helperCreateRoomStream(r *http.Request, roomId, streamId string) (*model.Stream, error) { + stream := &model.Stream{ + // TODO: + Name: "", + // TODO: + Token: "", + Audio: false, + Video: false, + Screen: false, + } + + gobStream, err := helper.GobEncode(stream) + if err != nil { + return stream, err + } + + if err := h.rdb.HSet(context.TODO(), roomId, streamId, gobStream).Err(); err != nil { + return stream, err + } + return stream, err +} + +func (h *Handler) helperShowRoom(r *http.Request) (*model.Room, error) { + roomId := chi.URLParam(r, "roomId") + room := model.Room{ + RoomId: roomId, + Streams: map[string]model.Stream{}, + } + result, err := h.rdb.HGetAll(context.TODO(), roomId).Result() + if err != nil { + return &room, err + } + + for k, v := range result { + if k == model.AdminUniqueKey { + admin := model.RoomAdmin{} + helper.GobDecode(&admin, []byte(v)) + room.RoomAdmin = admin + } else { + stream := model.Stream{} + helper.GobDecode(&stream, []byte(v)) + room.Streams[k] = stream + } + } + return &room, err +} diff --git a/server/api/v1/room.go b/server/api/v1/room.go index dfeae38..d793b53 100644 --- a/server/api/v1/room.go +++ b/server/api/v1/room.go @@ -7,17 +7,14 @@ import ( "woom/server/helper" "woom/server/model" - "github.com/go-chi/chi/v5" "github.com/go-chi/render" - "github.com/gofrs/uuid/v5" ) const idLength = 9 func (h *Handler) CreateRoom(w http.ResponseWriter, r *http.Request) { roomId := helper.AddSplitSymbol(helper.GenNumberSecret(idLength)) - id, err := uuid.NewV4() - streamId := id.String() + streamId, err := h.helperCreateStreamId() if err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte(err.Error())) @@ -29,42 +26,24 @@ func (h *Handler) CreateRoom(w http.ResponseWriter, r *http.Request) { Presenter: "", Locked: false, } - - stream := model.Stream{ - // TODO: - Name: "", - // TODO: - Token: "", - Audio: false, - Video: false, - Screen: false, - } - gobAdmin, err := helper.GobEncode(&admin) if err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte(err.Error())) return } - - gobStream, err := helper.GobEncode(&stream) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(err.Error())) - return - } - if err := h.rdb.HSet(context.TODO(), roomId, model.AdminUniqueKey, gobAdmin).Err(); err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte(err.Error())) return } - if err := h.rdb.HSet(context.TODO(), roomId, streamId, gobStream).Err(); err != nil { - w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(err.Error())) - return - } + //if _, err := h.helperCreateRoomStream(r, roomId, streamId); err != nil { + // w.WriteHeader(http.StatusInternalServerError) + // w.Write([]byte(err.Error())) + // return + //} + room := model.Room{ RoomId: roomId, RoomAdmin: admin, @@ -74,28 +53,11 @@ func (h *Handler) CreateRoom(w http.ResponseWriter, r *http.Request) { } func (h *Handler) ShowRoom(w http.ResponseWriter, r *http.Request) { - roomId := chi.URLParam(r, "roomId") - result, err := h.rdb.HGetAll(context.TODO(), roomId).Result() + room, err := h.helperShowRoom(r) if err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte(err.Error())) return } - - room := model.Room{ - RoomId: roomId, - Streams: map[string]model.Stream{}, - } - for k, v := range result { - if k == model.AdminUniqueKey { - admin := model.RoomAdmin{} - helper.GobDecode(&admin, []byte(v)) - room.RoomAdmin = admin - } else { - stream := model.Stream{} - helper.GobDecode(&stream, []byte(v)) - room.Streams[k] = stream - } - } render.JSON(w, r, room) } diff --git a/server/api/v1/stream.go b/server/api/v1/stream.go new file mode 100644 index 0000000..ae20bdb --- /dev/null +++ b/server/api/v1/stream.go @@ -0,0 +1,25 @@ +package v1 + +import ( + "net/http" + + "github.com/go-chi/render" +) + +func (h *Handler) CreateRoomStream(w http.ResponseWriter, r *http.Request) { + room, err := h.helperShowRoom(r) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + + streamId, err := h.helperCreateStreamId() + if _, err := h.helperCreateRoomStream(r, room.RoomId, streamId); err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + room.StreamId = streamId + render.JSON(w, r, room) +} diff --git a/webapp/components/join.tsx b/webapp/components/join.tsx index ff0b412..1f36e85 100644 --- a/webapp/components/join.tsx +++ b/webapp/components/join.tsx @@ -52,8 +52,8 @@ export default function Join() { setTmpId(delSplitSymbol(e.target.value))} + value={tmpId} + onChange={e => setTmpId(e.target.value)} maxLength={11} /> diff --git a/webapp/components/layout.tsx b/webapp/components/layout.tsx index 3a4632d..eb8af6a 100644 --- a/webapp/components/layout.tsx +++ b/webapp/components/layout.tsx @@ -30,7 +30,7 @@ export default function Layout(props: { meetingId: string }) { const refresh = async () => { let res = await fetch(location.origin + `/room/${props.meetingId}`) - const data = await res.json() + const data = (await res.json()).streams const r = Object.keys(data) .filter(i => i !== localStreamId) .filter(i => !!i) diff --git a/webapp/components/member.tsx b/webapp/components/member.tsx index 829d314..f234b52 100644 --- a/webapp/components/member.tsx +++ b/webapp/components/member.tsx @@ -16,7 +16,7 @@ export default function Member() { const refresh = async () => { let res = await fetch(location.origin + `/room/${meetingId}`) - setStream(Object.keys(await res.json()).filter(i => i !== localStreamId)) + setStream(Object.keys((await res.json()).streams).filter(i => i !== localStreamId)) } useEffect(() => { diff --git a/webapp/lib/storage.ts b/webapp/lib/storage.ts index 3cee3c2..6289678 100644 --- a/webapp/lib/storage.ts +++ b/webapp/lib/storage.ts @@ -14,10 +14,10 @@ function setStreamId(id: string) { } async function serverGetStreamId(meetingId: string): Promise { - let res = await fetch(`/room/${meetingId}/stream`, { + let res = await (await fetch(`/room/${meetingId}/stream`, { method: "POST" - }) - return res.text() + })).json() + return res.streamId } async function asyncGetStreamId(): Promise {