github.com/cloudwego/kitex@v0.9.0/pkg/generic/descriptor/route.go (about)

     1  /*
     2   * Copyright 2021 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package descriptor
    18  
    19  import "net/http"
    20  
    21  // Route the route annotation
    22  type Route interface {
    23  	Method() string
    24  	Path() string
    25  	Function() *FunctionDescriptor
    26  }
    27  
    28  // NewRoute route creator
    29  type NewRoute func(value string, function *FunctionDescriptor) Route
    30  
    31  var (
    32  	// APIGetAnnotation api.get annotation
    33  	APIGetAnnotation = NewBAMAnnotation("api.get", NewAPIGet)
    34  	// APIPostAnnotation api.post annotation
    35  	APIPostAnnotation = NewBAMAnnotation("api.post", NewAPIPost)
    36  	// APIPutAnnotation api.put annotation
    37  	APIPutAnnotation = NewBAMAnnotation("api.put", NewAPIPut)
    38  	// APIDeleteAnnotation api.delete annotation
    39  	APIDeleteAnnotation = NewBAMAnnotation("api.delete", NewAPIDelete)
    40  )
    41  
    42  // NewAPIGet ...
    43  var NewAPIGet NewRoute = func(value string, function *FunctionDescriptor) Route {
    44  	return &apiRoute{http.MethodGet, value, function}
    45  }
    46  
    47  // NewAPIPost ...
    48  var NewAPIPost NewRoute = func(value string, function *FunctionDescriptor) Route {
    49  	return &apiRoute{http.MethodPost, value, function}
    50  }
    51  
    52  // NewAPIPut ...
    53  var NewAPIPut NewRoute = func(value string, function *FunctionDescriptor) Route {
    54  	return &apiRoute{http.MethodPut, value, function}
    55  }
    56  
    57  // NewAPIDelete ...
    58  var NewAPIDelete NewRoute = func(value string, function *FunctionDescriptor) Route {
    59  	return &apiRoute{http.MethodDelete, value, function}
    60  }
    61  
    62  type apiRoute struct {
    63  	method   string
    64  	value    string
    65  	function *FunctionDescriptor
    66  }
    67  
    68  func (r *apiRoute) Method() string {
    69  	return r.method
    70  }
    71  
    72  func (r *apiRoute) Path() string {
    73  	return r.value
    74  }
    75  
    76  func (r *apiRoute) Function() *FunctionDescriptor {
    77  	return r.function
    78  }