Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions apiService/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@ import (
"strings"
"time"

"github.com/iris-contrib/sessiondb/redis"
"github.com/iris-contrib/sessiondb/redis/service"
"github.com/kataras/iris"
iconfig "github.com/kataras/iris/config"
"github.com/kataras/iris/sessions"
"github.com/kataras/iris/sessions/sessiondb/redis"
"github.com/kataras/iris/sessions/sessiondb/redis/service"

"github.com/longchat/longChat-Server/common/config"
"github.com/longchat/longChat-Server/common/consts"
"github.com/longchat/longChat-Server/idService/generator"
"github.com/longchat/longChat-Server/storageService/storage"
)

func Iint(framework *iris.Framework, idGen *generator.IdGenerator, store *storage.Storage) {
framework.Config.Gzip = true

func Init(framework *iris.Application, idGen *generator.IdGenerator, store *storage.Storage) {
redisAddr, err := config.GetConfigString(consts.RedisAddress)
if err != nil {
log.Fatalf(consts.ErrGetConfigFailed(consts.RedisAddress, err))
Expand All @@ -34,22 +33,24 @@ func Iint(framework *iris.Framework, idGen *generator.IdGenerator, store *storag
if err != nil {
log.Fatalf(consts.ErrGetConfigFailed(consts.SessionCookieName, err))
}
framework.Config.Sessions = iconfig.Sessions{
Cookie: cookie,
GcDuration: time.Duration(2) * time.Hour,
}
sess := sessions.New(sessions.Config{
Cookie: cookie,
Expires: time.Duration(2) * time.Hour,
})

db := redis.New(service.Config{Network: service.DefaultRedisNetwork,
Addr: redisAddr,
Password: redisPsw,
Database: "0",
MaxIdle: 4,
MaxActive: 4,
IdleTimeout: service.DefaultRedisIdleTimeout,
Prefix: redisPrefix,
MaxAgeSeconds: service.DefaultRedisMaxAgeSeconds}) // optionally configure the bridge between your redis server
Addr: redisAddr,
Password: redisPsw,
Database: "0",
MaxIdle: 4,
MaxActive: 4,
IdleTimeout: service.DefaultRedisIdleTimeout,
Prefix: redisPrefix})

framework.UseSessionDB(db)
sess.UseDatabase(db)

framework.Use(iris.Gzip)
framework.StaticWeb("/static", staicPath)

addrStr, err := config.GetConfigString(consts.LeafMsgServiceAddress)
if err != nil {
Expand All @@ -59,13 +60,13 @@ func Iint(framework *iris.Framework, idGen *generator.IdGenerator, store *storag
ua := UserApi{idGen: idGen, store: store, serverAddrs: addrs}
ua.RegisterRoute(framework)
au := AuthApi{store: store}
au.RegisterRoute(framework)
au.RegisterRoute(framework, sess)
ga := GroupApi{idGen: idGen, store: store}
ga.RegisterRoute(framework)
ga.RegisterRoute(framework, sess)

staicPath, err := config.GetConfigString(consts.ApiServiceStaticPath)
if err != nil {
log.Fatalf(consts.ErrGetConfigFailed(consts.ApiServiceAddress, err))
}
framework.Static("/static", staicPath, 1)

}
71 changes: 40 additions & 31 deletions apiService/api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
"net/http"
"time"

"github.com/kataras/iris"
"github.com/kataras/iris/sessions"

"github.com/longchat/longChat-Server/apiService/api/dto"
"github.com/longchat/longChat-Server/common/config"
"github.com/longchat/longChat-Server/common/consts"
Expand All @@ -20,9 +21,9 @@ type AuthApi struct {
store *storage.Storage
}

func (au *AuthApi) RegisterRoute(framework *iris.Framework) {
framework.Post("/login", au.login)
framework.Post("/logout", au.login)
func (au *AuthApi) RegisterRoute(framework *iris.Application, sess *sessions.Sessions) {
framework.Post("/login", au.login(sess))
framework.Post("/logout", au.logout(sess))
}

func getHashedPassword(raw string, salt string) string {
Expand All @@ -39,36 +40,44 @@ func newToken(id int64) (string, error) {
return util.NewToken(id, privateToken, time.Hour*12), nil
}

func (au *AuthApi) login(c *iris.Context) {
var loginReq dto.LoginReq
err := c.ReadJSON(&loginReq)
if err != nil {
c.JSON(http.StatusBadRequest, dto.PostDataErrRsp("LoginReq"))
return
}
user, err := au.store.GetUserByUserName(loginReq.UserName)
if err != nil {
log.ERROR.Printf("GetUserByUserName(%s) from storage failed!err:=%v\n", loginReq.UserName, err)
c.JSON(http.StatusInternalServerError, dto.InternalErrRsp())
return
}
if getHashedPassword(loginReq.Password, user.Salt) != user.Password {
c.JSON(http.StatusUnauthorized, dto.PasswordNotMatchErrRsp())
return
}
c.Session().Set("Id", user.Id)
func (au *AuthApi) login(sess *sessions.Sessions) func(iris.Context) {
return func(c iris.Context) {
var loginReq dto.LoginReq
err := c.ReadJSON(&loginReq)
if err != nil {
c.StatusCode(iris.StatusBadRequest)
c.JSON(dto.PostDataErrRsp("LoginReq"))
return
}
user, err := au.store.GetUserByUserName(loginReq.UserName)
if err != nil {
log.ERROR.Printf("GetUserByUserName(%s) from storage failed!err:=%v\n", loginReq.UserName, err)
c.StatusCode(iris.StatusInternalServerError)
c.JSON(dto.InternalErrRsp())
return
}
if getHashedPassword(loginReq.Password, user.Salt) != user.Password {
c.StatusCode(iris.StatusUnauthorized)
c.JSON(dto.PasswordNotMatchErrRsp())
return
}
session := sess.Start(c)
session.Set("Id", user.Id)

var userDto dto.UserInfo
userDto.Id = fmt.Sprintf("%d", user.Id)
userDto.Avatar = user.Avatar
userDto.Introduce = user.Introduce
userDto.NickName = user.NickName
var userDto dto.UserInfo
userDto.Id = fmt.Sprintf("%d", user.Id)
userDto.Avatar = user.Avatar
userDto.Introduce = user.Introduce
userDto.NickName = user.NickName

var rsp dto.LoginRsp
rsp.Data.User = userDto
c.JSON(http.StatusOK, &rsp)
var rsp dto.LoginRsp
rsp.Data.User = userDto
c.JSON(&rsp)
}
}

func (au *AuthApi) logout(c *iris.Context) {
func (au *AuthApi) logout(sess *sessions.Sessions) func(iris.Context) {
return func(c iris.Context) {

}
}
48 changes: 19 additions & 29 deletions apiService/api/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package api

import (
"fmt"
"net/http"

"github.com/kataras/iris"

"github.com/longchat/longChat-Server/apiService/api/dto"
"github.com/longchat/longChat-Server/common/log"
"github.com/longchat/longChat-Server/idService/generator"
Expand All @@ -16,45 +16,34 @@ type GroupApi struct {
store *storage.Storage
}

func (ga *GroupApi) RegisterRoute(framework *iris.Framework) {
func (ga *GroupApi) RegisterRoute(framework *iris.Application) {
users := framework.Party("/groups")
users.Get("", ga.getGroupList)
users.Get("/:id", ga.getGroupDetail)
users.Post("/:id/members/:uid", ga.joinGroup)
users.Get("/{id:long}", ga.getGroupDetail)
users.Post("/{id:long}/members/{uid:long}", ga.joinGroup)

}

func (ga *GroupApi) joinGroup(c *iris.Context) {
gId, err := c.ParamInt64("id")
if err != nil {
c.JSON(http.StatusBadRequest, dto.ParameterErrRsp("id"))
return
}
uId, err := c.ParamInt64("uid")
if err != nil {
c.JSON(http.StatusBadRequest, dto.ParameterErrRsp("uid"))
return
}

func (ga *GroupApi) joinGroup(c iris.Context) {
gId, _ := c.Params().GetInt64("id")
uId, _ := c.Params().GetInt64("uid")
err = ga.store.AddUserGroup(uId, gId)
if err != nil {
c.JSON(http.StatusInternalServerError, dto.InternalErrRsp())
c.StatusCode(iris.StatusInternalServerError)
c.JSON(dto.InternalErrRsp())
return
}
rsp := dto.SuccessRsp()
c.JSON(200, &rsp)
c.JSON(&rsp)
}

func (ga *GroupApi) getGroupDetail(c *iris.Context) {
gId, err := c.ParamInt64("id")
if err != nil {
c.JSON(http.StatusBadRequest, dto.ParameterErrRsp("id"))
return
}
func (ga *GroupApi) getGroupDetail(c iris.Context) {
gId, _ := c.Params().GetInt64("id")
group, err := ga.store.GetGroupById(gId)
if err != nil {
log.ERROR.Printf("getGroupById(%d) from storage failed!err:=%v\n", gId, err)
c.JSON(http.StatusInternalServerError, dto.InternalErrRsp())
c.StatusCode(iris.StatusInternalServerError)
c.JSON(dto.InternalErrRsp())
return
}
users, err := ga.store.GetUsersByIds(group.Members)
Expand All @@ -78,10 +67,10 @@ func (ga *GroupApi) getGroupDetail(c *iris.Context) {
usersDto = append(usersDto, userDto)
}
rsp.Data.Group.Members = usersDto
c.JSON(200, &rsp)
c.JSON(&rsp)
}

func (ga *GroupApi) getGroupList(c *iris.Context) {
func (ga *GroupApi) getGroupList(c iris.Context) {
orderIdx, err := c.URLParamInt64("orderidx")
if err != nil {
orderIdx = 0
Expand All @@ -93,7 +82,8 @@ func (ga *GroupApi) getGroupList(c *iris.Context) {
groups, err := ga.store.GetGroupsByOrderId(orderIdx, limit)
if err != nil {
log.ERROR.Printf("GetGroupsByOrderIdx from storage failed!err:=%v\n", err)
c.JSON(http.StatusInternalServerError, dto.InternalErrRsp())
c.StatusCode(iris.StatusInternalServerError)
c.JSON(dto.InternalErrRsp())
return
}
groupsDto := make([]dto.Group, limit)
Expand All @@ -112,5 +102,5 @@ func (ga *GroupApi) getGroupList(c *iris.Context) {
BaseRsp: *dto.SuccessRsp(),
}
rsp.Data.Groups = groupsDto[:len(groups)]
c.JSON(200, &rsp)
c.JSON(&rsp)
}
Loading