88 lines
1.7 KiB
Go
88 lines
1.7 KiB
Go
package tpl
|
|
|
|
var NewFile = `
|
|
// Code generated by protoc-gen-slc. DO NOT EDIT.
|
|
package server
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
{import}
|
|
"git.apinb.com/bsm-sdk/core/vars"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/reflection"
|
|
gwRuntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
|
)
|
|
|
|
type Server struct {
|
|
Grpc *grpc.Server
|
|
Ctx context.Context
|
|
Mux *gwRuntime.ServeMux
|
|
}
|
|
|
|
func New(addr string) *Server {
|
|
srv := &Server{Ctx: context.Background(), Grpc: grpc.NewServer(), Mux: gwRuntime.NewServeMux()}
|
|
|
|
// register service to grpc.Server
|
|
{register}
|
|
|
|
reflection.Register(srv.Grpc)
|
|
|
|
// 将服务注册到Gateway
|
|
opts := []grpc.DialOption{grpc.WithInsecure()}
|
|
{gw}
|
|
|
|
// Register services swagger
|
|
srv.RegisterSwagger()
|
|
|
|
|
|
return srv
|
|
}
|
|
|
|
// RegisterSwagger 注册swagger
|
|
func (s *Server) RegisterSwagger() {
|
|
srvKey := strings.ToLower(vars.ServiceKey)
|
|
s.Mux.HandlePath("GET", "/"+srvKey+".swagger.json", func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
bytes, err := os.ReadFile("./swagger/" + srvKey + ".swagger.json")
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
w.Write([]byte(err.Error()))
|
|
return
|
|
}
|
|
w.Write(bytes)
|
|
return
|
|
|
|
})
|
|
}
|
|
|
|
`
|
|
|
|
var Server = `
|
|
// Code generated by protoc-gen-slc. DO NOT EDIT.
|
|
package server
|
|
|
|
import (
|
|
"context"
|
|
{import}
|
|
)
|
|
|
|
type {service}Server struct {
|
|
pb.Unimplemented{service}Server
|
|
}
|
|
|
|
func New{service}Server() *{service}Server {
|
|
return &{service}Server{}
|
|
}
|
|
|
|
{method}
|
|
`
|
|
var Method = `
|
|
{comment}
|
|
func (s *{service}Server) {func}(ctx context.Context,in *pb.{input}) (*pb.{output},error) {
|
|
return {serviceLower}.{func}(ctx, in)
|
|
}
|
|
`
|