Permalink
Browse files

local APIClient, proto changes

  • Loading branch information...
peter-edge committed Dec 3, 2015
1 parent 2e83cdd commit 38ebe1dd8632bf046ec5bba9a05efa954b7a31cd
Showing with 138 additions and 90 deletions.
  1. +18 −13 api_server.go
  2. +30 −29 cmd/protoeasy/main.go
  3. +3 −1 cmd/protoeasyd/main.go
  4. +18 −0 local_api_client.go
  5. +25 −3 protoeasy.go
  6. +43 −43 protoeasy.pb.go
  7. +1 −1 protoeasy.proto
View
@@ -14,25 +14,18 @@ import (
type apiServer struct {
protorpclog.Logger
compiler Compiler
options APIServerOptions
}
func newAPIServer(compiler Compiler) *apiServer {
return &apiServer{protorpclog.NewLogger("protoeasy.API"), compiler}
func newAPIServer(compiler Compiler, options APIServerOptions) *apiServer {
return &apiServer{protorpclog.NewLogger("protoeasy.API"), compiler, options}
}
func (a *apiServer) Compile(ctx context.Context, request *CompileRequest) (response *CompileResponse, retErr error) {
start := time.Now()
defer func() {
var compileOptions *CompileOptions
if request != nil {
compileOptions = request.CompileOptions
}
var compileInfo *CompileInfo
if response != nil {
compileInfo = response.CompileInfo
}
a.Log(compileOptions, compileInfo, retErr, time.Since(start))
}()
if !a.options.NoLogging {
defer func() { a.logCompile(request, response, retErr, start) }()
}
dirPath, err := ioutil.TempDir("", "protoeasy-input")
if err != nil {
return nil, err
@@ -72,3 +65,15 @@ func (a *apiServer) Compile(ctx context.Context, request *CompileRequest) (respo
},
}, nil
}
func (a *apiServer) logCompile(request *CompileRequest, response *CompileResponse, err error, start time.Time) {
var compileOptions *CompileOptions
if request != nil {
compileOptions = request.CompileOptions
}
var compileInfo *CompileInfo
if response != nil {
compileInfo = response.CompileInfo
}
a.Log(compileOptions, compileInfo, err, time.Since(start))
}
View
@@ -8,6 +8,7 @@ import (
"google.golang.org/grpc"
"go.pedge.io/env"
"go.pedge.io/pkg/cobra"
"go.pedge.io/protoeasy"
"go.pedge.io/protolog"
@@ -37,37 +38,13 @@ func do(appEnvObj interface{}) error {
rootCmd := &cobra.Command{
Use: fmt.Sprintf("%s directory", os.Args[0]),
RunE: func(_ *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("must pass one argument, the directory, but passed %d arguments", len(args))
}
if err := optionsToCompileOptions(options, compileOptions); err != nil {
if err := pkgcobra.CheckFixedArgs(1, args); err != nil {
return err
}
dirPath := args[0]
outDirPath := args[0]
if options.OutDirPath != "" {
outDirPath = options.OutDirPath
}
compiler := protoeasy.DefaultServerCompiler
if appEnv.Address != "" {
clientConn, err := grpc.Dial(appEnv.Address, grpc.WithInsecure())
if err != nil {
return err
}
compiler = protoeasy.NewClientCompiler(
protoeasy.NewAPIClient(
clientConn,
),
protoeasy.CompilerOptions{},
)
}
commands, err := compiler.Compile(dirPath, outDirPath, compileOptions)
if err != nil {
if err := optionsToCompileOptions(options, compileOptions); err != nil {
return err
}
logCommands(commands)
pkgcobra.Check(run(appEnv, options, compileOptions, args[0]))
return nil
},
}
@@ -82,7 +59,7 @@ func bindCompileOptions(flagSet *pflag.FlagSet, compileOptions *protoeasy.Compil
flagSet.BoolVar(&compileOptions.GrpcGateway, "grpc-gateway", false, "Output grpc-gateway .gw.go files.")
flagSet.BoolVar(&compileOptions.NoDefaultIncludes, "no-default-includes", false, "Do not import the default include directories, implies --go-no-default-modifiers.")
flagSet.StringSliceVar(&compileOptions.ExcludePattern, "exclude", []string{}, "Exclude file patterns.")
flagSet.StringVar(&compileOptions.RelParent, "parent", "", "The directory we are within, must be relative. This directory will also be on the include path.")
flagSet.StringVar(&compileOptions.RelContext, "context", "", "The directory we are within, must be relative. This directory will be the base of the include path.")
flagSet.BoolVar(&compileOptions.Cpp, "cpp", false, "Output cpp files.")
flagSet.StringVar(&compileOptions.CppRelOut, "cpp-rel-out", "", "The directory, relative to the output directory, to output cpp files.")
@@ -141,10 +118,34 @@ func optionsToCompileOptions(options *options, compileOptions *protoeasy.Compile
return nil
}
func logCommands(commands []*protoeasy.Command) {
func run(appEnv *appEnv, options *options, compileOptions *protoeasy.CompileOptions, dirPath string) error {
outDirPath := dirPath
if options.OutDirPath != "" {
outDirPath = options.OutDirPath
}
compiler := protoeasy.DefaultServerCompiler
if appEnv.Address != "" {
clientConn, err := grpc.Dial(appEnv.Address, grpc.WithInsecure())
if err != nil {
return err
}
compiler = protoeasy.NewClientCompiler(
protoeasy.NewAPIClient(
clientConn,
),
protoeasy.CompilerOptions{},
)
}
commands, err := compiler.Compile(dirPath, outDirPath, compileOptions)
if err != nil {
return err
}
for _, command := range commands {
if len(command.Arg) > 0 {
protolog.Infof("\n%s\n", strings.Join(command.Arg, " \\\n\t"))
}
}
return nil
}
View
@@ -23,7 +23,9 @@ func main() {
func do(appEnvObj interface{}) error {
appEnv := appEnvObj.(*appEnv)
pkglog.SetupLogging("protoeasyd", appEnv.LogEnv)
if err := pkglog.SetupLogging("protoeasyd", appEnv.LogEnv); err != nil {
return err
}
server := grpc.NewServer(grpc.MaxConcurrentStreams(math.MaxUint32))
protoeasy.RegisterAPIServer(server, protoeasy.DefaultAPIServer)
View
@@ -0,0 +1,18 @@
package protoeasy
import (
"golang.org/x/net/context"
"google.golang.org/grpc"
)
type localAPIClient struct {
apiServer APIServer
}
func newLocalAPIClient(apiServer APIServer) *localAPIClient {
return &localAPIClient{apiServer}
}
func (a *localAPIClient) Compile(ctx context.Context, request *CompileRequest, _ ...grpc.CallOption) (*CompileResponse, error) {
return a.apiServer.Compile(ctx, request)
}
View
@@ -24,7 +24,19 @@ var (
//DefaultServerCompiler is the default Compiler for a server.
DefaultServerCompiler = NewServerCompiler(CompilerOptions{})
// DefaultAPIServer is the default API Server.
DefaultAPIServer = NewAPIServer(DefaultServerCompiler)
DefaultAPIServer = NewAPIServer(DefaultServerCompiler, APIServerOptions{})
// DefaultClientCompiler is the default Compiler for a client.
DefaultClientCompiler = NewClientCompiler(
NewLocalAPIClient(
NewAPIServer(
DefaultServerCompiler,
APIServerOptions{
NoLogging: true,
},
),
),
CompilerOptions{},
)
)
// Compiler compiles protocol buffer files.
@@ -42,9 +54,19 @@ func NewServerCompiler(options CompilerOptions) Compiler {
return newServerCompiler(options)
}
// APIServerOptions are options for an APIServer.
type APIServerOptions struct {
NoLogging bool
}
// NewAPIServer returns a new APIServer for the given Compiler.
func NewAPIServer(compiler Compiler) APIServer {
return newAPIServer(compiler)
func NewAPIServer(compiler Compiler, options APIServerOptions) APIServer {
return newAPIServer(compiler, options)
}
// NewLocalAPIClient returns a new APIClient that calls the APIServer directly.
func NewLocalAPIClient(apiServer APIServer) APIClient {
return newLocalAPIClient(apiServer)
}
// NewClientCompiler returns a new client Compiler for the given APIClient.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -19,7 +19,7 @@ message CompileOptions {
bool grpc_gateway = 2;
bool no_default_includes = 3;
repeated string exclude_pattern = 4;
string rel_parent = 5;
string rel_context = 5;
bool cpp = 20;
string cpp_rel_out = 21;

0 comments on commit 38ebe1d

Please sign in to comment.