-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
103 lines (93 loc) · 3.26 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
package main
import (
"context"
"crypto/tls"
"fmt"
"github.com/go-kit/log/level"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/nightsilvertech/foo/constant"
ep "github.com/nightsilvertech/foo/endpoint"
"github.com/nightsilvertech/foo/gvar"
pb "github.com/nightsilvertech/foo/protoc/api/v1"
"github.com/nightsilvertech/foo/repository"
"github.com/nightsilvertech/foo/service"
"github.com/nightsilvertech/foo/transport"
"github.com/nightsilvertech/utl/console"
"github.com/nightsilvertech/utl/preparation"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"log"
"net"
"net/http"
)
type Secure struct {
ServerCertPath string
ServerKeyPath string
ServerNameOverride string
Service pb.FooServiceServer
}
func (secure Secure) ServeGRPC() error {
level.Info(gvar.Logger).Log(console.LogInfo, "serving grpc server")
address := fmt.Sprintf("%s:%s", constant.Host, constant.GrpcPort)
serverCert, err := tls.LoadX509KeyPair(secure.ServerCertPath, secure.ServerKeyPath)
if err != nil {
return err
}
serverOpts := []grpc.ServerOption{grpc.Creds(credentials.NewServerTLSFromCert(&serverCert))}
listener, err := net.Listen("tcp", address)
if err != nil {
return err
}
grpcServer := grpc.NewServer(serverOpts...)
pb.RegisterFooServiceServer(grpcServer, secure.Service)
return grpcServer.Serve(listener)
}
func (secure Secure) ServeHTTP() error {
level.Info(gvar.Logger).Log(console.LogInfo, "serving http server")
httpAddress := fmt.Sprintf("%s:%s", constant.Host, constant.HttpPort)
grpcAddress := fmt.Sprintf("%s:%s", constant.Host, constant.GrpcPort)
clientCert, err := credentials.NewClientTLSFromFile(secure.ServerCertPath, secure.ServerNameOverride)
if err != nil {
return err
}
dialOptions := []grpc.DialOption{grpc.WithTransportCredentials(clientCert)}
mux := runtime.NewServeMux()
pb.RegisterFooServiceServer(grpc.NewServer(), secure.Service)
err = pb.RegisterFooServiceHandlerFromEndpoint(context.Background(), mux, grpcAddress, dialOptions)
if err != nil {
return err
}
return http.ListenAndServeTLS(httpAddress, secure.ServerCertPath, secure.ServerKeyPath, mux)
}
func Serve(service pb.FooServiceServer) {
secure := Secure{
ServerCertPath: "C:\\Users\\Asus\\Desktop\\tls\\foo\\server.crt",
ServerKeyPath: "C:\\Users\\Asus\\Desktop\\tls\\foo\\server.key",
ServerNameOverride: "0.0.0.0",
Service: service,
}
g := new(errgroup.Group)
g.Go(func() error { return secure.ServeGRPC() })
g.Go(func() error { return secure.ServeHTTP() })
log.Fatal(g.Wait())
}
func main() {
prepare := preparation.Data{
//LoggingFilePath: "C:\\Users\\Asus\\Desktop\\service.log",
TracerUrl: "http://localhost:9411/api/v2/spans",
CircuitBreakerTimeout: constant.CircuitBreakerTimout,
ServiceName: constant.ServiceName,
ZipkinEndpointPort: constant.ZipkinHostPort,
Debug: false,
FractionProbabilitySampler: 1,
}
prepare.CircuitBreaker()
gvar.Logger = prepare.Logger()
gvar.Tracer = prepare.Tracer()
repositories := repository.NewRepository()
services := service.NewService(*repositories)
endpoints := ep.NewFooEndpoint(services)
server := transport.NewFooServer(endpoints)
Serve(server)
}