-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathserver.go
67 lines (55 loc) · 1.37 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
package main
import (
"context"
"fmt"
"log"
"net"
"os"
"time"
"github.com/jacky-htg/erp-pkg/db/postgres"
"github.com/jacky-htg/erp-pkg/db/redis"
"github.com/jacky-htg/user-service/internal/config"
"github.com/jacky-htg/user-service/internal/route"
_ "github.com/lib/pq"
"google.golang.org/grpc"
)
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, "ERROR : ", 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
}
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
}
// listen tcp port
lis, err := net.Listen("tcp", ":"+port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
return
}
grpcServer := grpc.NewServer()
// routing grpc services
route.GrpcRoute(grpcServer, db, log, cache)
if err := grpcServer.Serve(lis); err != nil {
log.Fatalf("failed to serve: %s", err)
return
}
fmt.Println("serve grpc on port: " + port)
}