-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
134 lines (115 loc) · 3.38 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
package main
import (
"io"
"log"
"net"
stdHTTP "net/http"
"os"
"os/signal"
"syscall"
stdNats "github.com/nats-io/nats.go"
"github.com/pkg/errors"
"github.com/ulexxander/transport-madness/services"
"github.com/ulexxander/transport-madness/transport/graphql"
"github.com/ulexxander/transport-madness/transport/grpc"
"github.com/ulexxander/transport-madness/transport/http"
"github.com/ulexxander/transport-madness/transport/nats"
"github.com/ulexxander/transport-madness/transport/websocket"
stdGRPC "google.golang.org/grpc"
)
func main() {
log := log.New(os.Stdout, "", log.LstdFlags)
if err := run(log); err != nil {
log.Fatalln("service died", err)
}
}
func run(log *log.Logger) error {
log.Println("starting service")
httpAddr := ":4007"
httpMux := stdHTTP.NewServeMux()
httpServ := stdHTTP.Server{
Addr: httpAddr,
Handler: httpMux,
}
websocketPublisher := websocket.Publisher{
Mux: httpMux,
Log: log,
}
websocketPublisher.Setup()
natsURL := "nats://localhost:4222"
log.Println("connecting to nats server", natsURL)
natsConn, err := stdNats.Connect(natsURL)
if err != nil {
return errors.Wrap(err, "could not connect to nats")
}
natsPublisher := nats.Publisher{
Conn: natsConn,
Log: log,
}
usersService := services.NewUsersService(&websocketPublisher)
messagesService := services.NewMessagesService(usersService, &natsPublisher)
httpResponder := http.Responder{
Mux: httpMux,
UsersService: usersService,
MessagesService: messagesService,
Log: log,
}
httpResponder.Setup()
graphqlSchemaFilepath := "./transport/graphql/schema.graphql"
log.Println("opening graphql schema file", graphqlSchemaFilepath)
graphqlSchemaFile, err := os.Open(graphqlSchemaFilepath)
if err != nil {
return errors.Wrap(err, "could not open graphql schema file")
}
graphqlSchemaBytes, err := io.ReadAll(graphqlSchemaFile)
if err != nil {
return errors.Wrap(err, "could not read graphql schema file")
}
graphqlQueryResolver := graphql.Query{
UsersService: usersService,
MessagesService: messagesService,
}
graphqlResponder := graphql.Responder{
Mux: httpMux,
Schema: string(graphqlSchemaBytes),
QueryResolver: &graphqlQueryResolver,
Log: log,
}
graphqlResponder.Setup()
natsResponder := nats.Responder{
Conn: natsConn,
UsersService: usersService,
MessagesService: messagesService,
Log: log,
}
natsResponder.Setup()
grpcAddr := ":4008"
log.Println("creating tcp listener for grpc server on addr", grpcAddr)
grpcListener, err := net.Listen("tcp", grpcAddr)
if err != nil {
return errors.Wrap(err, "could not create listener for grpc server")
}
grpcServer := stdGRPC.NewServer()
go func() {
if err := grpcServer.Serve(grpcListener); err != nil {
log.Println("failed to serve grpc", err)
}
}()
grpcResponder := grpc.Responder{
Server: grpcServer,
UsersServer: &grpc.UsersServer{UsersService: usersService},
MessagesServer: &grpc.MessagesServer{MessagesService: messagesService},
}
grpcResponder.Setup()
go func() {
log.Println("starting http server on", httpAddr)
if err := httpServ.ListenAndServe(); err != nil {
log.Println("failed to listen and serve http", err)
}
}()
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
<-sigChan
log.Println("received interrupt from the os")
return nil
}