Created
March 29, 2019 04:52
-
-
Save haozibi/1a063822317c95cb1d0281ebb20cb1d6 to your computer and use it in GitHub Desktop.
gRPC gateway 示例代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"net" | |
"sync" | |
"github.com/fvbock/endless" | |
"github.com/gin-gonic/gin" | |
grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery" | |
"github.com/grpc-ecosystem/grpc-gateway/runtime" | |
"google.golang.org/grpc" | |
"google.golang.org/grpc/credentials" | |
pb "/path/to/you.proto.go" | |
) | |
const ( | |
gRPCPort = 9191 | |
) | |
func main() { | |
err := Run() | |
panic(err) | |
} | |
// Run run app | |
func Run() (err error) { | |
exitCh := make(chan error) | |
var once sync.Once | |
wg := &WaitGroupWrapper{} | |
exitFunc := func(err error) { | |
once.Do(func() { | |
if err != nil { | |
} | |
exitCh <- err | |
}) | |
} | |
wg.Wrap(func() { | |
exitFunc(runGRpc()) | |
}) | |
// wg.Wrap(func() { | |
// exitFunc(runGateway()) | |
// }) | |
wg.Wrap(func() { | |
exitFunc(runWeb()) | |
}) | |
return <-exitCh | |
} | |
func runGateway() (*runtime.ServeMux, error) { | |
ctx := context.Background() | |
// ctx, cancel := context.WithCancel(ctx) | |
// defer cancel() | |
dcreds, err := credentials.NewClientTLSFromFile("keys/grpc/grpc.pem", "demo") | |
if err != nil { | |
return nil, err | |
} | |
mux := runtime.NewServeMux( | |
runtime.WithIncomingHeaderMatcher(runtime.DefaultHeaderMatcher), | |
runtime.WithMarshalerOption( | |
runtime.MIMEWildcard, | |
&runtime.JSONPb{OrigName: true, EmitDefaults: true}, | |
), | |
) | |
opts := []grpc.DialOption{grpc.WithTransportCredentials(dcreds)} | |
// 根据实际情况填写 | |
err = pb.RegisterDNSServerHandlerFromEndpoint(ctx, mux, fmt.Sprintf("localhost:%d", gRPCPort), opts) | |
if err != nil { | |
return nil, err | |
} | |
return mux, nil | |
} | |
func runGRpc() error { | |
port := gRPCPort | |
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) | |
if err != nil { | |
return err | |
} | |
creds, err := credentials.NewServerTLSFromFile("keys/grpc/grpc.pem", "keys/grpc/grpc.key") | |
if err != nil { | |
return err | |
} | |
s := grpc.NewServer(grpc.Creds(creds), | |
grpc.StreamInterceptor( | |
grpc_recovery.StreamServerInterceptor(), | |
), | |
grpc.UnaryInterceptor( | |
grpc_recovery.UnaryServerInterceptor(), | |
), | |
) | |
// 根据实际情况填写 | |
pb.RegisterDNSServerServer(s, ggrpc.New()) | |
fmt.Println("[grpc] run:", port) | |
if err = s.Serve(lis); err != nil { | |
return err | |
} | |
return nil | |
} | |
func runWeb() error { | |
var err error | |
port := 8080 | |
mux, err := runGateway() | |
if err != nil { | |
return err | |
} | |
r := gin.Default() | |
r.Any("/api/v1/*any", AuthMiddleware(), gin.WrapF(mux.ServeHTTP)) | |
r.StaticFile("/swagger/swagger.json", "dns_pb/dns.swagger.json") | |
fmt.Println("[http] run:", port) | |
err = endless.ListenAndServe(fmt.Sprintf(":%d", port), r) | |
if err != nil { | |
return err | |
} | |
return nil | |
} | |
// AuthMiddleware Gin 鉴权中间件 | |
func AuthMiddleware() gin.HandlerFunc { | |
return func(c *gin.Context) { | |
fmt.Println("login ok") | |
c.Next() | |
return | |
} | |
} | |
type WaitGroupWrapper struct { | |
sync.WaitGroup | |
} | |
// Wrap 包装后的待执行函数都会在协成中执行 | |
func (w *WaitGroupWrapper) Wrap(cb func()) { | |
w.Add(1) | |
go func() { | |
cb() | |
w.Done() | |
}() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment