This repository has been archived by the owner on Feb 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.go
98 lines (89 loc) · 3.2 KB
/
cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"bufio"
"context"
"flag"
"fmt"
"github.com/brianvoe/gofakeit/v6"
log "github.com/sirupsen/logrus"
"github.com/umi0410/streamingChat/adapter"
"github.com/umi0410/streamingChat/client"
"github.com/umi0410/streamingChat/hotWord"
"github.com/umi0410/streamingChat/server"
"math/rand"
"os"
"os/signal"
"strconv"
"time"
)
var (
mode string
scanner = bufio.NewScanner(os.Stdin)
redisAddr = flag.String("redisAddr", "localhost:6379", "접속할 Redis의 주소와 포트")
serverAddr = flag.String("serverAddr", "localhost:50051", "접속할 gRPC 서버 주소와 포트")
username = flag.String("username", "", "클라이언트로 이용 시 채팅방에 접속할 username")
randomUsername = flag.Bool("randomUsername", false, "username을 랜덤으로 부여받을 것인지")
sendRandomChatMessage = flag.Bool("sendRandomChatMessage", false, "개발 과정을 위해 dummy 채팅 메시지를 자동으로 전송하는 클라이언트를 실행할 것인지")
)
func init() {
flag.Parse()
mode = flag.Arg(0)
log.SetLevel(log.DebugLevel)
log.SetFormatter(&log.TextFormatter{
ForceColors: true,
DisableColors: false,
DisableTimestamp: true,
})
rand.Seed(time.Now().Unix())
}
func main() {
ctx := WithGracefullyShutDownContext()
if mode == "server" {
messageAdapter := adapter.NewRedisMessageAdapter(ctx, *redisAddr, "chatroom")
log.Error(streamingChat.NewChatServer(*serverAddr, messageAdapter).Start(ctx))
} else if mode == "client" {
if *randomUsername {
*username += getRandomUsername()
}
if *username == "" {
fmt.Print("Please input your username: ")
scanner.Scan()
*username += scanner.Text()
}
c := client.NewChatClient(*serverAddr, *sendRandomChatMessage, *username+getRandomAvatar())
log.Error(c.Start(ctx))
} else if mode == "fakeClient" {
for _, fakeName := range []string{"Dummy", "Mike", "Coke", "Pizza", "Pasta"} {
c := client.NewChatClient(*serverAddr, *sendRandomChatMessage, fakeName+getRandomAvatar())
log.Error(c.Start(ctx))
}
c := client.NewChatClient(*serverAddr, *sendRandomChatMessage, "Chocolate")
log.Error(c.Start(ctx))
} else if mode == "hotWordCalculator" {
c := hotWord.NewHotWordCalculator(ctx, adapter.NewRedisMessageAdapter(ctx, *redisAddr, "chatroom"))
log.Error(c.Run())
} else {
log.Error(flag.Args(), mode)
panic("Invalid mode, given " + mode)
}
}
func WithGracefullyShutDownContext() context.Context {
sigInt := make(chan os.Signal, 10)
signal.Notify(sigInt, os.Interrupt)
ctx, cancel := context.WithCancel(context.Background())
go func() {
<-sigInt
log.Warning("인터럽트 발생! context를 Cancel합니다.")
cancel()
}()
return ctx
}
func getRandomAvatar() string {
randomAvatars := []string{
"🚀", "🐵", "🦍", "🐶", "🐺", "🐱", "🦁", "🐅", "🐷", "🐑", "🍎", "🍐", "🍑", "🍅", "🥝", "🥦", "🏖️", "🌋", "🏛️", "🏞️", "🏦", "🏭", "📊", "🎨", "⏩", "🤖", "🌻", "🌼", "🌲", "💮", "🌊", "🎄", "🔥",
}
return randomAvatars[rand.Intn(len(randomAvatars))]
}
func getRandomUsername() string {
return gofakeit.LastName() + strconv.Itoa(rand.Intn(100))
}