mirror of
https://github.com/m1k1o/neko.git
synced 2025-05-17 02:57:04 +02:00
move server to server directory.
This commit is contained in:
parent
da45f62ca8
commit
cfb423b13d
211 changed files with 18 additions and 10 deletions
80
server/internal/api/sessions/controller.go
Normal file
80
server/internal/api/sessions/controller.go
Normal file
|
@ -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)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue