This repository has been archived by the owner on Jul 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add session delete and disconnect to API.
- Loading branch information
Showing
7 changed files
with
214 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters