1
0
mirror of https://github.com/gusaul/grpcox.git synced 2024-12-24 17:30:41 +00:00

feat: 🎸 allow using metadata as reflection headers

This commit is contained in:
Petra 2022-07-30 12:22:19 +07:00
parent 8534ee1dc1
commit 63fe96c73d
3 changed files with 36 additions and 2 deletions

View File

@ -137,6 +137,11 @@ func (g *GrpCox) Extend(host string) {
g.activeConn.extend(host, g.maxLifeConn)
}
// SetReflectHeaders sets grpcox reflection headers
func (g *GrpCox) SetReflectHeaders(headers ...string) {
g.reflectHeaders = headers
}
func (g *GrpCox) dial(ctx context.Context, target string, plainText bool) (*grpc.ClientConn, error) {
dialTime := 10 * time.Second
ctx, cancel := context.WithTimeout(ctx, dialTime)

View File

@ -70,6 +70,26 @@ func (h *Handler) getLists(w http.ResponseWriter, r *http.Request) {
useTLS, _ := strconv.ParseBool(r.Header.Get("use_tls"))
restart, _ := strconv.ParseBool(r.FormValue("restart"))
// treat metadata as reflection headers
metadataHeader := r.Header.Get("Metadata")
metadataArr := strings.Split(metadataHeader, ",")
// construct array of strings with "key: value" form to be used in the reflection headers
var metadata []string
var metadataStr string
for i, m := range metadataArr {
i += 1
if isEven := i%2 == 0; isEven {
metadataStr = metadataStr + m
metadata = append(metadata, metadataStr)
metadataStr = ""
continue
}
metadataStr = fmt.Sprintf("%s:", m)
}
h.g.SetReflectHeaders(metadata...)
res, err := h.g.GetResource(context.Background(), host, !useTLS, restart)
if err != nil {
writeError(w, err)
@ -230,8 +250,8 @@ func (h *Handler) invokeFunction(w http.ResponseWriter, r *http.Request) {
var metadataStr string
for i, m := range metadataArr {
i += 1
if isEven := i % 2 == 0; isEven {
metadataStr = metadataStr+m
if isEven := i%2 == 0; isEven {
metadataStr = metadataStr + m
metadata = append(metadata, metadataStr)
metadataStr = ""
continue

View File

@ -17,6 +17,12 @@ $('#get-services').click(function(){
use_tls = "true"
}
// use metadata if there is any
ctxArr = [];
$(".ctx-metadata-input-field").each(function(index, val){
ctxArr.push($(val).text())
});
// determine whether the proto connection will use local proto or not
const use_proto = $('#local-proto').is(":checked");
@ -51,6 +57,9 @@ $('#get-services').click(function(){
beforeSend: function(xhr){
$('#choose-service').hide();
xhr.setRequestHeader('use_tls', use_tls);
if(ctxUse) {
xhr.setRequestHeader('Metadata', ctxArr);
}
$(this).html("Loading...");
show_loading();
},