-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
65 lines (52 loc) · 1.62 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
package main
import (
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
"github.com/gkiryaziev/go-ws-server/conf"
hdlLog "github.com/gkiryaziev/go-ws-server/handlers/log_handler"
hdlSubscriber "github.com/gkiryaziev/go-ws-server/handlers/subscriber_handler"
hdlTopic "github.com/gkiryaziev/go-ws-server/handlers/topic_handler"
hdlWs "github.com/gkiryaziev/go-ws-server/handlers/ws_handler"
)
// checkError check errors
func checkError(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
mx := mux.NewRouter()
// config
config, err := conf.NewConfig("config.yaml").Load()
checkError(err)
// http server address and port
hostBind := fmt.Sprintf("%s:%s", config.Server.Host, config.Server.Port)
// open connection to database
db, err := sqlx.Connect("sqlite3", "base.db3")
checkError(err)
db.SetMaxIdleConns(100)
defer db.Close()
// websocket hub
hub := hdlWs.NewHub(db)
go hub.Run()
// controllers
wsCtrl := hdlWs.NewWsController(hub)
logCtrl := hdlLog.NewLogController(db)
topicCtrl := hdlTopic.NewTopicController(db)
subscriberCtrl := hdlSubscriber.NewSubscriberController(db)
// user handler
mx.HandleFunc("/ws", wsCtrl.WsHandler)
mx.HandleFunc("/api/v1/logs", logCtrl.GetLogs).Methods("GET")
mx.HandleFunc("/api/v1/topics", topicCtrl.GetTopics).Methods("GET")
mx.HandleFunc("/api/v1/subscribers", subscriberCtrl.GetSubscribers).Methods("GET")
// static
mx.PathPrefix("/").Handler(http.FileServer(http.Dir("public")))
// start http server
fmt.Println("Listening on", hostBind)
err = http.ListenAndServe(hostBind, mx)
checkError(err)
}