protoc-gen-service

command module
v0.0.0-...-50956c5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 7, 2024 License: MIT Imports: 15 Imported by: 0

README

protoc-gen-service

protoファイルから、gatewayサービスで使用するボイラープレートコードを生成するprotocのプラグインです。 対象は、以下のような実装で実現されるgRPCサーバへのシンプルなproxy処理に絞っています。

  • protoファイル
syntax = "proto3";

package datasource.code;
option go_package = "github.com/ca-risken/datasource-api/proto/code";

import "google/protobuf/empty.proto";
import "code/entity.proto";

service CodeService {
  // Code DataSource
  rpc ListDataSource(ListDataSourceRequest) returns (ListDataSourceResponse);
}
  • 生成されるコード(service_code_generated.go)

コードは同一のパッケージ(main)に生成されます。 protoファイルにパッケージが異なり名前が同じメソッドが定義されていると生成されたコードに含まれるメソッド名が衝突する可能性があるため、 メソッド名を以下の形式で生成します。

<protoファイルのmethod名(先頭を小文字)><protoファイルのpackageのsuffix(先頭を大文字)>Handler

また、生成されるファイル名は service_<protoファイルのpackageのsuffix>_generated.goです。

// Code generated by protoc-gen-service. DO NOT EDIT.
// source: code/service.proto

package main

import (
	"net/http"

	"github.com/ca-risken/datasource-api/proto/code"
)

func (g *gatewayService) listDataSourceCodeHandler(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()
	req := &code.ListDataSourceRequest{}
	bind(req, r)
	if err := req.Validate(); err != nil {
		writeResponse(ctx, w, http.StatusBadRequest, map[string]interface{}{errorJSONKey: err.Error()})
		return
	}
	resp, err := g.codeClient.ListDataSource(ctx, req)
	if err != nil {
		writeResponse(ctx, w, http.StatusInternalServerError, map[string]interface{}{errorJSONKey: err.Error()})
		return
	}
	writeResponse(ctx, w, http.StatusOK, map[string]interface{}{successJSONKey: resp})
}

Prerequisites

バージョンは開発時に使用していたもの

Usage

  1. build
go build
  1. generate code from proto file
protoc --plugin=<path-to-this-directory>/protoc-gen-service --service_out=<path-to-directory-deployed-generated-code> <path-to-proto-file>

生成しないメソッドを指定する方法

マイクロサービスの内部だけで使用するためgatewayサービスからproxyしたくないメソッドや、proxyする前に複雑な処理を行いたいメソッドがある場合、そのメソッドを自動生成の対象外に指定してください。

  • config
excludes:
  ## core
  - AlertService.AnalyzeAlertAll
  - FindingService.BatchListFinding
  - FindingService.PutFindingBatch
  - FindingService.PutResourceBatch
  - FindingService.ClearScore
  - IAMService.AnalyzeTokenExpiration
  - IAMService.IsAuthorized
  - IAMService.IsAuthorizedAdmin
  - IAMService.IsAuthorizedToken
  - ReportService.CollectReportFinding
  - ProjectService.IsActive
  ## datasource-api
  - GoogleService.InvokeScanAll
  - AWSService.InvokeScanAll
  - CodeService.GetGitHubSetting
  - CodeService.ListGitHubEnterpriseOrg
  - CodeService.PutGitHubEnterpriseOrg
  - CodeService.DeleteGitHubEnterpriseOrg
  - CodeService.InvokeScanAll
  - DiagnosisService.InvokeScanAll
  - OsintService.InvokeScanAll
  • generate code
protoc --plugin=<path-to-this-directory>/protoc-gen-service --service_out=configPath=<path-to-config-file>:<path-to-directory-deployed-generated-code> <path-to-proto-file>

参考

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL