Created
January 27, 2022 12:26
-
-
Save jsmolina/3ef7759d1033c2f226baa942a1129348 to your computer and use it in GitHub Desktop.
Go Thrift decode server without IDL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net/http" | |
"io/ioutil" | |
) | |
import "github.com/thrift-iterator/go" | |
import "github.com/thrift-iterator/go/general" | |
func hello(w http.ResponseWriter, req *http.Request) { | |
fmt.Fprintf(w, "hello\n") | |
var msg general.Message | |
body, err := ioutil.ReadAll(req.Body) | |
if err != nil { | |
fmt.Println("Error reading body: %v", err) | |
return | |
} | |
fmt.Println("****Received!!!!") | |
err2 := thrifter.Unmarshal(body, &msg) | |
// the RPC call method name, type is string | |
fmt.Println("Error:") | |
fmt.Println(err2) | |
fmt.Println("Message:") | |
fmt.Println(msg) | |
fmt.Println("Args:") | |
// the RPC call arguments, type is general.Struct | |
fmt.Println(msg.Arguments) | |
} | |
func headers(w http.ResponseWriter, req *http.Request) { | |
for name, headers := range req.Header { | |
for _, h := range headers { | |
fmt.Fprintf(w, "%v: %v\n", name, h) | |
} | |
} | |
} | |
func main() { | |
http.HandleFunc("/", hello) | |
http.ListenAndServe(":8080", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment