当前位置: 首页 > news >正文

网站建设和管理情况自查报告泰国做网站网站要判几年

网站建设和管理情况自查报告,泰国做网站网站要判几年,网络整合营销方案策划,郑州最新通告1、gateway集成swagger 1、为了简化实战过程#xff0c;gRPC-Gateway暴露的服务并未使用https#xff0c;而是http#xff0c;但是swagger-ui提供的调用服 务却是https的#xff0c;因此要在proto文件中指定swagger以http调用服务#xff0c;指定的时候会用到文件 prot…1、gateway集成swagger 1、为了简化实战过程gRPC-Gateway暴露的服务并未使用https而是http但是swagger-ui提供的调用服 务却是https的因此要在proto文件中指定swagger以http调用服务指定的时候会用到文件 protoc-gen-swagger/options/annotations.proto因此需要找到这个文件对应的包放在合适的位置。 2、swaggerdemo.swagger.json这是swagger-ui要用的json文件依据此文件swagger才能正确的展现 出gRPC-Gateway暴露的服务和参数定义可以在页面上发起请求此文件由插件protoc-gen-swagger生成。 3、在gRPC-Gateway的代码中集成swagger-ui的代码swagger-ui的代码由多个png、html、js文件组成 需要用工具go-bindata转换成go源码并放入合适的位置流程如下图 4、要将swaggerdemo.swagger.json文件通过web暴露出来需要工具go-bindata-assetfs。 5、使用swagger的方式打开swagger-ui页面后将swaggerdemo.swagger.json输入给swagger-ui页面 令其解析后生成对应的在线接口服务。 1.1 安装必要的go包 1、安装protoc-gen-swagger $ go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger $ go install github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger2、go-bindata用来将swagger-ui的源码转为GO代码 $ go install github.com/jteeuwen/go-bindata/...3、go-bindata-assetfs在应用启动后对外提供文件服务这样可以通过web访问swagger的json文件 $ go install github.com/elazarl/go-bindata-assetfs/...4、glog是常用的日志工具 $ go get -u github.com/golang/glog1.2 编写proto文件 新建swaggerdemo.proto // 协议类型 syntax proto3;// 包名 package swaggerdemo;option go_package./protoc;swaggerdemo;import google/api/annotations.proto; import protoc-gen-swagger/options/annotations.proto;// 定义swagger内容 option (grpc.gateway.protoc_gen_swagger.options.openapiv2_swagger) {info: {title: grpc gateway helloworld sample;version: 1.0;};schemes: HTTP; };// 定义的服务名 service Greeter {// 具体的远程服务方法rpc SayHello (HelloRequest) returns (HelloReply) {option (google.api.http) {post: /helloworldbody: *};} }// SayHello方法的入参只有一个字符串字段 message HelloRequest {string name 1; }// SayHello方法的返回值只有一个字符串字段 message HelloReply {string message 1; }protoc-gen-swagger/options/annotations.proto来自于 github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger 生成swaggerdemo.pb.gogRPC所需的go文件 $ protoc --go_outpluginsgrpc:. protoc/swaggerdemo.proto生成swaggerdemo.pb.gw.gogRPC-Gateway所需的go文件 $ protoc --grpc-gateway_outlogtostderrtrue:. protoc/swaggerdemo.proto生成swaggerdemo.swagger.jsonswagger-ui要用的json文件依据此文件swagger展现的页面中会有 gRPC-Gateway暴露的服务和参数定义可以在页面上发起请求 $ protoc --swagger_outlogtostderrtrue:. protoc/swaggerdemo.proto1.3 生成swagger-ui的go文件 从 https://github.com/swagger-api/swagger-ui下载包解压把dist目录下的所有文件拷贝我们项目的 /swagger/swagger-ui/目录下。 运行指令把Swagger UI转成datafile.go代码 $ go-bindata --nocompress -pkg swagger -o swagger/datafile.go swagger/swagger-ui/...1.4 编写gRPC的服务端代码 新建文件server.go内容如下 package mainimport (contextgoogle.golang.org/grpclognetpb swaggerproject/protoc )const (port :50051 )// 定义结构体在调用注册api的时候作为入参 // 该结构体会带上SayHello方法里面是业务代码 // 这样远程调用时就执行了业务代码了 type server struct {// pb.go中自动生成的是个空结构体pb.UnimplementedGreeterServer }// 业务代码在此写客户端远程调用SayHello时 // 会执行这里的代码 func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {// 打印请求参数log.Printf(Received: %v, in.GetName())// 实例化结构体HelloReply作为返回值return pb.HelloReply{Message: Hello in.GetName()}, nil }func main() {// 要监听的协议和端口lis, err : net.Listen(tcp, port)if err ! nil {log.Fatalf(failed to listen: %v, err)}// 实例化gRPC server结构体s : grpc.NewServer()// 服务注册pb.RegisterGreeterServer(s, server{})log.Println(开始监听等待远程调用...)if err : s.Serve(lis); err ! nil {log.Fatalf(failed to serve: %v, err)} }1.5 编写gRPC-Gateway服务端的代码 新建文件gateway.go内容如下 package mainimport (github.com/golang/gloggithub.com/grpc-ecosystem/grpc-gateway/runtimegolang.org/x/net/contextgoogle.golang.org/grpclognet/httppathstringsgw swaggerproject/protocswaggerproject/swaggerassetfs github.com/elazarl/go-bindata-assetfs )func run() error {ctx : context.Background()ctx, cancel : context.WithCancel(ctx)defer cancel()gwmux, err : newGateway(ctx)if err ! nil {panic(err)}mux : http.NewServeMux()mux.Handle(/, gwmux)mux.HandleFunc(/swagger/, serveSwaggerFile)serveSwaggerUI(mux)log.Println(grpc-gateway listen on localhost:9090)return http.ListenAndServe(:9090, mux) }func newGateway(ctx context.Context) (http.Handler, error) {opts : []grpc.DialOption{grpc.WithInsecure()}gwmux : runtime.NewServeMux()if err : gw.RegisterGreeterHandlerFromEndpoint(ctx, gwmux, :50051, opts); err ! nil {return nil, err}return gwmux, nil }func serveSwaggerFile(w http.ResponseWriter, r *http.Request) {log.Println(start serveSwaggerFile)if !strings.HasSuffix(r.URL.Path, swagger.json) {log.Printf(Not Found: %s, r.URL.Path)http.NotFound(w, r)return}p : strings.TrimPrefix(r.URL.Path, /swagger/)p path.Join(./protoc/, p)log.Printf(Serving swagger-file: %s, p)http.ServeFile(w, r, p) }func serveSwaggerUI(mux *http.ServeMux) {fileServer : http.FileServer(assetfs.AssetFS{Asset: swagger.Asset,AssetDir: swagger.AssetDir,Prefix: swagger/swagger-ui,})prefix : /swagger-ui/mux.Handle(prefix, http.StripPrefix(prefix, fileServer)) }func main() {defer glog.Flush()if err : run(); err ! nil {glog.Fatal(err)} }对于这个gateway.go文件有以下几处需要重点注意 1、外部的RESTful请求转发到server.go的功能被封装到newGateway方法中 2、请求URL中如果含有/swagger就交给serveSwaggerFile方法处理这里面的逻辑是将文件 swaggerdemo.swagger.json返回给请求方 3、重点关注serveSwaggerUI方法经过该方法的处理后如果请求URL中含有/swagger-ui就会交给前面 生成的datafile.go处理也就是打开了swagger-ui的页面 至此开发工作已经完成可以开始验证了。 1.6 测试 [rootzsx swagger_demo]# go run server.go 2023/02/12 19:00:16 开始监听等待远程调用...[rootzsx swagger_demo]# go run gateway.go 2023/02/12 19:00:28 grpc-gateway listen on localhost:9090访问 http://127.0.0.1:9090/swagger-ui/ 输入 http://127.0.0.1:9090/swagger/swaggerdemo.swagger.json 输入请求数据 发送请求 至此gateway集成swagger完成。 # 项目结构 $ tree swagger_demo/ swagger_demo/ ├── gateway.go ├── go.mod ├── google │ └── api │ ├── annotations.proto │ └── http.proto ├── go.sum ├── protoc │ ├── swaggerdemo.pb.go │ ├── swaggerdemo.pb.gw.go │ ├── swaggerdemo.proto │ └── swaggerdemo.swagger.json ├── protoc-gen-swagger │ └── options │ ├── annotations.proto │ └── openapiv2.proto ├── server.go └── swagger├── datafile.go└── swagger-ui├── favicon-16x16.png├── favicon-32x32.png├── index.css├── index.html├── oauth2-redirect.html├── swagger-initializer.js├── swagger-ui-bundle.js├── swagger-ui-bundle.js.map├── swagger-ui.css├── swagger-ui.css.map├── swagger-ui-es-bundle-core.js├── swagger-ui-es-bundle-core.js.map├── swagger-ui-es-bundle.js├── swagger-ui-es-bundle.js.map├── swagger-ui.js├── swagger-ui.js.map├── swagger-ui-standalone-preset.js└── swagger-ui-standalone-preset.js.map7 directories, 31 files
http://www.dnsts.com.cn/news/220305.html

相关文章:

  • 网站开发与推广方向网站建设管理岗位职责
  • 网站界面类型金融网站开发目的
  • 太原网站推广公司网站域名注册证书是什么
  • 厦门网站建设哪里好在线查询企业
  • 建设网站的公司哪家好企业管理系统开发平台
  • 四会建设局网站郑州制作网页的公司
  • 瑞安网站建设优化海尔集团网站的网络营销是什么
  • 网站整体营销方案自考大型网站开发工具
  • 网站域名备案主机名响应式网站 英语
  • 淮安 网站建设:wordpress网页编辑
  • 门户网站群建设ui设计和平面设计有什么区别
  • 清镇网站建设平面设计软件图标图片
  • 营销网站制作费用建站平台费用
  • 网站seo提升视频网站如何做弹幕
  • 网站建设合同范本-经过律师审核大龄网站开发人员
  • 企业手机网站开发成都文化墙设计公司
  • 网站建设服务方案ppt模板wordpress建站哪里好
  • 东莞网站建设 石佳保定seo关键词优化外包
  • 做网站要的图片斗鱼交互效果网站
  • 建设网站的服务费是指什么意思网站服务端做处理跨域
  • 龙海市邦策网站建设平台东莞网站设计讯息
  • 长春网站运做思路做网站价格miniuinet
  • 网页qq怎么登录新seo排名点击软件
  • 网站建设中服务器搭建方式郑州豆芽网站建设
  • 学做巧裁缝官方网站用户体验设计课程
  • 重庆网站seo方法免费建英文网站
  • 登录浏览器是建设银行移动门户网站wordpress变成英文版
  • 网站建设宀金手指花总十四行业论坛网站
  • 福州网站设计软件成都高端网站设计
  • 网站打不开怎么处理湖南省建设厅田明革简介