Skip to content

Commit

Permalink
✨ feat: support grpc server start print info (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwnmengjing authored Aug 9, 2024
1 parent eef02c4 commit 4a74e60
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 87 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ jobs:
go install golang.org/x/tools/cmd/goimports
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v6
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: latest
# args: "run -v ./..."
args: "--timeout=5m"
58 changes: 58 additions & 0 deletions core/server/default.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package server

import (
"fmt"
"net"
"strings"
"sync"
)

/*
* @Author: lwnmengjing
* @Date: 2022/3/11 9:42
Expand All @@ -9,3 +16,54 @@ package server

// Manage server
var Manage = New()

var Mux sync.Mutex

func PrintRunningInfo(address, protocol string) {
Mux.Lock()
defer Mux.Unlock()
var port string
fmt.Println(" \033[90m╔═════════════════════════════════════════════════════════╗\033[0m")
fmt.Printf(" \033[90m║\033[0m %s listening at: \033[90m║\033[0m", protocol)
fmt.Println()
arr := strings.Split(address, ":")
if len(arr) <= 1 {
port = "80"
} else {
port = arr[len(arr)-1]
}
hosts := []string{"localhost", "127.0.0.1"}
if strings.Contains(address, "0.0.0.0") || !strings.Contains(address, ".") {
interfaces, _ := net.Interfaces()
for i := range interfaces {
if interfaces[i].Flags&net.FlagUp == 0 {
continue
}
addresses, _ := interfaces[i].Addrs()
for _, addr := range addresses {
ipNet, ok := addr.(*net.IPNet)
if ok && !ipNet.IP.IsLoopback() && ipNet.IP.To4() != nil {
hosts = append(hosts, ipNet.IP.String())
}
}
}
}
prefix := " "
for i := range hosts {
if i == 1 {
prefix = "\033[32mready\033[0m - "
}
s := fmt.Sprintf("%s\033[90m║ >\033[0m grpc://%s:%s", prefix, hosts[i], port)
fmt.Println(s + strings.Repeat(
" ",
51-len(fmt.Sprintf("%s://%s:%s", protocol, hosts[i], port)),
) + "\033[90m║\033[0m")
prefix = " "
}
fmt.Println(" \033[90m║\033[0m" +
" \033[90m║\033[0m")
fmt.Print(" \033[90m║\033[0m \033[1;97mNow you can reqeust the above addresses↑\033[0m")
fmt.Print(" ")
fmt.Println("\033[90m║\033[0m")
fmt.Println(" \033[90m╚═════════════════════════════════════════════════════════╝\033[0m")
}
8 changes: 8 additions & 0 deletions core/server/grpc/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type Options struct {
id string
domain string
addr string
startedHook func()
certFile string
keyFile string
tls *tls.Config
Expand Down Expand Up @@ -98,6 +99,13 @@ func WithAddr(s string) Option {
}
}

// WithStartedHook 设置启动回调函数
func WithStartedHook(f func()) Option {
return func(o *Options) {
o.startedHook = f
}
}

// WithCert 设置cert
func WithCert(s string) Option {
return func(o *Options) {
Expand Down
20 changes: 14 additions & 6 deletions core/server/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import (
"context"
"errors"
"fmt"
"log"
"log/slog"
"net"
"os"
"sync"

"github.com/mss-boot-io/mss-boot/core/server"

"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"

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

go func() {
if err = e.srv.Serve(ts); err != nil {
slog.ErrorContext(ctx, "gRPC Server start error", slog.Any("err", err))
}
<-ctx.Done()
err = e.Shutdown(ctx)
if err != nil {
slog.ErrorContext(ctx, "gRPC Server shutdown error", slog.Any("err", err))
}
}()
e.started = true
<-ctx.Done()
return e.Shutdown(ctx)
if e.options.startedHook != nil {
e.options.startedHook()
}
server.PrintRunningInfo(e.options.addr, "grpc")
return nil
}

// Shutdown shutdown
func (e *Server) Shutdown(ctx context.Context) error {
<-ctx.Done()
slog.InfoContext(ctx, "gRPC Server will be shutdown gracefully")
e.srv.GracefulStop()
return nil
Expand Down
48 changes: 1 addition & 47 deletions core/server/listener/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ package listener

import (
"context"
"fmt"
"log/slog"
"net"
"net/http"
"net/http/pprof"
"strings"

ginPprof "github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -129,55 +127,11 @@ func (e *Server) Start(ctx context.Context) error {
if e.options.startedHook != nil {
e.options.startedHook()
}
e.PrintRunningInfo()
server.PrintRunningInfo(e.options.addr, "http")
return nil
}

// Shutdown 停止
func (e *Server) Shutdown(ctx context.Context) error {
return e.srv.Shutdown(ctx)
}

func (e *Server) PrintRunningInfo() {
var port string
//todo 优雅输出
fmt.Println(" \033[90m╔════════════════════════════════════════════════════╗\033[0m")
fmt.Println(" \033[90m║\033[0m App listening at: \033[90m║\033[0m")
arr := strings.Split(e.options.addr, ":")
if len(arr) <= 1 {
port = "80"
} else {
port = arr[len(arr)-1]
}
hosts := []string{"localhost", "127.0.0.1"}
if strings.Contains(e.options.addr, "0.0.0.0") || !strings.Contains(e.options.addr, ".") {
interfaces, _ := net.Interfaces()
for i := range interfaces {
if interfaces[i].Flags&net.FlagUp == 0 {
continue
}
addresses, _ := interfaces[i].Addrs()
for _, addr := range addresses {
ipNet, ok := addr.(*net.IPNet)
if ok && !ipNet.IP.IsLoopback() && ipNet.IP.To4() != nil {
hosts = append(hosts, ipNet.IP.String())
}
}
}
}
prefix := " "
for i := range hosts {
if i == 1 {
prefix = "\033[32mready\033[0m - "
}
s := fmt.Sprintf("%s\033[90m║ >\033[0m http://%s:%s", prefix, hosts[i], port)
fmt.Println(s + strings.Repeat(
" ",
46-len(fmt.Sprintf("http://%s:%s", hosts[i], port)),
) + "\033[90m║\033[0m")
prefix = " "
}
fmt.Println(" \033[90m║\033[0m \033[90m║\033[0m")
fmt.Println(" \033[90m║\033[0m \033[1;97mNow you can open browser with the above addresses↑\033[0m \033[90m║\033[0m")
fmt.Println(" \033[90m╚════════════════════════════════════════════════════╝\033[0m")
}
64 changes: 32 additions & 32 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ require (
github.com/aws/aws-sdk-go-v2/config v1.27.27
github.com/aws/aws-sdk-go-v2/credentials v1.17.27
github.com/aws/aws-sdk-go-v2/service/appconfigdata v1.16.3
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.34.3
github.com/aws/aws-sdk-go-v2/service/s3 v1.58.2
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.34.4
github.com/aws/aws-sdk-go-v2/service/s3 v1.58.3
github.com/casbin/casbin/v2 v2.98.0
github.com/casbin/gorm-adapter/v3 v3.26.0
github.com/casbin/mongodb-adapter/v3 v3.7.0
Expand All @@ -21,7 +21,7 @@ require (
github.com/gogo/protobuf v1.3.2
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/google/uuid v1.6.0
github.com/grafana/loki/v3 v3.1.0
github.com/grafana/loki/v3 v3.1.1
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0
github.com/hashicorp/consul/api v1.29.2
Expand All @@ -32,10 +32,10 @@ require (
github.com/sanity-io/litter v1.5.5
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
github.com/smartystreets/goconvey v1.8.1
github.com/spf13/cast v1.6.0
github.com/spf13/cast v1.7.0
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.53.0
go.opentelemetry.io/otel/trace v1.28.0
golang.org/x/time v0.5.0
golang.org/x/time v0.6.0
google.golang.org/grpc v1.65.0
google.golang.org/protobuf v1.34.2
gorm.io/driver/mysql v1.5.7
Expand All @@ -62,8 +62,8 @@ require (
github.com/aws/aws-sdk-go v1.54.20 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4 // indirect
github.com/bboreham/go-loser v0.0.0-20230920113527-fcc2c21820a3 // indirect
github.com/bytedance/sonic v1.11.9 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/bytedance/sonic v1.12.1 // indirect
github.com/bytedance/sonic/loader v0.2.0 // indirect
github.com/c2h5oh/datasize v0.0.0-20231215233829-aa82cc1e6500 // indirect
github.com/casbin/govaluate v1.2.0 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
Expand All @@ -80,8 +80,8 @@ require (
github.com/facette/natsort v0.0.0-20181210072756-2cd4dd1e2dcb // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/gabriel-vasile/mimetype v1.4.4 // indirect
github.com/go-jose/go-jose/v4 v4.0.3 // indirect
github.com/gabriel-vasile/mimetype v1.4.5 // indirect
github.com/go-jose/go-jose/v4 v4.0.4 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
Expand All @@ -99,10 +99,10 @@ require (
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/grafana/dskit v0.0.0-20240719153732-6e8a03e781de // indirect
github.com/grafana/gomemcache v0.0.0-20240229205252-cd6a66d6fb56 // indirect
github.com/grafana/dskit v0.0.0-20240807162847-859371e7a400 // indirect
github.com/grafana/gomemcache v0.0.0-20240805133030-fdaf6a95408e // indirect
github.com/grafana/jsonparser v0.0.0-20240425183733-ea80629e1a32 // indirect
github.com/grafana/loki/pkg/push v0.0.0-20240722125035-ce02cc254abc // indirect
github.com/grafana/loki/pkg/push v0.0.0-20240808153141-66d71383c7b9 // indirect
github.com/grafana/pyroscope-go/godeltaprof v0.1.7 // indirect
github.com/grafana/regexp v0.0.0-20240518133315-a468a5bfb3bc // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
Expand Down Expand Up @@ -138,7 +138,7 @@ require (
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/opentracing-contrib/go-grpc v0.0.0-20210225150812-73cb765af46e // indirect
github.com/opentracing-contrib/go-grpc v0.0.0-20240724223109-9dec25a38fa8 // indirect
github.com/opentracing-contrib/go-stdlib v1.0.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
Expand Down Expand Up @@ -170,17 +170,17 @@ require (
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/term v0.22.0 // indirect
golang.org/x/tools v0.23.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240722135656-d784300faade // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240722135656-d784300faade // indirect
golang.org/x/arch v0.9.0 // indirect
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa // indirect
golang.org/x/mod v0.20.0 // indirect
golang.org/x/term v0.23.0 // indirect
golang.org/x/tools v0.24.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240808171019-573a1156607a // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240808171019-573a1156607a // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240709000822-3c01b740850f // indirect
k8s.io/kube-openapi v0.0.0-20240808142205-8e686545bdb8 // indirect
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
Expand Down Expand Up @@ -241,19 +241,19 @@ require (
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20240424034433-3c2c7870ae76 // indirect
go.mongodb.org/mongo-driver v1.16.0
golang.org/x/crypto v0.25.0
golang.org/x/image v0.18.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/oauth2 v0.21.0
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
go.mongodb.org/mongo-driver v1.16.1
golang.org/x/crypto v0.26.0
golang.org/x/image v0.19.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/oauth2 v0.22.0
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.17.0 // indirect
gopkg.in/yaml.v3 v3.0.1
gorm.io/driver/sqlserver v1.5.3 // indirect
modernc.org/libc v1.55.3 // indirect
modernc.org/libc v1.57.0 // indirect
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.8.0 // indirect
modernc.org/sqlite v1.31.1 // indirect
modernc.org/sqlite v1.32.0 // indirect
)
Loading

0 comments on commit 4a74e60

Please sign in to comment.