Skip to content
This repository has been archived by the owner on Jul 18, 2024. It is now read-only.

Commit

Permalink
add session delete and disconnect to API.
Browse files Browse the repository at this point in the history
  • Loading branch information
m1k1o committed May 9, 2024
1 parent 416faa3 commit 0e8108e
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 25 deletions.
5 changes: 4 additions & 1 deletion internal/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/demodesk/neko/internal/api/members"
"github.com/demodesk/neko/internal/api/room"
"github.com/demodesk/neko/internal/api/sessions"
"github.com/demodesk/neko/pkg/auth"
"github.com/demodesk/neko/pkg/types"
"github.com/demodesk/neko/pkg/utils"
Expand Down Expand Up @@ -45,7 +46,9 @@ func (api *ApiManagerCtx) Route(r types.Router) {

r.Post("/logout", api.Logout)
r.Get("/whoami", api.Whoami)
r.Get("/sessions", api.Sessions)

sessionsHandler := sessions.New(api.sessions)
r.Route("/sessions", sessionsHandler.Route)

membersHandler := members.New(api.members)
r.Route("/members", membersHandler.Route)
Expand Down
13 changes: 0 additions & 13 deletions internal/api/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,3 @@ func (api *ApiManagerCtx) Whoami(w http.ResponseWriter, r *http.Request) error {
State: session.State(),
})
}

func (api *ApiManagerCtx) Sessions(w http.ResponseWriter, r *http.Request) error {
sessions := []SessionDataPayload{}
for _, session := range api.sessions.List() {
sessions = append(sessions, SessionDataPayload{
ID: session.ID(),
Profile: session.Profile(),
State: session.State(),
})
}

return utils.HttpSuccess(w, sessions)
}
80 changes: 80 additions & 0 deletions internal/api/sessions/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package sessions

import (
"errors"
"net/http"

"github.com/demodesk/neko/pkg/auth"
"github.com/demodesk/neko/pkg/types"
"github.com/demodesk/neko/pkg/utils"
"github.com/go-chi/chi"
)

type SessionDataPayload struct {
ID string `json:"id"`
Profile types.MemberProfile `json:"profile"`
State types.SessionState `json:"state"`
}

func (h *SessionsHandler) sessionsList(w http.ResponseWriter, r *http.Request) error {
sessions := []SessionDataPayload{}
for _, session := range h.sessions.List() {
sessions = append(sessions, SessionDataPayload{
ID: session.ID(),
Profile: session.Profile(),
State: session.State(),
})
}

return utils.HttpSuccess(w, sessions)
}

func (h *SessionsHandler) sessionsRead(w http.ResponseWriter, r *http.Request) error {
sessionId := chi.URLParam(r, "sessionId")

session, ok := h.sessions.Get(sessionId)
if !ok {
return utils.HttpNotFound("session not found")
}

return utils.HttpSuccess(w, SessionDataPayload{
ID: session.ID(),
Profile: session.Profile(),
State: session.State(),
})
}

func (h *SessionsHandler) sessionsDelete(w http.ResponseWriter, r *http.Request) error {
session, _ := auth.GetSession(r)

sessionId := chi.URLParam(r, "sessionId")
if sessionId == session.ID() {
return utils.HttpBadRequest("cannot delete own session")
}

err := h.sessions.Delete(sessionId)
if err != nil {
if errors.Is(err, types.ErrSessionNotFound) {
return utils.HttpBadRequest("session not found")
} else {
return utils.HttpInternalServerError().WithInternalErr(err)
}
}

return utils.HttpSuccess(w)
}

func (h *SessionsHandler) sessionsDisconnect(w http.ResponseWriter, r *http.Request) error {
sessionId := chi.URLParam(r, "sessionId")

err := h.sessions.Disconnect(sessionId)
if err != nil {
if errors.Is(err, types.ErrSessionNotFound) {
return utils.HttpBadRequest("session not found")
} else {
return utils.HttpInternalServerError().WithInternalErr(err)
}
}

return utils.HttpSuccess(w)
}
30 changes: 30 additions & 0 deletions internal/api/sessions/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package sessions

import (
"github.com/demodesk/neko/pkg/auth"
"github.com/demodesk/neko/pkg/types"
)

type SessionsHandler struct {
sessions types.SessionManager
}

func New(
sessions types.SessionManager,
) *SessionsHandler {
// Init

return &SessionsHandler{
sessions: sessions,
}
}

func (h *SessionsHandler) Route(r types.Router) {
r.Get("/", h.sessionsList)

r.With(auth.AdminsOnly).Route("/{sessionId}", func(r types.Router) {
r.Get("/", h.sessionsRead)
r.Delete("/", h.sessionsDelete)
r.Post("/disconnect", h.sessionsDisconnect)
})
}
20 changes: 20 additions & 0 deletions internal/session/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,26 @@ func (manager *SessionManagerCtx) Delete(id string) error {
return nil
}

func (manager *SessionManagerCtx) Disconnect(id string) error {
manager.sessionsMu.Lock()
session, ok := manager.sessions[id]
if !ok {
manager.sessionsMu.Unlock()
return types.ErrSessionNotFound
}
manager.sessionsMu.Unlock()

if session.State().IsConnected {
session.DestroyWebSocketPeer("session disconnected")
}

if session.State().IsWatching {
session.GetWebRTCPeer().Destroy()
}

return nil
}

func (manager *SessionManagerCtx) Get(id string) (types.Session, bool) {
manager.sessionsMu.Lock()
defer manager.sessionsMu.Unlock()
Expand Down
90 changes: 79 additions & 11 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ servers:
url: http://localhost:3000

tags:
- name: session
description: Session management.
- name: sessions
description: Sessions management.
- name: room
description: Room releated operations.
- name: members
Expand Down Expand Up @@ -61,13 +61,11 @@ paths:
required: true

#
# session
# current session
#

/api/login:
post:
tags:
- session
summary: login
operationId: login
security: []
Expand All @@ -90,8 +88,6 @@ paths:
required: true
/api/logout:
post:
tags:
- session
summary: logout
operationId: logout
responses:
Expand All @@ -101,8 +97,6 @@ paths:
$ref: '#/components/responses/Unauthorized'
/api/whoami:
get:
tags:
- session
summary: whoami
operationId: whoami
responses:
Expand All @@ -116,10 +110,15 @@ paths:
$ref: '#/components/responses/Unauthorized'
'403':
$ref: '#/components/responses/Forbidden'

#
# sessions
#

/api/sessions:
get:
tags:
- session
- sessions
summary: get sessions
operationId: sessionsGet
responses:
Expand All @@ -135,6 +134,75 @@ paths:
$ref: '#/components/responses/Unauthorized'
'403':
$ref: '#/components/responses/Forbidden'
/api/sessions/{sessionId}:
get:
tags:
- sessions
summary: get session
operationId: sessionGet
parameters:
- in: path
name: sessionId
description: session identifier
required: true
schema:
type: string
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/SessionData'
'401':
$ref: '#/components/responses/Unauthorized'
'403':
$ref: '#/components/responses/Forbidden'
'404':
$ref: '#/components/responses/NotFound'
delete:
tags:
- sessions
summary: remove session
operationId: sessionRemove
parameters:
- in: path
name: sessionId
description: session identifier
required: true
schema:
type: string
responses:
'204':
description: OK
'401':
$ref: '#/components/responses/Unauthorized'
'403':
$ref: '#/components/responses/Forbidden'
'404':
$ref: '#/components/responses/NotFound'
/api/sessions/{sessionId}/disconnect:
post:
tags:
- sessions
summary: disconnect session
operationId: sessionDisconnect
parameters:
- in: path
name: sessionId
description: session identifier
required: true
schema:
type: string
responses:
'204':
description: OK
'401':
$ref: '#/components/responses/Unauthorized'
'403':
$ref: '#/components/responses/Forbidden'
'404':
$ref: '#/components/responses/NotFound'

#
# room
Expand Down Expand Up @@ -1023,7 +1091,7 @@ components:
type: integer

#
# session
# sessions
#

SessionLogin:
Expand Down
1 change: 1 addition & 0 deletions pkg/types/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type SessionManager interface {
Create(id string, profile MemberProfile) (Session, string, error)
Update(id string, profile MemberProfile) error
Delete(id string) error
Disconnect(id string) error
Get(id string) (Session, bool)
GetByToken(token string) (Session, bool)
List() []Session
Expand Down

0 comments on commit 0e8108e

Please sign in to comment.