mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
API implement members.
This commit is contained in:
parent
4898cecd56
commit
4d2fb157f9
@ -1,6 +1,7 @@
|
|||||||
package members
|
package members
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strconv"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"demodesk/neko/internal/types"
|
"demodesk/neko/internal/types"
|
||||||
@ -12,12 +13,40 @@ type MemberDataPayload struct {
|
|||||||
*types.MemberProfile
|
*types.MemberProfile
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MemberCreatePayload struct {
|
||||||
|
Username string `json:"username"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
Profile types.MemberProfile `json:"profile"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type MemberPasswordPayload struct {
|
||||||
|
Password string `json:"password"`
|
||||||
|
}
|
||||||
|
|
||||||
func (h *MembersHandler) membersList(w http.ResponseWriter, r *http.Request) {
|
func (h *MembersHandler) membersList(w http.ResponseWriter, r *http.Request) {
|
||||||
|
limit, err := strconv.Atoi(r.URL.Query().Get("limit"))
|
||||||
|
if err != nil {
|
||||||
|
// TODO: Default zero.
|
||||||
|
limit = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
offset, err := strconv.Atoi(r.URL.Query().Get("offset"))
|
||||||
|
if err != nil {
|
||||||
|
// TODO: Default zero.
|
||||||
|
offset = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
entries, err := h.members.SelectAll(limit, offset)
|
||||||
|
if err != nil {
|
||||||
|
utils.HttpInternalServerError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
members := []MemberDataPayload{}
|
members := []MemberDataPayload{}
|
||||||
for _, session := range h.sessions.List() {
|
for id, profile := range entries {
|
||||||
profile := session.Profile()
|
profile := profile
|
||||||
members = append(members, MemberDataPayload{
|
members = append(members, MemberDataPayload{
|
||||||
ID: session.ID(),
|
ID: id,
|
||||||
MemberProfile: &profile,
|
MemberProfile: &profile,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -26,9 +55,9 @@ func (h *MembersHandler) membersList(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *MembersHandler) membersCreate(w http.ResponseWriter, r *http.Request) {
|
func (h *MembersHandler) membersCreate(w http.ResponseWriter, r *http.Request) {
|
||||||
data := &MemberDataPayload{
|
data := &MemberCreatePayload{
|
||||||
// default values
|
// default values
|
||||||
MemberProfile: &types.MemberProfile{
|
Profile: types.MemberProfile{
|
||||||
IsAdmin: false,
|
IsAdmin: false,
|
||||||
CanLogin: true,
|
CanLogin: true,
|
||||||
CanConnect: true,
|
CanConnect: true,
|
||||||
@ -42,51 +71,59 @@ func (h *MembersHandler) membersCreate(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if data.Name == "" {
|
if data.Username == "" {
|
||||||
utils.HttpBadRequest(w, "Name cannot be empty.")
|
utils.HttpBadRequest(w, "Username cannot be empty.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if data.ID == "" {
|
if data.Password == "" {
|
||||||
var err error
|
utils.HttpBadRequest(w, "Password cannot be empty.")
|
||||||
if data.ID, err = utils.NewUID(32); err != nil {
|
|
||||||
utils.HttpInternalServerError(w, err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
if _, ok := h.sessions.Get(data.ID); ok {
|
|
||||||
utils.HttpBadRequest(w, "Member ID already exists.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
session, _, err := h.sessions.Create(data.ID, *data.MemberProfile)
|
id, err := h.members.Insert(data.Username, data.Password, data.Profile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpInternalServerError(w, err)
|
utils.HttpInternalServerError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.HttpSuccess(w, MemberDataPayload{
|
utils.HttpSuccess(w, MemberDataPayload{
|
||||||
ID: session.ID(),
|
ID: id,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *MembersHandler) membersRead(w http.ResponseWriter, r *http.Request) {
|
func (h *MembersHandler) membersRead(w http.ResponseWriter, r *http.Request) {
|
||||||
member := GetMember(r)
|
member := GetMember(r)
|
||||||
profile := member.Profile()
|
profile := member.Profile
|
||||||
|
|
||||||
utils.HttpSuccess(w, profile)
|
utils.HttpSuccess(w, profile)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *MembersHandler) membersUpdate(w http.ResponseWriter, r *http.Request) {
|
func (h *MembersHandler) membersUpdateProfile(w http.ResponseWriter, r *http.Request) {
|
||||||
member := GetMember(r)
|
member := GetMember(r)
|
||||||
profile := member.Profile()
|
profile := member.Profile
|
||||||
|
|
||||||
if !utils.HttpJsonRequest(w, r, &profile) {
|
if !utils.HttpJsonRequest(w, r, &profile) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := h.sessions.Update(member.ID(), profile); err != nil {
|
if err := h.members.UpdateProfile(member.ID, profile); err != nil {
|
||||||
|
utils.HttpInternalServerError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
utils.HttpSuccess(w)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *MembersHandler) membersUpdatePassword(w http.ResponseWriter, r *http.Request) {
|
||||||
|
member := GetMember(r)
|
||||||
|
data := MemberPasswordPayload{}
|
||||||
|
|
||||||
|
if !utils.HttpJsonRequest(w, r, &data) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := h.members.UpdatePassword(member.ID, data.Password); err != nil {
|
||||||
utils.HttpInternalServerError(w, err)
|
utils.HttpInternalServerError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -97,7 +134,7 @@ func (h *MembersHandler) membersUpdate(w http.ResponseWriter, r *http.Request) {
|
|||||||
func (h *MembersHandler) membersDelete(w http.ResponseWriter, r *http.Request) {
|
func (h *MembersHandler) membersDelete(w http.ResponseWriter, r *http.Request) {
|
||||||
member := GetMember(r)
|
member := GetMember(r)
|
||||||
|
|
||||||
if err := h.sessions.Delete(member.ID()); err != nil {
|
if err := h.members.Delete(member.ID); err != nil {
|
||||||
utils.HttpInternalServerError(w, err)
|
utils.HttpInternalServerError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -18,16 +18,16 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type MembersHandler struct {
|
type MembersHandler struct {
|
||||||
sessions types.SessionManager
|
members types.MemberManager
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(
|
func New(
|
||||||
sessions types.SessionManager,
|
members types.MemberManager,
|
||||||
) *MembersHandler {
|
) *MembersHandler {
|
||||||
// Init
|
// Init
|
||||||
|
|
||||||
return &MembersHandler{
|
return &MembersHandler{
|
||||||
sessions: sessions,
|
members: members,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,29 +38,38 @@ func (h *MembersHandler) Route(r chi.Router) {
|
|||||||
r.Post("/", h.membersCreate)
|
r.Post("/", h.membersCreate)
|
||||||
r.With(h.ExtractMember).Route("/{memberId}", func(r chi.Router) {
|
r.With(h.ExtractMember).Route("/{memberId}", func(r chi.Router) {
|
||||||
r.Get("/", h.membersRead)
|
r.Get("/", h.membersRead)
|
||||||
r.Post("/", h.membersUpdate)
|
r.Post("/", h.membersUpdateProfile)
|
||||||
|
r.Post("/password", h.membersUpdatePassword)
|
||||||
r.Delete("/", h.membersDelete)
|
r.Delete("/", h.membersDelete)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetMember(r *http.Request, session types.Session) *http.Request {
|
type MemberData struct {
|
||||||
|
ID string
|
||||||
|
Profile types.MemberProfile
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetMember(r *http.Request, session MemberData) *http.Request {
|
||||||
ctx := context.WithValue(r.Context(), keyMemberCtx, session)
|
ctx := context.WithValue(r.Context(), keyMemberCtx, session)
|
||||||
return r.WithContext(ctx)
|
return r.WithContext(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetMember(r *http.Request) types.Session {
|
func GetMember(r *http.Request) MemberData {
|
||||||
return r.Context().Value(keyMemberCtx).(types.Session)
|
return r.Context().Value(keyMemberCtx).(MemberData)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *MembersHandler) ExtractMember(next http.Handler) http.Handler {
|
func (h *MembersHandler) ExtractMember(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
memberId := chi.URLParam(r, "memberId")
|
memberId := chi.URLParam(r, "memberId")
|
||||||
session, ok := h.sessions.Get(memberId)
|
profile, err := h.members.Select(memberId)
|
||||||
if !ok {
|
if err != nil {
|
||||||
utils.HttpNotFound(w, "Member was not found.")
|
utils.HttpNotFound(w, err)
|
||||||
} else {
|
} else {
|
||||||
next.ServeHTTP(w, SetMember(r, session))
|
next.ServeHTTP(w, SetMember(r, MemberData{
|
||||||
|
ID: memberId,
|
||||||
|
Profile: profile,
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@ import (
|
|||||||
|
|
||||||
type ApiManagerCtx struct {
|
type ApiManagerCtx struct {
|
||||||
sessions types.SessionManager
|
sessions types.SessionManager
|
||||||
|
members types.MemberManager
|
||||||
desktop types.DesktopManager
|
desktop types.DesktopManager
|
||||||
capture types.CaptureManager
|
capture types.CaptureManager
|
||||||
routers map[string]func(chi.Router)
|
routers map[string]func(chi.Router)
|
||||||
@ -22,6 +23,7 @@ type ApiManagerCtx struct {
|
|||||||
|
|
||||||
func New(
|
func New(
|
||||||
sessions types.SessionManager,
|
sessions types.SessionManager,
|
||||||
|
members types.MemberManager,
|
||||||
desktop types.DesktopManager,
|
desktop types.DesktopManager,
|
||||||
capture types.CaptureManager,
|
capture types.CaptureManager,
|
||||||
conf *config.Server,
|
conf *config.Server,
|
||||||
@ -29,6 +31,7 @@ func New(
|
|||||||
|
|
||||||
return &ApiManagerCtx{
|
return &ApiManagerCtx{
|
||||||
sessions: sessions,
|
sessions: sessions,
|
||||||
|
members: members,
|
||||||
desktop: desktop,
|
desktop: desktop,
|
||||||
capture: capture,
|
capture: capture,
|
||||||
routers: make(map[string]func(chi.Router)),
|
routers: make(map[string]func(chi.Router)),
|
||||||
@ -45,7 +48,7 @@ func (api *ApiManagerCtx) Route(r chi.Router) {
|
|||||||
r.Post("/logout", api.Logout)
|
r.Post("/logout", api.Logout)
|
||||||
r.Get("/whoami", api.Whoami)
|
r.Get("/whoami", api.Whoami)
|
||||||
|
|
||||||
membersHandler := members.New(api.sessions)
|
membersHandler := members.New(api.members)
|
||||||
r.Route("/members", membersHandler.Route)
|
r.Route("/members", membersHandler.Route)
|
||||||
|
|
||||||
roomHandler := room.New(api.sessions, api.desktop, api.capture)
|
roomHandler := room.New(api.sessions, api.desktop, api.capture)
|
||||||
|
16
neko.go
16
neko.go
@ -134,17 +134,18 @@ func (neko *Neko) Preflight() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (neko *Neko) Start() {
|
func (neko *Neko) Start() {
|
||||||
neko.memberManager = member.New(
|
|
||||||
neko.Configs.Member,
|
|
||||||
)
|
|
||||||
if err := neko.memberManager.Connect(); err != nil {
|
|
||||||
neko.logger.Panic().Err(err).Msg("unable to connect to member manager")
|
|
||||||
}
|
|
||||||
|
|
||||||
neko.sessionManager = session.New(
|
neko.sessionManager = session.New(
|
||||||
neko.Configs.Session,
|
neko.Configs.Session,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
neko.memberManager = member.New(
|
||||||
|
neko.Configs.Member,
|
||||||
|
)
|
||||||
|
|
||||||
|
if err := neko.memberManager.Connect(); err != nil {
|
||||||
|
neko.logger.Panic().Err(err).Msg("unable to connect to member manager")
|
||||||
|
}
|
||||||
|
|
||||||
neko.desktopManager = desktop.New(
|
neko.desktopManager = desktop.New(
|
||||||
neko.Configs.Desktop,
|
neko.Configs.Desktop,
|
||||||
)
|
)
|
||||||
@ -173,6 +174,7 @@ func (neko *Neko) Start() {
|
|||||||
|
|
||||||
neko.apiManager = api.New(
|
neko.apiManager = api.New(
|
||||||
neko.sessionManager,
|
neko.sessionManager,
|
||||||
|
neko.memberManager,
|
||||||
neko.desktopManager,
|
neko.desktopManager,
|
||||||
neko.captureManager,
|
neko.captureManager,
|
||||||
neko.Configs.Server,
|
neko.Configs.Server,
|
||||||
|
Loading…
Reference in New Issue
Block a user