-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
184 lines (153 loc) · 4.63 KB
/
main.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"database/sql"
"log"
"net"
"net/http"
"os"
"time"
conf "main/config"
grpcCalendar "main/delivery/grpc/calendar"
protoCalendar "main/delivery/grpc/calendar/proto"
grpcChat "main/delivery/grpc/chat"
protoChat "main/delivery/grpc/chat/proto"
ws "main/delivery/ws"
localStore "main/repository/local-storage"
pgstore "main/repository/pg"
chatusecase "main/usecase/chat"
_ "main/docs"
"github.com/gorilla/mux"
httpSwagger "github.com/swaggo/http-swagger"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/keepalive"
)
var urlDB string
var filestoragePath string
var chatFilesPath string
var homeworkFilesPath string
var solutionFilesPath string
var urlDomain string
var calendarGrpcUrl string
func loggingAndCORSHeadersMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println(r.RequestURI, r.Method)
for header := range conf.Headers {
w.Header().Set(header, conf.Headers[header])
}
next.ServeHTTP(w, r)
})
}
func init() {
var exist bool
calendarGrpcUrl, exist = os.LookupEnv(conf.CALENDAR_GRPC_URL)
if !exist || len(calendarGrpcUrl) == 0 {
log.Fatalln("could not get calendar grpc url from env")
}
pgUser, exist := os.LookupEnv(conf.PG_USER)
if !exist || len(pgUser) == 0 {
log.Fatalln("could not get database host from env")
}
pgPwd, exist := os.LookupEnv(conf.PG_PWD)
if !exist || len(pgPwd) == 0 {
log.Fatalln("could not get database password from env")
}
pgHost, exist := os.LookupEnv(conf.PG_HOST)
if !exist || len(pgHost) == 0 {
log.Fatalln("could not get database host from env")
}
pgPort, exist := os.LookupEnv(conf.PG_PORT)
if !exist || len(pgPort) == 0 {
log.Fatalln("could not get database port from env")
}
pgDB, exist := os.LookupEnv(conf.PG_DB)
if !exist || len(pgDB) == 0 {
log.Fatalln("could not get database name from env")
}
urlDB = "postgres://" + pgUser + ":" + pgPwd + "@" + pgHost + ":" + pgPort + "/" + pgDB
filestoragePath, exist = os.LookupEnv(conf.FILESTORAGE_PATH)
if !exist || len(filestoragePath) == 0 {
log.Fatalln("could not get filestorage path from env")
}
chatFilesPath, exist = os.LookupEnv(conf.CHAT_FILES_PATH)
if !exist || len(chatFilesPath) == 0 {
log.Fatalln("could not get chat files path from env")
}
homeworkFilesPath, exist = os.LookupEnv(conf.HOMEWORK_FILES_PATH)
if !exist || len(homeworkFilesPath) == 0 {
log.Fatalln("could not get homework files path from env")
}
solutionFilesPath, exist = os.LookupEnv(conf.SOLUTION_FILES_PATH)
if !exist || len(solutionFilesPath) == 0 {
log.Fatalln("could not get solution files path from env")
}
urlDomain, exist = os.LookupEnv(conf.URL_DOMAIN)
if !exist || len(urlDomain) == 0 {
log.Fatalln("could not get url domain from env")
}
}
func main() {
myRouter := mux.NewRouter()
db, err := sql.Open("pgx", urlDB)
if err != nil {
log.Fatalln("could not connect to database")
}
defer db.Close()
if err := db.Ping(); err != nil {
log.Fatalln("unable to reach database ", err)
}
log.Println("database is reachable")
hub := ws.NewHub()
go hub.Run()
myRouter.HandleFunc(conf.PathWS, hub.AddConnection).Methods(http.MethodGet, http.MethodOptions)
myRouter.PathPrefix(conf.PathDocs).Handler(httpSwagger.WrapHandler)
myRouter.Use(loggingAndCORSHeadersMiddleware)
grcpConnCalendar, err := grpc.Dial(
calendarGrpcUrl,
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
log.Fatalln("cant create connecter to grpc calendar")
}
log.Println("connecter to grpc calendar service is created")
defer grcpConnCalendar.Close()
lis, err := net.Listen("tcp", conf.PortGRPC)
if err != nil {
log.Fatalln("cant listen grpc port", err)
}
server := grpc.NewServer(
grpc.MaxRecvMsgSize(1024*1024),
grpc.MaxConcurrentStreams(35),
grpc.KeepaliveParams(keepalive.ServerParameters{
Time: 1 * time.Second,
Timeout: 5 * time.Second,
}),
)
dataStore := pgstore.NewPostgreSqlStore(db)
fileStore := localStore.NewLocalStore(
chatFilesPath,
homeworkFilesPath,
solutionFilesPath,
filestoragePath,
)
calendar := grpcCalendar.NewCalendarService(
protoCalendar.NewCalendarClient(grcpConnCalendar),
)
usecase := chatusecase.NewChatUsecase(
hub,
dataStore,
fileStore,
calendar,
filestoragePath,
urlDomain,
)
grpcHandler := grpcChat.NewChatGrpcHander(usecase)
protoChat.RegisterChatServer(server, grpcHandler)
log.Println("starting grpc server at " + conf.PortGRPC)
go server.Serve(lis)
log.Println("starting web server at " + conf.PortWS)
err = http.ListenAndServe(conf.PortWS, myRouter)
if err != nil {
log.Fatalln("cant serve", err)
}
}