add some routes

This commit is contained in:
2026-09-03 18:48:23 +08:00
parent c4ccba256d
commit 86cde9bb97
20 changed files with 520712 additions and 7 deletions

54
pbutil/pbutil.go Normal file
View File

@@ -0,0 +1,54 @@
package pbutil
import (
"encoding/json"
"fmt"
"os"
"strings"
_ "bdgrp_go/proto/cn"
_ "bdgrp_go/proto/gl"
_ "bdgrp_go/proto/jp"
_ "bdgrp_go/proto/tw"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
)
func fullName(message string) protoreflect.FullName {
server := strings.ToLower(os.Getenv("SERVER"))
// twdori.proto.ce.UserLogin
return protoreflect.FullName(fmt.Sprintf("%sdori.proto.ce.%s", server, message))
}
func Decode(message string, data []byte) (proto.Message, error) {
mt, err := protoregistry.GlobalTypes.FindMessageByName(fullName(message))
if err != nil {
return nil, fmt.Errorf("message %s not found", message)
}
msg := mt.New().Interface()
if err := proto.Unmarshal(data, msg); err != nil {
return nil, err
}
return msg, nil
}
func Encode(message string, data map[string]any) ([]byte, error) {
mt, err := protoregistry.GlobalTypes.FindMessageByName(fullName(message))
if err != nil {
return nil, fmt.Errorf("message %s not found", message)
}
msg := mt.New().Interface()
// map → json → proto
jsonBytes, _ := json.Marshal(data)
if err := protojson.Unmarshal(jsonBytes, msg); err != nil {
return nil, err
}
return proto.Marshal(msg)
}