github.com/cloudwego/kitex@v0.9.0/tool/internal_pkg/tpl/server.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  // ServerTpl is the template for generating server.go.
    18  var ServerTpl string = `// Code generated by Kitex {{.Version}}. DO NOT EDIT.
    19  package {{ToLower .ServiceName}}
    20  
    21  import (
    22  	{{- range $path, $aliases := .Imports}}
    23  		{{- if not $aliases}}
    24  			"{{$path}}"
    25  		{{- else}}
    26  			{{- range $alias, $is := $aliases}}
    27  				{{$alias}} "{{$path}}"
    28  			{{- end}}
    29  		{{- end}}
    30  	{{- end}}
    31  )
    32  
    33  // NewServer creates a server.Server with the given handler and options.
    34  func NewServer(handler {{call .ServiceTypeName}}, opts ...server.Option) server.Server {
    35      var options []server.Option
    36      {{template "@server.go-NewServer-option" .}}
    37      options = append(options, opts...)
    38  	{{- if eq $.Codec "thrift"}}
    39  	options = append(options, server.WithCompatibleMiddlewareForUnary())
    40  	{{- end}}
    41  
    42      svr := server.NewServer(options...)
    43      if err := svr.RegisterService(serviceInfo(), handler); err != nil {
    44              panic(err)
    45      }
    46  	{{- if .FrugalPretouch}}
    47  	pretouch()
    48  	{{- end}}{{/* if .FrugalPretouch */}}
    49      return svr
    50  }
    51  {{template "@server.go-EOF" .}}
    52  
    53  func RegisterService(svr server.Server, handler {{call .ServiceTypeName}}, opts ...server.RegisterOption) error {
    54  	return svr.RegisterService(serviceInfo(), handler, opts...)
    55  }
    56  `