-
Notifications
You must be signed in to change notification settings - Fork 18
/
server.go
62 lines (52 loc) · 1.54 KB
/
server.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
package main
import (
"fmt"
"net"
"os"
_ "github.com/go-sql-driver/mysql"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
"github.com/raahii/golang-grpc-realworld-example/db"
"github.com/raahii/golang-grpc-realworld-example/handler"
pb "github.com/raahii/golang-grpc-realworld-example/proto"
"github.com/raahii/golang-grpc-realworld-example/store"
"github.com/rs/zerolog"
"google.golang.org/grpc"
)
const (
port = ":50051"
)
func main() {
w := zerolog.ConsoleWriter{Out: os.Stderr}
l := zerolog.New(w).With().Timestamp().Caller().Logger()
d, err := db.New()
if err != nil {
err = fmt.Errorf("failed to connect database: %w", err)
l.Fatal().Err(err).Msg("failed to connect the database")
}
l.Info().Str("name", d.Dialect().GetName()).
Str("database", d.Dialect().CurrentDatabase()).
Msg("succeeded to connect to the database")
err = db.AutoMigrate(d)
if err != nil {
l.Fatal().Err(err).Msg("failed to migrate database")
}
us := store.NewUserStore(d)
as := store.NewArticleStore(d)
h := handler.New(&l, us, as)
lis, err := net.Listen("tcp", port)
if err != nil {
l.Panic().Err(fmt.Errorf("failed to listen: %w", err))
}
s := grpc.NewServer(
grpc_middleware.WithUnaryServerChain(
grpc_recovery.UnaryServerInterceptor(),
),
)
pb.RegisterUsersServer(s, h)
pb.RegisterArticlesServer(s, h)
l.Info().Str("port", port).Msg("starting server")
if err := s.Serve(lis); err != nil {
l.Panic().Err(fmt.Errorf("failed to serve: %w", err))
}
}