github.com/Kong/go-pdk@v0.11.0/router/router.go (about)

     1  /*
     2  Router module.
     3  
     4  A set of functions to access the routing properties of the request.
     5  */
     6  package router
     7  
     8  import (
     9  	"github.com/Kong/go-pdk/bridge"
    10  	"github.com/Kong/go-pdk/entities"
    11  	"github.com/Kong/go-pdk/server/kong_plugin_protocol"
    12  )
    13  
    14  // Holds this module's functions.  Accessible as `kong.Router`
    15  type Router struct {
    16  	bridge.PdkBridge
    17  }
    18  
    19  // kong.Router.GetRoute() returns the current route entity.
    20  // The request was matched against this route.
    21  func (c Router) GetRoute() (route entities.Route, err error) {
    22  	out := new(kong_plugin_protocol.Route)
    23  	err = c.Ask(`kong.router.get_route`, nil, out)
    24  	if err != nil {
    25  		return
    26  	}
    27  
    28  	serviceId := ""
    29  	if out.Service != nil {
    30  		serviceId = out.Service.Id
    31  	}
    32  
    33  	route = entities.Route{
    34  		Id:                      out.Id,
    35  		CreatedAt:               int(out.CreatedAt),
    36  		UpdatedAt:               int(out.UpdatedAt),
    37  		Name:                    out.Name,
    38  		Protocols:               out.Protocols,
    39  		Methods:                 out.Methods,
    40  		Hosts:                   out.Hosts,
    41  		Paths:                   out.Paths,
    42  		Headers:                 out.Headers,
    43  		HTTPSRedirectStatusCode: int(out.HttpsRedirectStatusCode),
    44  		RegexPriority:           int(out.RegexPriority),
    45  		StripPath:               out.StripPath,
    46  		PreserveHost:            out.PreserveHost,
    47  		SNIs:                    out.Snis,
    48  		Sources:                 out.Sources,
    49  		Destinations:            out.Destinations,
    50  		Tags:                    out.Tags,
    51  		Service:                 entities.ServiceKey{Id: serviceId},
    52  	}
    53  	return
    54  }
    55  
    56  // // kong.Router.GetService() returns the current service entity.
    57  // // The request will be targetted to this upstream service.
    58  func (c Router) GetService() (service entities.Service, err error) {
    59  	out := new(kong_plugin_protocol.Service)
    60  	err = c.Ask(`kong.router.get_service`, nil, out)
    61  	if err != nil {
    62  		return
    63  	}
    64  
    65  	service = entities.Service{
    66  		Id:                out.Id,
    67  		CreatedAt:         int(out.CreatedAt),
    68  		UpdatedAt:         int(out.UpdatedAt),
    69  		Name:              out.Name,
    70  		Retries:           int(out.Retries),
    71  		Protocol:          out.Protocol,
    72  		Host:              out.Host,
    73  		Port:              int(out.Port),
    74  		Path:              out.Path,
    75  		ConnectTimeout:    int(out.ConnectTimeout),
    76  		WriteTimeout:      int(out.WriteTimeout),
    77  		ReadTimeout:       int(out.ReadTimeout),
    78  		Tags:              out.Tags,
    79  // 		ClientCertificate: entities.CertificateKey{Id: out.ClientCertificate.Id},
    80  	}
    81  	return
    82  }