github.com/cloudwego/kitex@v0.9.0/tool/internal_pkg/tpl/invoker.go (about)

     1  // Copyright 2022 CloudWeGo Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //   http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package tpl
    16  
    17  // InvokerTpl is the template for generating invoker.go.
    18  var InvokerTpl string = `// Code generated by Kitex {{.Version}}. DO NOT EDIT.
    19  
    20  package {{ToLower .ServiceName}}
    21  
    22  import (
    23  	{{- range $path, $aliases := .Imports}}
    24  		{{- if not $aliases}}
    25  			"{{$path}}"
    26  		{{- else}}
    27  			{{- range $alias, $is := $aliases}}
    28  				{{$alias}} "{{$path}}"
    29  			{{- end}}
    30  		{{- end}}
    31  	{{- end}}
    32  )
    33  
    34  // NewInvoker creates a server.Invoker with the given handler and options.
    35  func NewInvoker(handler {{call .ServiceTypeName}}, opts ...server.Option) server.Invoker {
    36  	var options []server.Option
    37      {{template "@invoker.go-NewInvoker-option" .}}
    38  	options = append(options, opts...)
    39  
    40      s := server.NewInvoker(options...)
    41      if err := s.RegisterService(serviceInfo(), handler); err != nil {
    42              panic(err)
    43  	}
    44  	if err := s.Init(); err != nil {
    45  		panic(err)
    46  	}
    47  	{{- if .FrugalPretouch}}
    48  	pretouch()
    49  	{{- end}}{{/* if .FrugalPretouch */}}
    50      return s
    51  }
    52  {{template "@invoker.go-EOF" .}}
    53  `