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
44 changes: 23 additions & 21 deletions apiService/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ 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/v12"
"github.com/kataras/iris/v12/sessions"
"github.com/kataras/iris/v12/sessions/sessiondb/redis"
"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 Iint(framework *iris.Application, idGen *generator.IdGenerator, store *storage.Storage) {
framework.Use(iris.Gzip)

redisAddr, err := config.GetConfigString(consts.RedisAddress)
if err != nil {
Expand All @@ -34,22 +33,25 @@ 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,
}

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
db := redis.New(redis.Config{
Network: "tcp",
Addr: redisAddr,
Timeout: time.Duration(30) * time.Second,
MaxActive: 4,
Password: redisPsw,
Database: "0",
Prefix: redisPrefix,
Delim: "-",
Driver: redis.Redigo(), // redis.Radix() can be used instead.
})

framework.UseSessionDB(db)
sessManager := sessions.New(sessions.Config{
Cookie: cookie,
Expires: 2 * time.Hour,
})
sessManager.UseDatabase(db)
framework.Use(sessManager.Handler())

addrStr, err := config.GetConfigString(consts.LeafMsgServiceAddress)
if err != nil {
Expand All @@ -67,5 +69,5 @@ func Iint(framework *iris.Framework, idGen *generator.IdGenerator, store *storag
if err != nil {
log.Fatalf(consts.ErrGetConfigFailed(consts.ApiServiceAddress, err))
}
framework.Static("/static", staicPath, 1)
framework.HandleDir("/static", staicPath)
}
24 changes: 14 additions & 10 deletions apiService/api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
"net/http"
"time"

"github.com/kataras/iris"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/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,7 +20,7 @@ type AuthApi struct {
store *storage.Storage
}

func (au *AuthApi) RegisterRoute(framework *iris.Framework) {
func (au *AuthApi) RegisterRoute(framework *iris.Application) {
framework.Post("/login", au.login)
framework.Post("/logout", au.login)
}
Expand All @@ -39,24 +39,28 @@ func newToken(id int64) (string, error) {
return util.NewToken(id, privateToken, time.Hour*12), nil
}

func (au *AuthApi) login(c *iris.Context) {
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"))
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.JSON(http.StatusInternalServerError, dto.InternalErrRsp())
c.StatusCode(iris.StatusInternalServerError)
c.JSON(dto.InternalErrRsp())
return
}
if getHashedPassword(loginReq.Password, user.Salt) != user.Password {
c.JSON(http.StatusUnauthorized, dto.PasswordNotMatchErrRsp())
c.StatusCode(iris.StatusUnauthorized)
c.JSON(dto.PasswordNotMatchErrRsp())
return
}
c.Session().Set("Id", user.Id)

sessions.Get(c).Set("Id", user.Id)

var userDto dto.UserInfo
userDto.Id = fmt.Sprintf("%d", user.Id)
Expand All @@ -66,9 +70,9 @@ func (au *AuthApi) login(c *iris.Context) {

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

func (au *AuthApi) logout(c *iris.Context) {
func (au *AuthApi) logout(c iris.Context) {

}
41 changes: 23 additions & 18 deletions apiService/api/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package api

import (
"fmt"
"net/http"

"github.com/kataras/iris"
"github.com/kataras/iris/v12"
"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 +15,50 @@ 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)

}

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

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")
func (ga *GroupApi) getGroupDetail(c iris.Context) {
gId, err := c.Params().GetInt64("id")
if err != nil {
c.JSON(http.StatusBadRequest, dto.ParameterErrRsp("id"))
c.StatusCode(iris.StatusBadRequest)
c.JSON(dto.ParameterErrRsp("id"))
return
}
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 +82,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 +97,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 +117,5 @@ func (ga *GroupApi) getGroupList(c *iris.Context) {
BaseRsp: *dto.SuccessRsp(),
}
rsp.Data.Groups = groupsDto[:len(groups)]
c.JSON(200, &rsp)
c.JSON(&rsp)
}
59 changes: 34 additions & 25 deletions apiService/api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package api

import (
"fmt"
"net/http"

"github.com/kataras/iris"
"github.com/kataras/iris/v12"
"github.com/longchat/longChat-Server/apiService/api/dto"
"github.com/longchat/longChat-Server/common/log"
"github.com/longchat/longChat-Server/common/util"
Expand All @@ -19,43 +18,47 @@ type UserApi struct {
serverAddrs []string
}

func (ua *UserApi) RegisterRoute(framework *iris.Framework) {
func (ua *UserApi) RegisterRoute(framework *iris.Application) {
users := framework.Party("/users")
users.Post("", ua.createUser)
users.Put("/:id", ua.updateInfo)
users.Get("/:id", ua.getInfo)
users.Get("/:id/serveraddr", ua.getserverAddr)
}

func (ua *UserApi) getserverAddr(c *iris.Context) {
uid, err := c.ParamInt64("id")
func (ua *UserApi) getserverAddr(c iris.Context) {
uid, err := c.Params().GetInt64("id")
if err != nil {
c.JSON(http.StatusBadRequest, dto.ParameterErrRsp("id"))
c.StatusCode(iris.StatusBadRequest)
c.JSON(dto.ParameterErrRsp("id"))
return
}
clusterId, err := graph.GetClusterByUserId(uid)
if err != nil {
log.ERROR.Printf("get user cluster id from graph failed!err:=%v\n", uid, err)
c.JSON(http.StatusInternalServerError, dto.InternalErrRsp())
log.ERROR.Printf("get user cluster id(%d) from graph failed!err:=%v\n", uid, err)
c.StatusCode(iris.StatusInternalServerError)
c.JSON(dto.InternalErrRsp())
return
}

id := clusterId % len(ua.serverAddrs)
userRsp := dto.GetUserServerAddrRsp{BaseRsp: *dto.SuccessRsp()}
userRsp.Data.Addr = ua.serverAddrs[id]
c.JSON(http.StatusOK, &userRsp)
c.JSON(&userRsp)
}

func (ua *UserApi) getInfo(c *iris.Context) {
uid, err := c.ParamInt64("id")
func (ua *UserApi) getInfo(c iris.Context) {
uid, err := c.Params().GetInt64("id")
if err != nil {
c.JSON(http.StatusBadRequest, dto.ParameterErrRsp("id"))
c.StatusCode(iris.StatusBadRequest)
c.JSON(dto.ParameterErrRsp("id"))
return
}
user, err := ua.store.GetUserById(uid)
if err != nil {
log.ERROR.Printf("get usser(%d) from storage failed!err:=%v\n", uid, err)
c.JSON(http.StatusInternalServerError, dto.InternalErrRsp())
c.StatusCode(iris.StatusInternalServerError)
c.JSON(dto.InternalErrRsp())
return
}
userRsp := dto.GetUserInfoRsp{BaseRsp: *dto.SuccessRsp()}
Expand All @@ -65,49 +68,55 @@ func (ua *UserApi) getInfo(c *iris.Context) {
Avatar: user.Avatar,
Introduce: user.Introduce,
}
c.JSON(http.StatusOK, &userRsp)
c.JSON(&userRsp)
}

func (ua *UserApi) updateInfo(c *iris.Context) {
func (ua *UserApi) updateInfo(c iris.Context) {
var infoReq dto.UpdateInfoReq
err := c.ReadJSON(&infoReq)
if err != nil {
c.JSON(http.StatusBadRequest, dto.PostDataErrRsp("UpdateInfoReq"))
c.StatusCode(iris.StatusBadRequest)
c.JSON(dto.PostDataErrRsp("UpdateInfoReq"))
return
}
uid, err := c.ParamInt64("id")
uid, err := c.Params().GetInt64("id")
if err != nil {
c.JSON(http.StatusBadRequest, dto.ParameterErrRsp("id"))
c.StatusCode(iris.StatusBadRequest)
c.JSON(dto.ParameterErrRsp("id"))
return
}
err = ua.store.UpdateUserInfo(uid, infoReq.NickName, infoReq.Avatar, infoReq.Introduce)
if err != nil {
log.ERROR.Printf("UpdateUserInfo from storage failed!err:=%v\n", err)
c.JSON(http.StatusInternalServerError, dto.InternalErrRsp())
c.StatusCode(iris.StatusInternalServerError)
c.JSON(dto.InternalErrRsp())
return
}
c.JSON(http.StatusOK, dto.SuccessRsp())
c.JSON(dto.SuccessRsp())
}

func (ua *UserApi) createUser(c *iris.Context) {
func (ua *UserApi) createUser(c iris.Context) {
var userReq dto.CreateUserReq
err := c.ReadJSON(&userReq)
if err != nil {
c.JSON(http.StatusBadRequest, dto.PostDataErrRsp("CreateUserReq"))
c.StatusCode(iris.StatusBadRequest)
c.JSON(dto.PostDataErrRsp("CreateUserReq"))
return
}
id, err := ua.idGen.Generate(generator.GenerateReq_User)
if err != nil {
c.JSON(http.StatusInternalServerError, dto.InternalErrRsp())
c.StatusCode(iris.StatusInternalServerError)
c.JSON(dto.InternalErrRsp())
return
}
salt := util.RandomString(8)
hashedPassword := getHashedPassword(userReq.PassWord, salt)
err = ua.store.CreateUser(id, userReq.UserName, hashedPassword, salt, c.RemoteAddr())
if err != nil {
log.ERROR.Printf("CreateUser from storage failed!err:=%v\n", err)
c.JSON(http.StatusInternalServerError, dto.InternalErrRsp())
c.StatusCode(iris.StatusInternalServerError)
c.JSON(dto.InternalErrRsp())
return
}
c.JSON(http.StatusOK, dto.SuccessRsp())
c.JSON(dto.SuccessRsp())
}
Loading