github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/testutil/httptransporttestutil/server/cmd/app/routes/proxy.go (about)

     1  package routes
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/machinefi/w3bstream/pkg/depends/kit/httptransport"
     8  	"github.com/machinefi/w3bstream/pkg/depends/kit/httptransport/client"
     9  	"github.com/machinefi/w3bstream/pkg/depends/kit/httptransport/httpx"
    10  	"github.com/machinefi/w3bstream/pkg/depends/kit/kit"
    11  )
    12  
    13  var ProxyRouter = kit.NewRouter(httptransport.Group("/proxy"))
    14  
    15  var (
    16  	c = &client.Client{
    17  		Host:    "ip-api.com",
    18  		Timeout: 100 * time.Second,
    19  	}
    20  )
    21  
    22  func init() {
    23  	c.SetDefault()
    24  
    25  	RootRouter.Register(ProxyRouter)
    26  
    27  	ProxyRouter.Register(kit.NewRouter(&Proxy{}))
    28  	ProxyRouter.Register(kit.NewRouter(&ProxyV2{}))
    29  }
    30  
    31  type Proxy struct {
    32  	httpx.MethodGet
    33  }
    34  
    35  func (Proxy) Output(ctx context.Context) (interface{}, error) {
    36  	resp := &IpInfo{}
    37  	_, err := c.Do(ctx, &GetByJSON{}).Into(resp)
    38  	return resp, err
    39  }
    40  
    41  type ProxyV2 struct {
    42  	httpx.MethodGet `basePath:"/demo/v2"`
    43  }
    44  
    45  func (ProxyV2) Output(ctx context.Context) (interface{}, error) {
    46  	result := c.Do(ctx, &GetByJSON{})
    47  
    48  	return httpx.WrapSchema(&IpInfo{})(result), nil
    49  }
    50  
    51  type GetByJSON struct {
    52  	httpx.MethodGet
    53  }
    54  
    55  func (GetByJSON) Path() string {
    56  	return "/json"
    57  }
    58  
    59  type IpInfo struct {
    60  	Country     string `json:"country"     xml:"country"`
    61  	CountryCode string `json:"countryCode" xml:"countryCode"`
    62  }