forked from grupo8hackglobo2019/back-end
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (44 loc) · 1.36 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
package main
import (
"os"
"log"
"net/http"
"github.com/caarlos0/env"
"github.com/joho/godotenv"
"gopkg.in/olivere/elastic.v6"
"github.com/gorilla/websocket"
"github.com/grupo8hackglobo2019/back-end/handler"
"github.com/grupo8hackglobo2019/back-end/service"
"github.com/grupo8hackglobo2019/back-end/model"
)
func main() {
if err := godotenv.Load(); err != nil {
log.Println("File .env not found, reading configuration from ENV")
}
var cfg model.Config
if err := env.Parse(&cfg); err != nil {
log.Fatalln("Failed to parse ENV")
}
elasticClient, err := elastic.NewClient(elastic.SetURL(cfg.ElasticSearchUrl), elastic.SetSniff(false))
if err != nil {
log.Fatal("Error Creating Elastic Client: ", err)
}
log.Printf("Elastic Search Client Created")
elasticService := &service.ElasticService {
ElasticCLI: elasticClient,
}
chatHandler := &handler.Handler{
Upgrader: websocket.Upgrader{},
Service: elasticService,
}
fs := http.FileServer(http.Dir("../public"))
http.Handle("/", fs)
http.HandleFunc("/ws", chatHandler.HandleConnections)
http.HandleFunc("/sendMessage", chatHandler.SendViaPost)
go chatHandler.HandleMessages()
log.Println("http server started on " + os.Getenv("PORT"))
error := http.ListenAndServe(":" + os.Getenv("PORT"), nil)
if error != nil {
log.Fatal("ListenAndServe: ", error)
}
}