-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (65 loc) · 1.86 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
package main
import (
"net/http"
"os"
"time"
mgo "gopkg.in/mgo.v2"
"github.com/alecholmez/messenger/config"
"github.com/alecholmez/messenger/sockets"
"github.com/alecholmez/messenger/users"
"github.com/deciphernow/gm-fabric-go/dbutil"
"github.com/deciphernow/gm-fabric-go/middleware"
"github.com/gorilla/mux"
nats "github.com/nats-io/go-nats"
"github.com/rs/zerolog"
"github.com/rs/zerolog/hlog"
)
func main() {
log := zerolog.New(os.Stderr).With().Timestamp().Logger().
Output(zerolog.ConsoleWriter{Out: os.Stderr})
// Read in config
sc := config.NewConfig()
nc, err := nats.Connect(sc.NatsIP)
if err != nil {
log.Error().Err(err).Msg("Failed to connect to NATS")
return
}
c, err := nats.NewEncodedConn(nc, nats.JSON_ENCODER)
if err != nil {
log.Error().Err(err).Msg("Failed to create encoded NATS conn")
}
defer c.Close()
sess, err := mgo.Dial(sc.MongoIP)
if err != nil {
log.Error().Err(err).Msg("Failed to connect to Mongo")
return
}
defer sess.Close()
stack := middleware.Chain(
middleware.MiddlewareFunc(hlog.NewHandler(log)),
middleware.MiddlewareFunc(hlog.AccessHandler(func(r *http.Request, status int, size int, duration time.Duration) {
hlog.FromRequest(r).Info().
Str("method", r.Method).
Str("path", r.URL.String()).
Int("status", status).
Int("size", size).
Dur("duration", duration).
Msg("Access")
})),
middleware.MiddlewareFunc(hlog.UserAgentHandler("user_agent")),
config.WithNats(c),
dbutil.WithMongo(sess),
)
mux := mux.NewRouter()
mux.HandleFunc("/subscribe", sockets.Subscribe)
mux.HandleFunc("/send", sockets.Send)
mux.HandleFunc("/signup", users.Signup).Methods("POST")
mux.HandleFunc("/login", users.Login).Methods("POST")
s := http.Server{
Addr: ":8080",
Handler: stack.Wrap(mux),
}
log.Info().Msg("Messenger listening on :8080")
// Block on this
s.ListenAndServe()
}