github.com/gramework/gramework@v1.8.1-0.20231027140105-82555c9057f5/x/client/handler.go (about)

     1  // Copyright 2017-present Kirill Danshin and Gramework contributors
     2  // Copyright 2019-present Highload LTD (UK CN: 11893420)
     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  
    11  package client
    12  
    13  import (
    14  	"github.com/gramework/gramework"
    15  )
    16  
    17  // Handler returns gramework handler
    18  func (client *Instance) Handler() func(*gramework.Context) error {
    19  	return client.handleHTTP
    20  }
    21  
    22  func (client *Instance) handleHTTP(ctx *gramework.Context) error {
    23  	api, err := client.nextServer()
    24  	if err != nil {
    25  		ctx.Logger.Errorf("error %s", err)
    26  		return err
    27  	}
    28  
    29  	bytes := buffer.Get()
    30  	defer buffer.Put(bytes)
    31  	statusCode, body, err := api.HostClient.Get(bytes.B, api.Addr)
    32  	if err != nil {
    33  		ctx.Logger.Errorf("error while .Do() the request %s", err)
    34  		return err
    35  	}
    36  
    37  	ctx.SetStatusCode(statusCode)
    38  	_, err = ctx.Write(body)
    39  	return err
    40  }