第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

jsonrpc 服務(wù)器接受請(qǐng)求的小寫(xiě)方法名稱(用于大寫(xiě)注冊(cè)服務(wù))

jsonrpc 服務(wù)器接受請(qǐng)求的小寫(xiě)方法名稱(用于大寫(xiě)注冊(cè)服務(wù))

Go
墨色風(fēng)雨 2023-06-01 15:07:25
我正在嘗試編寫(xiě)一個(gè) jsonrpc 服務(wù)器,它將接受請(qǐng)求的小寫(xiě)方法名稱,例如 Arith.multiply,并將它們正確地路由到相應(yīng)的大寫(xiě)方法,例如 Arith.Multiply。這可能嗎?PS 它是用于測(cè)試的生產(chǎn)服務(wù)器的輕量級(jí)克隆,API 是固定的,包括小寫(xiě)的方法名稱,所以我無(wú)法將請(qǐng)求的方法名稱更改為大寫(xiě)。package mainimport (    "log"    "net/http"    "github.com/gorilla/mux"    "github.com/gorilla/rpc"    "github.com/gorilla/rpc/json")type Args struct {    A, B int}type Arith inttype Result intfunc (t *Arith) Multiply(r *http.Request, args *Args, result *Result) error {    log.Printf("Multiplying %d with %d\n", args.A, args.B)    *result = Result(args.A * args.B)    return nil}func main() {    s := rpc.NewServer()    s.RegisterCodec(json.NewCodec(), "application/json")    s.RegisterCodec(json.NewCodec(), "application/json;charset=UTF-8")    arith := new(Arith)    s.RegisterService(arith, "")    r := mux.NewRouter()    r.Handle("/rpc", s)    http.ListenAndServe(":1234", r)}
查看完整描述

1 回答

?
臨摹微笑

TA貢獻(xiàn)1982條經(jīng)驗(yàn) 獲得超2個(gè)贊

看來(lái)您可以將某些東西偷偷放入自定義編解碼器中,以將小寫(xiě)方法定向到正確的大寫(xiě)方法。編寫(xiě) gorilla/rpc/json 實(shí)現(xiàn)的 CodecRequest,您可以繼續(xù)使用所有 gorilla 機(jī)器來(lái)處理請(qǐng)求。


下面的工作示例??雌饋?lái)很長(zhǎng),但都是評(píng)論。


package main


import (

    "fmt"

    "log"

    "net/http"

    "strings"

    "unicode"

    "unicode/utf8"


    "github.com/gorilla/mux"

    "github.com/gorilla/rpc"

    "github.com/gorilla/rpc/json"

)


type Args struct {

    A, B int

}

type Arith int


type Result int


func (t *Arith) Multiply(r *http.Request, args *Args, result *Result) error {

    log.Printf("Multiplying %d with %d\n", args.A, args.B)

    *result = Result(args.A * args.B)

    return nil

}


// UpCodec creates a CodecRequest to process each request.

type UpCodec struct {

}


// NewUpCodec returns a new UpCodec.

func NewUpCodec() *UpCodec {

    return &UpCodec{}

}


// NewRequest returns a new CodecRequest of type UpCodecRequest.

func (c *UpCodec) NewRequest(r *http.Request) rpc.CodecRequest {

    outerCR := &UpCodecRequest{}   // Our custom CR

    jsonC := json.NewCodec()       // json Codec to create json CR

    innerCR := jsonC.NewRequest(r) // create the json CR, sort of.


    // NOTE - innerCR is of the interface type rpc.CodecRequest.

    // Because innerCR is of the rpc.CR interface type, we need a

    // type assertion in order to assign it to our struct field's type.

    // We defined the source of the interface implementation here, so

    // we can be confident that innerCR will be of the correct underlying type

    outerCR.CodecRequest = innerCR.(*json.CodecRequest)

    return outerCR

}


// UpCodecRequest decodes and encodes a single request. UpCodecRequest

// implements gorilla/rpc.CodecRequest interface primarily by embedding

// the CodecRequest from gorilla/rpc/json. By selectively adding

// CodecRequest methods to UpCodecRequest, we can modify that behaviour

// while maintaining all the other remaining CodecRequest methods from

// gorilla's rpc/json implementation

type UpCodecRequest struct {

    *json.CodecRequest

}


// Method returns the decoded method as a string of the form "Service.Method"

// after checking for, and correcting a lowercase method name

// By being of lower depth in the struct , Method will replace the implementation

// of Method() on the embedded CodecRequest. Because the request data is part

// of the embedded json.CodecRequest, and unexported, we have to get the

// requested method name via the embedded CR's own method Method().

// Essentially, this just intercepts the return value from the embedded

// gorilla/rpc/json.CodecRequest.Method(), checks/modifies it, and passes it

// on to the calling rpc server.

func (c *UpCodecRequest) Method() (string, error) {

    m, err := c.CodecRequest.Method()

    if len(m) > 1 && err == nil {

        parts := strings.Split(m, ".")

        service, method := parts[0], parts[1]

        r, n := utf8.DecodeRuneInString(method) // get the first rune, and it's length

        if unicode.IsLower(r) {

            upMethod := service + "." + string(unicode.ToUpper(r)) + method[n:]

            log.Printf("lowercase method %s requested: treated as %s\n", m, upMethod)

            return upMethod, err

        }

    }

    return m, err

}


func main() {

    s := rpc.NewServer()


    // Register our own Codec

    s.RegisterCodec(NewUpCodec(), "application/json")

    s.RegisterCodec(NewUpCodec(), "application/json;charset=UTF-8")


    arith := new(Arith)

    s.RegisterService(arith, "")

    r := mux.NewRouter()

    r.Handle("/rpc", s)

    fmt.Println(http.ListenAndServe(":1234", r))

}

通過(guò)以下方式調(diào)用方法:


curl -X POST -H "Content-Type: application/json" -d '{"id": 1, "method": "Arith.multiply", "params": [{"A": 10, "B": 30}]}' 127.0.0.1:1234/rpc



查看完整回答
反對(duì) 回復(fù) 2023-06-01
  • 1 回答
  • 0 關(guān)注
  • 166 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)