I'm writing a thrift service in Golang and I would like to understand how I can get the client's IP address in the handler functions implementation.
I have tried it in Java, which can be obtained by overwrite TProcessor#process(in,out), but it seems that this way doesn't work in Golang.
serverTransport, err := thrift.NewTServerSocket(net.JoinHostPort(ip, strconv.FormatInt(port, 10)))
//serverTransport, err := thrift.NewTServerSocket(net.JoinHostPort("127.0.0.1", "9999"))
if err != nil {
log.Error(err.Error())
os.Exit(1)
}
protocolFactory := thrift.NewTCompactProtocolFactory()
transportFactory := thrift.NewTFramedTransportFactoryMaxLength(thrift.NewTTransportFactory(), 524288000)
processor := iface.NewNMPDataServiceProcessor(new(impl.NMPDataService))
server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
server.Serve()
defer server.Stop()
The Handler implementation of NMPDataService interface:
package impl
import (
"NMPService/Framework/logger"
"NMPService/NmpService/thrift/iface"
"NMPService/NmpService/utils"
"syscall"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"github.com/yeka/zip"
)
var log = logger.GetBeeLogger()
// NMPDataService .
type NMPDataService struct {
}
const (
// MaxDownloadFileLimit ...
MaxDownloadFileLimit int64 = 15728640
TempFolder string = "tmp"
)
// ExistsFolder method
// Parameters:
// - Path
func (handler *NMPDataService) ExistsFolder(path string) (bool, error) {
log.Info("Check exist folder, path: %s", path)
if strings.Contains(path, "\\") {
path = strings.ReplaceAll(path, "\\", "/")
}
fileInfo, err := os.Stat(path)
if os.IsNotExist(err) {
log.Error("folder %s not exist", path)
return false, nil
}
if !fileInfo.IsDir() {
msg := "the path is not folder."
log.Error(msg)
return false, NewRPCException(1, msg, errors.New(msg))
}
if err != nil {
log.Error("Check exist folder error.")
return false, NewRPCException(1, "Check exist folder error", err)
}
return true, nil
}
Above is my code on Server side. I want to get client IP information from server.