Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init log service #66

Merged
merged 3 commits into from
Apr 24, 2024
Merged
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ configs/configs.yml
./test.go
vendor/
minio_data/*

logs/logs*.json
8 changes: 4 additions & 4 deletions app/controller/user_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import (
"github.com/kavkaco/Kavka-Core/internal/domain/user"
"github.com/kavkaco/Kavka-Core/pkg/session"
"github.com/kavkaco/Kavka-Core/utils/bearer"
"go.uber.org/zap"
)

type UserController struct {
logger *zap.Logger
userService user.Service
chatService chat.Service
}

func NewUserController(userService user.Service, chatService chat.Service) *UserController {
return &UserController{userService, chatService}
func NewUserController(logger *zap.Logger, userService user.Service, chatService chat.Service) *UserController {
return &UserController{logger, userService, chatService}
}

func (ctrl *UserController) HandleLogin(ctx *gin.Context) {
Expand Down Expand Up @@ -55,8 +57,6 @@ func (ctrl *UserController) HandleVerifyOTP(ctx *gin.Context) {
}

func (ctrl *UserController) HandleRefreshToken(ctx *gin.Context) {
refreshToken := ctx.GetHeader("refresh") //nolint

refreshToken, bearerRfOk := bearer.RefreshToken(ctx)

if bearerRfOk {
Expand Down
5 changes: 3 additions & 2 deletions app/router/user_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/kavkaco/Kavka-Core/app/controller"
"github.com/kavkaco/Kavka-Core/internal/domain/chat"
"github.com/kavkaco/Kavka-Core/internal/domain/user"
"go.uber.org/zap"
)

type UserRouter struct {
Expand All @@ -14,8 +15,8 @@ type UserRouter struct {
router *gin.RouterGroup
}

func NewUserRouter(router *gin.RouterGroup, userService user.Service, chatService chat.Service) *UserRouter {
ctrl := controller.NewUserController(userService, chatService)
func NewUserRouter(logger *zap.Logger, router *gin.RouterGroup, userService user.Service, chatService chat.Service) *UserRouter {
ctrl := controller.NewUserController(logger, userService, chatService)

router.POST("/login", ctrl.HandleLogin)
router.POST("/verify_otp", ctrl.HandleVerifyOTP)
Expand Down
8 changes: 1 addition & 7 deletions app/socket/chats_handler.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package socket

import (
"log"

"go.mongodb.org/mongo-driver/bson/primitive"
)

Expand Down Expand Up @@ -48,15 +46,11 @@ func GetChat(event string, args MessageHandlerArgs) bool {
}

_, err := args.socketService.chatService.GetChat(staticID.(primitive.ObjectID))
if err != nil {
log.Println("find chat error in socket:", err)
return false
}

// FIXME
// err = args.conn.WriteJSON(presenters.ChatAsJSON(event, chat))

return true
return err == nil
}

func CreateGroup(event string, args MessageHandlerArgs) bool {
Expand Down
10 changes: 5 additions & 5 deletions app/socket/socket_handler.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package socket

import (
"log"

"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/kavkaco/Kavka-Core/internal/domain/chat"
"github.com/kavkaco/Kavka-Core/internal/domain/message"
"github.com/kavkaco/Kavka-Core/internal/domain/user"
"github.com/kavkaco/Kavka-Core/utils/bearer"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.uber.org/zap"
)

var (
Expand All @@ -21,6 +20,7 @@ var (
)

type Service struct {
logger *zap.Logger
userService user.Service
chatService chat.Service
msgService message.Service
Expand All @@ -40,8 +40,8 @@ type MessageHandlerArgs struct {

var upgrader = websocket.Upgrader{}

func NewSocketService(app *gin.Engine, userService user.Service, chatService chat.Service, messageService message.Service) *Service {
socketService := &Service{userService, chatService, messageService}
func NewSocketService(logger *zap.Logger, app *gin.Engine, userService user.Service, chatService chat.Service, messageService message.Service) *Service {
socketService := &Service{logger, userService, chatService, messageService}

app.GET("/ws", socketService.handleWebsocket)

Expand Down Expand Up @@ -77,7 +77,7 @@ func (s *Service) handleWebsocket(ctx *gin.Context) {
var msgData *Message

if err := conn.ReadJSON(&msgData); err != nil {
log.Println("Unmarshal json error in socket:", err)
s.logger.Error("Unmarshal json error in socket: " + err.Error())
break
}

Expand Down
26 changes: 15 additions & 11 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ import (
messageRepository "github.com/kavkaco/Kavka-Core/internal/repository/message"
userRepository "github.com/kavkaco/Kavka-Core/internal/repository/user"
"github.com/kavkaco/Kavka-Core/internal/service"
"github.com/kavkaco/Kavka-Core/logs"
"github.com/kavkaco/Kavka-Core/pkg/session"
"github.com/kavkaco/Kavka-Core/pkg/sms_service"
)

func main() {
// Init Zap Logger
logger := logs.InitZapLogger()

// Define paths
TemplatesPath := config.ProjectRootPath + "/app/views/mail/"

Expand Down Expand Up @@ -50,24 +54,24 @@ func main() {
AllowCredentials: true,
}))

// ----- Init Services -----
session := session.NewSession(redisClient, configs.App.Auth)
smsService := sms_service.NewSmsService(&configs.SMS, TemplatesPath)
// Initializing various services and repositories used in the application
session := session.NewSession(logger, redisClient, configs.App.Auth)
smsService := sms_service.NewSmsService(logger, &configs.SMS, TemplatesPath)

userRepo := userRepository.NewRepository(mongoDB)
userService := service.NewUserService(userRepo, session, smsService)
userRepo := userRepository.NewRepository(logger, mongoDB)
userService := service.NewUserService(logger, userRepo, session, smsService)

chatRepo := chatRepository.NewRepository(mongoDB)
chatService := service.NewChatService(chatRepo, userRepo)
chatRepo := chatRepository.NewRepository(logger, mongoDB)
chatService := service.NewChatService(logger, chatRepo, userRepo)

messageRepo := messageRepository.NewRepository(mongoDB)
messageRepository := service.NewMessageService(messageRepo, chatRepo)
messageRepo := messageRepository.NewRepository(logger, mongoDB)
messageService := service.NewMessageService(logger, messageRepo, chatRepo)

// Init routes
router.NewUserRouter(app.Group("/users"), userService, chatService)
router.NewUserRouter(logger, app.Group("/users"), userService, chatService)

// Init Socket Server
socket.NewSocketService(app, userService, chatService, messageRepository)
socket.NewSocketService(logger, app, userService, chatService, messageService)

// Everything almost done!
err := app.Run(configs.App.HTTP.Address)
Expand Down
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
)

var (
EnvItems = []string{"devel", "prod"}
CurrentEnv string
EnvItems = []string{"devel", "prod"}
CurrentEnv string = EnvItems[0]
)

type (
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/minio/minio-go/v7 v7.0.69
github.com/stretchr/testify v1.8.4
go.mongodb.org/mongo-driver v1.12.1
go.uber.org/zap v1.27.0
gopkg.in/go-playground/assert.v1 v1.2.1
gopkg.in/yaml.v3 v3.0.1
)
Expand Down Expand Up @@ -37,6 +38,7 @@ require (
github.com/rs/xid v1.5.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/arch v0.5.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/net v0.21.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7Jul
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.mongodb.org/mongo-driver v1.12.1 h1:nLkghSU8fQNaK7oUmDhQFsnrtcoNy7Z6LVFKsEecqgE=
go.mongodb.org/mongo-driver v1.12.1/go.mod h1:/rGBTebI3XYboVmgz+Wv3Bcbl3aD0QF9zl6kDDw18rQ=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.5.0 h1:jpGode6huXQxcskEIpOCvrU+tzo81b6+oFLUYXWtH/Y=
golang.org/x/arch v0.5.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
Expand Down
9 changes: 6 additions & 3 deletions internal/repository/chat/chat_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.uber.org/zap"
)

var (
Expand All @@ -17,11 +18,12 @@ var (
)

type chatRepository struct {
logger *zap.Logger
chatsCollection *mongo.Collection
}

func NewRepository(db *mongo.Database) chat.Repository {
return &chatRepository{db.Collection(database.ChatsCollection)}
func NewRepository(logger *zap.Logger, db *mongo.Database) chat.Repository {
return &chatRepository{logger, db.Collection(database.ChatsCollection)}
}

func (repo *chatRepository) Create(newChat chat.Chat) (*chat.Chat, error) {
Expand Down Expand Up @@ -99,7 +101,8 @@ func (repo *chatRepository) GetUserChats(userStaticID primitive.ObjectID) ([]cha
"pipeline": []interface{}{
bson.M{
"$match": bson.M{
"$expr": bson.M{"$eq": []interface{}{"$chat_type", "direct"}}},
"$expr": bson.M{"$eq": []interface{}{"$chat_type", "direct"}},
},
},
},
},
Expand Down
14 changes: 9 additions & 5 deletions internal/repository/chat/chat_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
"go.uber.org/zap"
)

func TestChatRepository(t *testing.T) {
logger, _ := zap.NewDevelopment()
defer logger.Sync() // nolint

mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()

mt.Run("test create channel", func(mt *mtest.T) {
mt.AddMockResponses(mtest.CreateSuccessResponse())
chatRepo := NewRepository(mt.DB)
chatRepo := NewRepository(logger, mt.DB)

ownerStaticID := primitive.NewObjectID()
model := chat.NewChat(chat.TypeChannel, &chat.ChannelChatDetail{
Expand All @@ -44,7 +48,7 @@ func TestChatRepository(t *testing.T) {

mt.Run("test create direct", func(mt *mtest.T) {
mt.AddMockResponses(mtest.CreateSuccessResponse())
chatRepo := NewRepository(mt.DB)
chatRepo := NewRepository(logger, mt.DB)

user1StaticID := primitive.NewObjectID()
user2StaticID := primitive.NewObjectID()
Expand All @@ -63,7 +67,7 @@ func TestChatRepository(t *testing.T) {
})

mt.Run("test find by id", func(mt *mtest.T) {
chatRepo := NewRepository(mt.DB)
chatRepo := NewRepository(logger, mt.DB)

chatID := primitive.NewObjectID()
ownerStaticID := primitive.NewObjectID()
Expand Down Expand Up @@ -92,7 +96,7 @@ func TestChatRepository(t *testing.T) {
})

mt.Run("test find chat or sides by static id", func(mt *mtest.T) {
chatRepo := NewRepository(mt.DB)
chatRepo := NewRepository(logger, mt.DB)

chatID := primitive.NewObjectID()
user1StaticID := primitive.NewObjectID()
Expand Down Expand Up @@ -123,7 +127,7 @@ func TestChatRepository(t *testing.T) {
})

mt.Run("test find by sides", func(mt *mtest.T) {
chatRepo := NewRepository(mt.DB)
chatRepo := NewRepository(logger, mt.DB)

chatID := primitive.NewObjectID()
user1StaticID := primitive.NewObjectID()
Expand Down
6 changes: 4 additions & 2 deletions internal/repository/message/message_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.uber.org/zap"
)

var (
Expand All @@ -18,11 +19,12 @@ var (
)

type repository struct {
logger *zap.Logger
chatsCollection *mongo.Collection
}

func NewRepository(db *mongo.Database) message.Repository {
return &repository{db.Collection(database.ChatsCollection)}
func NewRepository(logger *zap.Logger, db *mongo.Database) message.Repository {
return &repository{logger, db.Collection(database.ChatsCollection)}
}

func (repo *repository) Insert(chatID primitive.ObjectID, msg *message.Message) (*message.Message, error) {
Expand Down
10 changes: 7 additions & 3 deletions internal/repository/message/message_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
"go.uber.org/zap"
)

func TestMessageRepository(t *testing.T) {
logger, _ := zap.NewDevelopment()
defer logger.Sync() // nolint

mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test insert text message", func(mt *mtest.T) {
messageRepo := NewRepository(mt.DB)
messageRepo := NewRepository(logger, mt.DB)

expectedResult := []bson.E{
{Key: "NModified", Value: 1},
Expand Down Expand Up @@ -47,7 +51,7 @@ func TestMessageRepository(t *testing.T) {
})

mt.Run("test delete message", func(mt *mtest.T) {
messageRepo := NewRepository(mt.DB)
messageRepo := NewRepository(logger, mt.DB)

ownerStaticID := primitive.NewObjectID()
chatModel := chat.NewChat(chat.TypeChannel, &chat.ChannelChatDetail{
Expand All @@ -74,7 +78,7 @@ func TestMessageRepository(t *testing.T) {
assert.NoError(t, err)
})
mt.Run("test update message", func(mt *mtest.T) {
messageRepo := NewRepository(mt.DB)
messageRepo := NewRepository(logger, mt.DB)

ownerStaticID := primitive.NewObjectID()
chatModel := chat.NewChat(chat.TypeChannel, &chat.ChannelChatDetail{
Expand Down
6 changes: 4 additions & 2 deletions internal/repository/user/user_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.uber.org/zap"
)

var (
Expand All @@ -18,11 +19,12 @@ var (
)

type userRepository struct {
logger *zap.Logger
usersCollection *mongo.Collection
}

func NewRepository(db *mongo.Database) user.UserRepository {
return &userRepository{db.Collection(database.UsersCollection)}
func NewRepository(logger *zap.Logger, db *mongo.Database) user.UserRepository {
return &userRepository{logger, db.Collection(database.UsersCollection)}
}

func (repo *userRepository) Create(user *user.User) (*user.User, error) {
Expand Down
Loading
Loading