/
server.go
140 lines (129 loc) · 3.65 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
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
135
136
137
138
139
140
package command
import (
"net"
"net/http"
_ "net/http/pprof"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.octolab.org/errors"
"go.octolab.org/sync"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
v1 "go.octolab.org/template/service/api/rpc/v1"
"go.octolab.org/template/service/api/rpc/v1/v1connect"
"go.octolab.org/template/service/internal/config"
"go.octolab.org/template/service/internal/server"
)
// NewServer returns the new server command.
func NewServer(cnf *config.Service) *cobra.Command {
var path string
v := viper.New()
v.AddConfigPath(".")
command := cobra.Command{
Use: "server",
Short: "server of the service",
Long: "Execute remote client commands.",
Args: cobra.NoArgs,
SilenceErrors: false,
SilenceUsage: true,
PersistentPreRunE: func(*cobra.Command, []string) error {
v.SetConfigFile(path)
if err := v.ReadInConfig(); err != nil {
return err
}
return v.Unmarshal(cnf)
},
}
flags := command.PersistentFlags()
flags.StringVarP(&path, "config", "c", "config.toml", "path to config file")
command.AddCommand(
Run(&cnf.Server),
)
return &command
}
func Run(cnf *config.Server) *cobra.Command {
command := cobra.Command{
Use: "run",
Short: "run the server",
Long: "Start listening required protocols.",
RunE: func(cmd *cobra.Command, args []string) error {
if cnf.Twirp.IsEnabled() {
twirp := server.Twirp()
mux := http.NewServeMux()
mux.Handle(twirp.PathPrefix(), twirp)
go func() {
cmd.Println("twirp server starts listening", cnf.Twirp.BaseURL())
cmd.Println("twirp server status:", http.ListenAndServe(
cnf.Twirp.Address,
mux,
))
}()
}
if cnf.GRPC.IsEnabled() {
listener, err := net.Listen("tcp", cnf.GRPC.Address)
if err != nil {
return err
}
srv := grpc.NewServer()
v1.RegisterGreeterServiceServer(srv, new(server.GRPC))
go func() {
cmd.Println("grpc server starts listening", "tcp://"+cnf.GRPC.Address)
cmd.Println("grpc server status:", srv.Serve(listener))
}()
if cnf.Gateway.IsEnabled() {
mux := runtime.NewServeMux()
if err := v1.RegisterGreeterServiceHandlerFromEndpoint(
cmd.Context(),
mux,
cnf.GRPC.Address,
[]grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
},
); err != nil {
return err
}
go func() {
cmd.Println("gateway starts listening", cnf.Gateway.BaseURL())
cmd.Println("gateway status:", http.ListenAndServe(
cnf.Gateway.Address,
mux,
))
}()
}
}
if cnf.Profile.IsEnabled() {
mux := http.DefaultServeMux
go func() {
cmd.Println("profiler starts listening", cnf.Profile.BaseURL())
cmd.Println("profiler status:", http.ListenAndServe(
cnf.Profile.Address,
mux,
))
}()
}
mux := http.NewServeMux()
path, handler := v1connect.NewGreeterServiceHandler(new(server.Connect))
mux.Handle(path, handler)
srv := &http.Server{
Addr: cnf.Connect.Address,
Handler: h2c.NewHandler(mux, new(http2.Server)),
}
go func() {
cmd.Println("rpc server starts listening", cnf.Connect.BaseURL())
cmd.Println("rpc server status:", srv.ListenAndServe())
}()
err := sync.Termination().Wait(cmd.Context())
if errors.Is(err, sync.ErrSignalTrapped) {
cmd.Println("shutting down rpc server:", srv.Shutdown(cmd.Context()))
return nil
}
return err
},
}
flags := command.Flags()
flags.StringVar(&cnf.Connect.Address, "host", "", "remote rpc host")
return &command
}