Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
gqcn committed Dec 25, 2021
1 parent 0df07e1 commit e136c2d
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 43 deletions.
16 changes: 8 additions & 8 deletions internal/handler/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ import (
"github.com/gogf/gf/v2/util/gconv"
)

var Chat = &handlerChat{
var Chat = hChat{
Users: gmap.New(true),
Names: gset.NewStrSet(true),
}

type handlerChat struct {
type hChat struct {
Users *gmap.Map // All users in chat.
Names *gset.StrSet // All names in chat for unique name validation.
}

func (h *handlerChat) Index(ctx context.Context, req *apiv1.ChatIndexReq) (res *apiv1.ChatIndexRes, err error) {
func (h *hChat) Index(ctx context.Context, req *apiv1.ChatIndexReq) (res *apiv1.ChatIndexRes, err error) {
var (
r = g.RequestFromCtx(ctx)
)
Expand All @@ -43,7 +43,7 @@ func (h *handlerChat) Index(ctx context.Context, req *apiv1.ChatIndexReq) (res *
return
}

func (h *handlerChat) Name(ctx context.Context, req *apiv1.ChatNameReq) (res *apiv1.ChatNameRes, err error) {
func (h *hChat) Name(ctx context.Context, req *apiv1.ChatNameReq) (res *apiv1.ChatNameRes, err error) {
var (
session = g.RequestFromCtx(ctx).Session
)
Expand All @@ -62,7 +62,7 @@ func (h *handlerChat) Name(ctx context.Context, req *apiv1.ChatNameReq) (res *ap
return
}

func (h *handlerChat) Websocket(ctx context.Context, req *apiv1.ChatWebsocketReq) (res *apiv1.ChatWebsocketRes, err error) {
func (h *hChat) Websocket(ctx context.Context, req *apiv1.ChatWebsocketReq) (res *apiv1.ChatWebsocketRes, err error) {
var (
r = g.RequestFromCtx(ctx)
ws *ghttp.WebSocket
Expand Down Expand Up @@ -141,7 +141,7 @@ func (h *handlerChat) Websocket(ctx context.Context, req *apiv1.ChatWebsocketReq
}

// write sends message to current client.
func (h *handlerChat) write(ws *ghttp.WebSocket, msg model.ChatMsg) error {
func (h *hChat) write(ws *ghttp.WebSocket, msg model.ChatMsg) error {
msgBytes, err := gjson.Encode(msg)
if err != nil {
return err
Expand All @@ -150,7 +150,7 @@ func (h *handlerChat) write(ws *ghttp.WebSocket, msg model.ChatMsg) error {
}

// writeGroup sends message to all clients.
func (h *handlerChat) writeGroup(msg model.ChatMsg) error {
func (h *hChat) writeGroup(msg model.ChatMsg) error {
b, err := gjson.Encode(msg)
if err != nil {
return err
Expand All @@ -165,7 +165,7 @@ func (h *handlerChat) writeGroup(msg model.ChatMsg) error {
}

// writeGroupWithTypeList sends "list" type message to all clients that can update users list in each client.
func (h *handlerChat) writeGroupWithTypeList() error {
func (h *hChat) writeGroupWithTypeList() error {
array := garray.NewSortedStrArray()
h.Names.Iterator(func(v string) bool {
array.Add(v)
Expand Down
20 changes: 9 additions & 11 deletions internal/handler/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,39 @@ import (
"github.com/gogf/gf/v2/errors/gerror"
)

var (
User = handlerUser{}
)
var User = hUser{}

type handlerUser struct{}
type hUser struct{}

func (a *handlerUser) SignUp(ctx context.Context, req *apiv1.UserSignUpReq) (res *apiv1.UserSignUpRes, err error) {
func (h *hUser) SignUp(ctx context.Context, req *apiv1.UserSignUpReq) (res *apiv1.UserSignUpRes, err error) {
err = service.User().Create(ctx, model.UserCreateInput{
Passport: req.Passport,
Password: req.Password,
Nickname: req.Nickname,
})
return
}
func (a *handlerUser) SignIn(ctx context.Context, req *apiv1.UserSignInReq) (res *apiv1.UserSignInRes, err error) {
func (h *hUser) SignIn(ctx context.Context, req *apiv1.UserSignInReq) (res *apiv1.UserSignInRes, err error) {
err = service.User().SignIn(ctx, model.UserSignInInput{
Passport: req.Passport,
Password: req.Password,
})
return
}

func (a *handlerUser) IsSignedIn(ctx context.Context, req *apiv1.UserIsSignedInReq) (res *apiv1.UserIsSignedInRes, err error) {
func (h *hUser) IsSignedIn(ctx context.Context, req *apiv1.UserIsSignedInReq) (res *apiv1.UserIsSignedInRes, err error) {
res = &apiv1.UserIsSignedInRes{
OK: service.User().IsSignedIn(ctx),
}
return
}

func (a *handlerUser) SignOut(ctx context.Context, req *apiv1.UserSignOutReq) (res *apiv1.UserSignOutRes, err error) {
func (h *hUser) SignOut(ctx context.Context, req *apiv1.UserSignOutReq) (res *apiv1.UserSignOutRes, err error) {
err = service.User().SignOut(ctx)
return
}

func (a *handlerUser) CheckPassport(ctx context.Context, req *apiv1.UserCheckPassportReq) (res *apiv1.UserCheckPassportRes, err error) {
func (h *hUser) CheckPassport(ctx context.Context, req *apiv1.UserCheckPassportReq) (res *apiv1.UserCheckPassportRes, err error) {
available, err := service.User().IsPassportAvailable(ctx, req.Passport)
if err != nil {
return nil, err
Expand All @@ -54,7 +52,7 @@ func (a *handlerUser) CheckPassport(ctx context.Context, req *apiv1.UserCheckPas
return
}

func (a *handlerUser) CheckNickName(ctx context.Context, req *apiv1.UserCheckNickNameReq) (res *apiv1.UserCheckNickNameRes, err error) {
func (h *hUser) CheckNickName(ctx context.Context, req *apiv1.UserCheckNickNameReq) (res *apiv1.UserCheckNickNameRes, err error) {
available, err := service.User().IsNicknameAvailable(ctx, req.Nickname)
if err != nil {
return nil, err
Expand All @@ -65,7 +63,7 @@ func (a *handlerUser) CheckNickName(ctx context.Context, req *apiv1.UserCheckNic
return
}

func (a *handlerUser) Profile(ctx context.Context, req *apiv1.UserProfileReq) (res *apiv1.UserProfileRes, err error) {
func (h *hUser) Profile(ctx context.Context, req *apiv1.UserProfileReq) (res *apiv1.UserProfileRes, err error) {
res = &apiv1.UserProfileRes{
User: service.User().GetProfile(ctx),
}
Expand Down
10 changes: 5 additions & 5 deletions internal/service/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ var (
)

// Context returns the interface of Context service.
func Context() SContext {
return insContext
func Context() *SContext {
return &insContext
}

// Init initializes and injects custom business context object into request context.
func (s SContext) Init(r *ghttp.Request, customCtx *model.Context) {
func (s *SContext) Init(r *ghttp.Request, customCtx *model.Context) {
r.SetCtxVar(consts.ContextKey, customCtx)
}

// Get retrieves and returns the user object from context.
// It returns nil if nothing found in given context.
func (s SContext) Get(ctx context.Context) *model.Context {
func (s *SContext) Get(ctx context.Context) *model.Context {
value := ctx.Value(consts.ContextKey)
if value == nil {
return nil
Expand All @@ -42,6 +42,6 @@ func (s SContext) Get(ctx context.Context) *model.Context {
}

// SetUser injects business user object into context.
func (s SContext) SetUser(ctx context.Context, ctxUser *model.ContextUser) {
func (s *SContext) SetUser(ctx context.Context, ctxUser *model.ContextUser) {
s.Get(ctx).User = ctxUser
}
10 changes: 5 additions & 5 deletions internal/service/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ var (
)

// Middleware returns the interface of Middleware service.
func Middleware() SMiddleware {
return insMiddleware
func Middleware() *SMiddleware {
return &insMiddleware
}

// Ctx injects custom business context variable into context of current request.
func (s SMiddleware) Ctx(r *ghttp.Request) {
func (s *SMiddleware) Ctx(r *ghttp.Request) {
customCtx := &model.Context{
Session: r.Session,
}
Expand All @@ -40,7 +40,7 @@ func (s SMiddleware) Ctx(r *ghttp.Request) {
}

// Auth validates the request to allow only signed-in users visit.
func (s SMiddleware) Auth(r *ghttp.Request) {
func (s *SMiddleware) Auth(r *ghttp.Request) {
if User().IsSignedIn(r.Context()) {
r.Middleware.Next()
} else {
Expand All @@ -49,7 +49,7 @@ func (s SMiddleware) Auth(r *ghttp.Request) {
}

// CORS allows Cross-origin resource sharing.
func (s SMiddleware) CORS(r *ghttp.Request) {
func (s *SMiddleware) CORS(r *ghttp.Request) {
r.Response.CORSDefault()
r.Middleware.Next()
}
10 changes: 5 additions & 5 deletions internal/service/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ var (
)

// Session returns the interface of Session service.
func Session() SSession {
return insSession
func Session() *SSession {
return &insSession
}

// SetUser sets user into the session.
func (s SSession) SetUser(ctx context.Context, user *entity.User) error {
func (s *SSession) SetUser(ctx context.Context, user *entity.User) error {
return Context().Get(ctx).Session.Set(consts.UserSessionKey, user)
}

// GetUser retrieves and returns the user from session.
// It returns nil if the user did not sign in.
func (s SSession) GetUser(ctx context.Context) *entity.User {
func (s *SSession) GetUser(ctx context.Context) *entity.User {
customCtx := Context().Get(ctx)
if customCtx != nil {
if v := customCtx.Session.MustGet(consts.UserSessionKey); !v.IsNil() {
Expand All @@ -42,7 +42,7 @@ func (s SSession) GetUser(ctx context.Context) *entity.User {
}

// RemoveUser removes user rom session.
func (s SSession) RemoveUser(ctx context.Context) error {
func (s *SSession) RemoveUser(ctx context.Context) error {
customCtx := Context().Get(ctx)
if customCtx != nil {
return customCtx.Session.Remove(consts.UserSessionKey)
Expand Down
18 changes: 9 additions & 9 deletions internal/service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ var (
)

// User returns the interface of User service.
func User() SUser {
return insUser
func User() *SUser {
return &insUser
}

// Create creates user account.
func (s SUser) Create(ctx context.Context, in model.UserCreateInput) (err error) {
func (s *SUser) Create(ctx context.Context, in model.UserCreateInput) (err error) {
// If Nickname is not specified, it then uses Passport as its default Nickname.
if in.Nickname == "" {
in.Nickname = in.Passport
Expand Down Expand Up @@ -62,15 +62,15 @@ func (s SUser) Create(ctx context.Context, in model.UserCreateInput) (err error)
}

// IsSignedIn checks and returns whether current user is already signed-in.
func (s SUser) IsSignedIn(ctx context.Context) bool {
func (s *SUser) IsSignedIn(ctx context.Context) bool {
if v := Context().Get(ctx); v != nil && v.User != nil {
return true
}
return false
}

// SignIn creates session for given user account.
func (s SUser) SignIn(ctx context.Context, in model.UserSignInInput) (err error) {
func (s *SUser) SignIn(ctx context.Context, in model.UserSignInInput) (err error) {
var user *entity.User
err = dao.User.Ctx(ctx).Where(dto.User{
Passport: in.Passport,
Expand All @@ -94,12 +94,12 @@ func (s SUser) SignIn(ctx context.Context, in model.UserSignInInput) (err error)
}

// SignOut removes the session for current signed-in user.
func (s SUser) SignOut(ctx context.Context) error {
func (s *SUser) SignOut(ctx context.Context) error {
return Session().RemoveUser(ctx)
}

// IsPassportAvailable checks and returns given passport is available for signing up.
func (s SUser) IsPassportAvailable(ctx context.Context, passport string) (bool, error) {
func (s *SUser) IsPassportAvailable(ctx context.Context, passport string) (bool, error) {
count, err := dao.User.Ctx(ctx).Where(dto.User{
Passport: passport,
}).Count()
Expand All @@ -110,7 +110,7 @@ func (s SUser) IsPassportAvailable(ctx context.Context, passport string) (bool,
}

// IsNicknameAvailable checks and returns given nickname is available for signing up.
func (s SUser) IsNicknameAvailable(ctx context.Context, nickname string) (bool, error) {
func (s *SUser) IsNicknameAvailable(ctx context.Context, nickname string) (bool, error) {
count, err := dao.User.Ctx(ctx).Where(dto.User{
Nickname: nickname,
}).Count()
Expand All @@ -121,6 +121,6 @@ func (s SUser) IsNicknameAvailable(ctx context.Context, nickname string) (bool,
}

// GetProfile retrieves and returns current user info in session.
func (s SUser) GetProfile(ctx context.Context) *entity.User {
func (s *SUser) GetProfile(ctx context.Context) *entity.User {
return Session().GetUser(ctx)
}

0 comments on commit e136c2d

Please sign in to comment.