Added chat microservice, removed air
This commit is contained in:
9
chat-service/repository/chat.go
Normal file
9
chat-service/repository/chat.go
Normal file
@ -0,0 +1,9 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"twitch-clone/chat-service/models"
|
||||
)
|
||||
|
||||
type ChatRepository interface {
|
||||
Store(*models.ChatMessage) error
|
||||
}
|
48
chat-service/repository/scylla/chat.go
Normal file
48
chat-service/repository/scylla/chat.go
Normal file
@ -0,0 +1,48 @@
|
||||
package scylla
|
||||
|
||||
import (
|
||||
"os"
|
||||
"twitch-clone/chat-service/models"
|
||||
|
||||
"github.com/gocql/gocql"
|
||||
"github.com/scylladb/gocqlx/v2"
|
||||
"github.com/scylladb/gocqlx/v2/table"
|
||||
)
|
||||
|
||||
var messageMetadata = table.Metadata{
|
||||
Name: "chat_service.messages",
|
||||
Columns: []string{"message_id", "from_user_id", "from_user", "to_user_id", "to_user", "content", "created_at"},
|
||||
}
|
||||
|
||||
var messageTable = table.New(messageMetadata)
|
||||
|
||||
type ChatRepository struct {
|
||||
cluster gocql.ClusterConfig
|
||||
}
|
||||
|
||||
func (r *ChatRepository) Store(msg *models.ChatMessage) error {
|
||||
session, err := gocqlx.WrapSession(r.cluster.CreateSession())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer session.Close()
|
||||
|
||||
q := session.Query(messageTable.Insert()).BindStruct(msg)
|
||||
if err := q.ExecRelease(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewChatRepository() *ChatRepository {
|
||||
cluster := gocql.NewCluster(os.Getenv("CHAT_SCYLLA_HOSTS"))
|
||||
|
||||
session, _ := gocqlx.WrapSession(cluster.CreateSession())
|
||||
Seed(session)
|
||||
session.Close()
|
||||
|
||||
return &ChatRepository{
|
||||
cluster: *cluster,
|
||||
}
|
||||
}
|
30
chat-service/repository/scylla/seed.go
Normal file
30
chat-service/repository/scylla/seed.go
Normal file
@ -0,0 +1,30 @@
|
||||
package scylla
|
||||
|
||||
import "github.com/scylladb/gocqlx/v2"
|
||||
|
||||
func Seed(session gocqlx.Session) error {
|
||||
err := session.ExecStmt(`
|
||||
CREATE KEYSPACE IF NOT EXISTS chat_service
|
||||
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = session.ExecStmt(`
|
||||
CREATE TABLE IF NOT EXISTS chat_service.messages (
|
||||
message_id bigint,
|
||||
from_user_id bigint,
|
||||
from_user text,
|
||||
to_user_id bigint,
|
||||
to_user text,
|
||||
content text,
|
||||
created_at timestamp,
|
||||
PRIMARY KEY (to_user_id, message_id)
|
||||
)`)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user