Skip to content

Commit 4a74e60

Browse files
authored
✨ feat: support grpc server start print info (#137)
1 parent eef02c4 commit 4a74e60

File tree

7 files changed

+184
-87
lines changed

7 files changed

+184
-87
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ jobs:
2323
go install golang.org/x/tools/cmd/goimports
2424
2525
- name: golangci-lint
26-
uses: golangci/golangci-lint-action@v3
26+
uses: golangci/golangci-lint-action@v6
2727
with:
2828
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
2929
version: latest
30-
# args: "run -v ./..."
30+
args: "--timeout=5m"

core/server/default.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package server
22

3+
import (
4+
"fmt"
5+
"net"
6+
"strings"
7+
"sync"
8+
)
9+
310
/*
411
* @Author: lwnmengjing
512
* @Date: 2022/3/11 9:42
@@ -9,3 +16,54 @@ package server
916

1017
// Manage server
1118
var Manage = New()
19+
20+
var Mux sync.Mutex
21+
22+
func PrintRunningInfo(address, protocol string) {
23+
Mux.Lock()
24+
defer Mux.Unlock()
25+
var port string
26+
fmt.Println(" \033[90m╔═════════════════════════════════════════════════════════╗\033[0m")
27+
fmt.Printf(" \033[90m║\033[0m %s listening at: \033[90m║\033[0m", protocol)
28+
fmt.Println()
29+
arr := strings.Split(address, ":")
30+
if len(arr) <= 1 {
31+
port = "80"
32+
} else {
33+
port = arr[len(arr)-1]
34+
}
35+
hosts := []string{"localhost", "127.0.0.1"}
36+
if strings.Contains(address, "0.0.0.0") || !strings.Contains(address, ".") {
37+
interfaces, _ := net.Interfaces()
38+
for i := range interfaces {
39+
if interfaces[i].Flags&net.FlagUp == 0 {
40+
continue
41+
}
42+
addresses, _ := interfaces[i].Addrs()
43+
for _, addr := range addresses {
44+
ipNet, ok := addr.(*net.IPNet)
45+
if ok && !ipNet.IP.IsLoopback() && ipNet.IP.To4() != nil {
46+
hosts = append(hosts, ipNet.IP.String())
47+
}
48+
}
49+
}
50+
}
51+
prefix := " "
52+
for i := range hosts {
53+
if i == 1 {
54+
prefix = "\033[32mready\033[0m - "
55+
}
56+
s := fmt.Sprintf("%s\033[90m║ >\033[0m grpc://%s:%s", prefix, hosts[i], port)
57+
fmt.Println(s + strings.Repeat(
58+
" ",
59+
51-len(fmt.Sprintf("%s://%s:%s", protocol, hosts[i], port)),
60+
) + "\033[90m║\033[0m")
61+
prefix = " "
62+
}
63+
fmt.Println(" \033[90m║\033[0m" +
64+
" \033[90m║\033[0m")
65+
fmt.Print(" \033[90m║\033[0m \033[1;97mNow you can reqeust the above addresses↑\033[0m")
66+
fmt.Print(" ")
67+
fmt.Println("\033[90m║\033[0m")
68+
fmt.Println(" \033[90m╚═════════════════════════════════════════════════════════╝\033[0m")
69+
}

core/server/grpc/options.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type Options struct {
5555
id string
5656
domain string
5757
addr string
58+
startedHook func()
5859
certFile string
5960
keyFile string
6061
tls *tls.Config
@@ -98,6 +99,13 @@ func WithAddr(s string) Option {
9899
}
99100
}
100101

102+
// WithStartedHook 设置启动回调函数
103+
func WithStartedHook(f func()) Option {
104+
return func(o *Options) {
105+
o.startedHook = f
106+
}
107+
}
108+
101109
// WithCert 设置cert
102110
func WithCert(s string) Option {
103111
return func(o *Options) {

core/server/grpc/server.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ import (
1111
"context"
1212
"errors"
1313
"fmt"
14-
"log"
1514
"log/slog"
1615
"net"
1716
"os"
1817
"sync"
1918

19+
"github.com/mss-boot-io/mss-boot/core/server"
20+
2021
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
2122

2223
"github.com/prometheus/client_golang/prometheus"
@@ -122,21 +123,28 @@ func (e *Server) Start(ctx context.Context) error {
122123
if err != nil {
123124
return fmt.Errorf("gRPC Server listening on %s failed: %w", e.options.addr, err)
124125
}
125-
log.Printf("gRPC Server listening on %s\n", ts.Addr().String())
126+
//log.Printf("gRPC Server listening on %s\n", ts.Addr().String())
127+
e.started = true
126128

127129
go func() {
128130
if err = e.srv.Serve(ts); err != nil {
129131
slog.ErrorContext(ctx, "gRPC Server start error", slog.Any("err", err))
130132
}
133+
<-ctx.Done()
134+
err = e.Shutdown(ctx)
135+
if err != nil {
136+
slog.ErrorContext(ctx, "gRPC Server shutdown error", slog.Any("err", err))
137+
}
131138
}()
132-
e.started = true
133-
<-ctx.Done()
134-
return e.Shutdown(ctx)
139+
if e.options.startedHook != nil {
140+
e.options.startedHook()
141+
}
142+
server.PrintRunningInfo(e.options.addr, "grpc")
143+
return nil
135144
}
136145

137146
// Shutdown shutdown
138147
func (e *Server) Shutdown(ctx context.Context) error {
139-
<-ctx.Done()
140148
slog.InfoContext(ctx, "gRPC Server will be shutdown gracefully")
141149
e.srv.GracefulStop()
142150
return nil

core/server/listener/server.go

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ package listener
99

1010
import (
1111
"context"
12-
"fmt"
1312
"log/slog"
1413
"net"
1514
"net/http"
1615
"net/http/pprof"
17-
"strings"
1816

1917
ginPprof "github.com/gin-contrib/pprof"
2018
"github.com/gin-gonic/gin"
@@ -129,55 +127,11 @@ func (e *Server) Start(ctx context.Context) error {
129127
if e.options.startedHook != nil {
130128
e.options.startedHook()
131129
}
132-
e.PrintRunningInfo()
130+
server.PrintRunningInfo(e.options.addr, "http")
133131
return nil
134132
}
135133

136134
// Shutdown 停止
137135
func (e *Server) Shutdown(ctx context.Context) error {
138136
return e.srv.Shutdown(ctx)
139137
}
140-
141-
func (e *Server) PrintRunningInfo() {
142-
var port string
143-
//todo 优雅输出
144-
fmt.Println(" \033[90m╔════════════════════════════════════════════════════╗\033[0m")
145-
fmt.Println(" \033[90m║\033[0m App listening at: \033[90m║\033[0m")
146-
arr := strings.Split(e.options.addr, ":")
147-
if len(arr) <= 1 {
148-
port = "80"
149-
} else {
150-
port = arr[len(arr)-1]
151-
}
152-
hosts := []string{"localhost", "127.0.0.1"}
153-
if strings.Contains(e.options.addr, "0.0.0.0") || !strings.Contains(e.options.addr, ".") {
154-
interfaces, _ := net.Interfaces()
155-
for i := range interfaces {
156-
if interfaces[i].Flags&net.FlagUp == 0 {
157-
continue
158-
}
159-
addresses, _ := interfaces[i].Addrs()
160-
for _, addr := range addresses {
161-
ipNet, ok := addr.(*net.IPNet)
162-
if ok && !ipNet.IP.IsLoopback() && ipNet.IP.To4() != nil {
163-
hosts = append(hosts, ipNet.IP.String())
164-
}
165-
}
166-
}
167-
}
168-
prefix := " "
169-
for i := range hosts {
170-
if i == 1 {
171-
prefix = "\033[32mready\033[0m - "
172-
}
173-
s := fmt.Sprintf("%s\033[90m║ >\033[0m http://%s:%s", prefix, hosts[i], port)
174-
fmt.Println(s + strings.Repeat(
175-
" ",
176-
46-len(fmt.Sprintf("http://%s:%s", hosts[i], port)),
177-
) + "\033[90m║\033[0m")
178-
prefix = " "
179-
}
180-
fmt.Println(" \033[90m║\033[0m \033[90m║\033[0m")
181-
fmt.Println(" \033[90m║\033[0m \033[1;97mNow you can open browser with the above addresses↑\033[0m \033[90m║\033[0m")
182-
fmt.Println(" \033[90m╚════════════════════════════════════════════════════╝\033[0m")
183-
}

go.mod

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ require (
99
github.com/aws/aws-sdk-go-v2/config v1.27.27
1010
github.com/aws/aws-sdk-go-v2/credentials v1.17.27
1111
github.com/aws/aws-sdk-go-v2/service/appconfigdata v1.16.3
12-
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.34.3
13-
github.com/aws/aws-sdk-go-v2/service/s3 v1.58.2
12+
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.34.4
13+
github.com/aws/aws-sdk-go-v2/service/s3 v1.58.3
1414
github.com/casbin/casbin/v2 v2.98.0
1515
github.com/casbin/gorm-adapter/v3 v3.26.0
1616
github.com/casbin/mongodb-adapter/v3 v3.7.0
@@ -21,7 +21,7 @@ require (
2121
github.com/gogo/protobuf v1.3.2
2222
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
2323
github.com/google/uuid v1.6.0
24-
github.com/grafana/loki/v3 v3.1.0
24+
github.com/grafana/loki/v3 v3.1.1
2525
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1
2626
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0
2727
github.com/hashicorp/consul/api v1.29.2
@@ -32,10 +32,10 @@ require (
3232
github.com/sanity-io/litter v1.5.5
3333
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
3434
github.com/smartystreets/goconvey v1.8.1
35-
github.com/spf13/cast v1.6.0
35+
github.com/spf13/cast v1.7.0
3636
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.53.0
3737
go.opentelemetry.io/otel/trace v1.28.0
38-
golang.org/x/time v0.5.0
38+
golang.org/x/time v0.6.0
3939
google.golang.org/grpc v1.65.0
4040
google.golang.org/protobuf v1.34.2
4141
gorm.io/driver/mysql v1.5.7
@@ -62,8 +62,8 @@ require (
6262
github.com/aws/aws-sdk-go v1.54.20 // indirect
6363
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4 // indirect
6464
github.com/bboreham/go-loser v0.0.0-20230920113527-fcc2c21820a3 // indirect
65-
github.com/bytedance/sonic v1.11.9 // indirect
66-
github.com/bytedance/sonic/loader v0.1.1 // indirect
65+
github.com/bytedance/sonic v1.12.1 // indirect
66+
github.com/bytedance/sonic/loader v0.2.0 // indirect
6767
github.com/c2h5oh/datasize v0.0.0-20231215233829-aa82cc1e6500 // indirect
6868
github.com/casbin/govaluate v1.2.0 // indirect
6969
github.com/cespare/xxhash v1.1.0 // indirect
@@ -80,8 +80,8 @@ require (
8080
github.com/facette/natsort v0.0.0-20181210072756-2cd4dd1e2dcb // indirect
8181
github.com/fatih/color v1.17.0 // indirect
8282
github.com/felixge/httpsnoop v1.0.4 // indirect
83-
github.com/gabriel-vasile/mimetype v1.4.4 // indirect
84-
github.com/go-jose/go-jose/v4 v4.0.3 // indirect
83+
github.com/gabriel-vasile/mimetype v1.4.5 // indirect
84+
github.com/go-jose/go-jose/v4 v4.0.4 // indirect
8585
github.com/go-kit/log v0.2.1 // indirect
8686
github.com/go-logfmt/logfmt v0.6.0 // indirect
8787
github.com/go-logr/logr v1.4.2 // indirect
@@ -99,10 +99,10 @@ require (
9999
github.com/google/go-cmp v0.6.0 // indirect
100100
github.com/google/gofuzz v1.2.0 // indirect
101101
github.com/gorilla/mux v1.8.1 // indirect
102-
github.com/grafana/dskit v0.0.0-20240719153732-6e8a03e781de // indirect
103-
github.com/grafana/gomemcache v0.0.0-20240229205252-cd6a66d6fb56 // indirect
102+
github.com/grafana/dskit v0.0.0-20240807162847-859371e7a400 // indirect
103+
github.com/grafana/gomemcache v0.0.0-20240805133030-fdaf6a95408e // indirect
104104
github.com/grafana/jsonparser v0.0.0-20240425183733-ea80629e1a32 // indirect
105-
github.com/grafana/loki/pkg/push v0.0.0-20240722125035-ce02cc254abc // indirect
105+
github.com/grafana/loki/pkg/push v0.0.0-20240808153141-66d71383c7b9 // indirect
106106
github.com/grafana/pyroscope-go/godeltaprof v0.1.7 // indirect
107107
github.com/grafana/regexp v0.0.0-20240518133315-a468a5bfb3bc // indirect
108108
github.com/hashicorp/errwrap v1.1.0 // indirect
@@ -138,7 +138,7 @@ require (
138138
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
139139
github.com/ncruces/go-strftime v0.1.9 // indirect
140140
github.com/oklog/ulid v1.3.1 // indirect
141-
github.com/opentracing-contrib/go-grpc v0.0.0-20210225150812-73cb765af46e // indirect
141+
github.com/opentracing-contrib/go-grpc v0.0.0-20240724223109-9dec25a38fa8 // indirect
142142
github.com/opentracing-contrib/go-stdlib v1.0.0 // indirect
143143
github.com/opentracing/opentracing-go v1.2.0 // indirect
144144
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
@@ -170,17 +170,17 @@ require (
170170
go.uber.org/multierr v1.11.0 // indirect
171171
go.uber.org/zap v1.27.0 // indirect
172172
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect
173-
golang.org/x/arch v0.8.0 // indirect
174-
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
175-
golang.org/x/mod v0.19.0 // indirect
176-
golang.org/x/term v0.22.0 // indirect
177-
golang.org/x/tools v0.23.0 // indirect
178-
google.golang.org/genproto/googleapis/api v0.0.0-20240722135656-d784300faade // indirect
179-
google.golang.org/genproto/googleapis/rpc v0.0.0-20240722135656-d784300faade // indirect
173+
golang.org/x/arch v0.9.0 // indirect
174+
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa // indirect
175+
golang.org/x/mod v0.20.0 // indirect
176+
golang.org/x/term v0.23.0 // indirect
177+
golang.org/x/tools v0.24.0 // indirect
178+
google.golang.org/genproto/googleapis/api v0.0.0-20240808171019-573a1156607a // indirect
179+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240808171019-573a1156607a // indirect
180180
gopkg.in/inf.v0 v0.9.1 // indirect
181181
gopkg.in/yaml.v2 v2.4.0 // indirect
182182
k8s.io/klog/v2 v2.130.1 // indirect
183-
k8s.io/kube-openapi v0.0.0-20240709000822-3c01b740850f // indirect
183+
k8s.io/kube-openapi v0.0.0-20240808142205-8e686545bdb8 // indirect
184184
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
185185
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
186186
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
@@ -241,19 +241,19 @@ require (
241241
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
242242
github.com/xdg-go/scram v1.1.2 // indirect
243243
github.com/xdg-go/stringprep v1.0.4 // indirect
244-
github.com/youmark/pkcs8 v0.0.0-20240424034433-3c2c7870ae76 // indirect
245-
go.mongodb.org/mongo-driver v1.16.0
246-
golang.org/x/crypto v0.25.0
247-
golang.org/x/image v0.18.0 // indirect
248-
golang.org/x/net v0.27.0 // indirect
249-
golang.org/x/oauth2 v0.21.0
250-
golang.org/x/sync v0.7.0 // indirect
251-
golang.org/x/sys v0.22.0 // indirect
252-
golang.org/x/text v0.16.0 // indirect
244+
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
245+
go.mongodb.org/mongo-driver v1.16.1
246+
golang.org/x/crypto v0.26.0
247+
golang.org/x/image v0.19.0 // indirect
248+
golang.org/x/net v0.28.0 // indirect
249+
golang.org/x/oauth2 v0.22.0
250+
golang.org/x/sync v0.8.0 // indirect
251+
golang.org/x/sys v0.24.0 // indirect
252+
golang.org/x/text v0.17.0 // indirect
253253
gopkg.in/yaml.v3 v3.0.1
254254
gorm.io/driver/sqlserver v1.5.3 // indirect
255-
modernc.org/libc v1.55.3 // indirect
255+
modernc.org/libc v1.57.0 // indirect
256256
modernc.org/mathutil v1.6.0 // indirect
257257
modernc.org/memory v1.8.0 // indirect
258-
modernc.org/sqlite v1.31.1 // indirect
258+
modernc.org/sqlite v1.32.0 // indirect
259259
)

0 commit comments

Comments
 (0)