-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
78 lines (63 loc) · 1.72 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"log"
"net"
"os"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
_ "github.com/lib/pq"
"google.golang.org/grpc"
"ledger-service/internal/config"
"ledger-service/internal/middleware"
"ledger-service/internal/pkg/db/postgres"
"ledger-service/internal/route"
)
const defaultPort = "8000"
func main() {
// lookup and setup env
if _, ok := os.LookupEnv("PORT"); !ok {
config.Setup(".env")
}
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}
// init log
log := log.New(os.Stdout, "LMS : ", log.LstdFlags|log.Lmicroseconds|log.Lshortfile)
// create postgres database connection
db, err := postgres.Open()
if err != nil {
log.Fatalf("connecting to db: %v", err)
return
}
log.Print("connecting to postgresql database")
defer db.Close()
// create redis cache connection
/*cache, err := redis.NewCache(context.Background(), os.Getenv("REDIS_ADDRESS"), os.Getenv("REDIS_PASSWORD"), 24*time.Hour)
if err != nil {
log.Fatalf("cannot create redis connection: %v", err)
return
}
log.Print("connecting to redis cache") */
// listen tcp port
lis, err := net.Listen("tcp", ":"+port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
authInterceptor := middleware.Context{}
serverOptions := []grpc.ServerOption{
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
authInterceptor.Unary(),
)),
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
authInterceptor.Stream(),
)),
}
grpcServer := grpc.NewServer(serverOptions...)
// routing grpc services
route.GrpcRoute(grpcServer, db, log)
if err := grpcServer.Serve(lis); err != nil {
log.Fatalf("failed to serve: %s", err)
return
}
log.Print("serve grpc on port: " + port)
}