From 6ad727c72886771b1bb10cd0a945f57165267f07 Mon Sep 17 00:00:00 2001 From: mountain Date: Tue, 21 Nov 2023 16:52:06 +0900 Subject: [PATCH] =?UTF-8?q?InternalServerAPI=EC=97=90=20=EC=B6=94=EA=B0=80?= =?UTF-8?q?=20header=EB=A5=BC=20=EC=9D=B8=EC=9E=90=EB=A1=9C=20=EB=B0=9B?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server.go | 51 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/server.go b/server.go index 959ce7b..5578566 100644 --- a/server.go +++ b/server.go @@ -791,22 +791,34 @@ func (hc *HttpApiBroker) call(funcname string, w http.ResponseWriter, r *http.Re } } -func CallInternalServiceAPI[T any](url string, apitoken string, method string, data T) error { - reqURL := fmt.Sprintf("%s/api?call=%s", url, method) +func CallInternalServiceAPI[T any](url string, apitoken string, method string, data T, headers ...string) error { + tempHeader := make(http.Header) + tempHeader.Set("MG-X-API-TOKEN", apitoken) + tempHeader.Set("Content-Type", "application/gob") + + for i := 1; i < len(headers); i += 2 { + tempHeader.Set(headers[i-1], headers[i]) + } buff := new(bytes.Buffer) - enc := gob.NewEncoder(buff) - enc.Encode(data) + ct := tempHeader.Get("Content-Type") + if ct == "application/gob" { + enc := gob.NewEncoder(buff) + enc.Encode(data) + } else if ct == "application/json" { + enc := json.NewEncoder(buff) + enc.Encode(data) + } + reqURL := fmt.Sprintf("%s/api?call=%s", url, method) req, err := http.NewRequest("POST", reqURL, buff) if err != nil { return err } - req.Header.Set("MG-X-API-TOKEN", apitoken) - req.Header.Set("Content-Type", "application/gob") - + req.Header = tempHeader resp, err := http.DefaultClient.Do(req) + defer func() { if resp != nil && resp.Body != nil { resp.Body.Close() @@ -822,21 +834,32 @@ func CallInternalServiceAPI[T any](url string, apitoken string, method string, d return err } -func CallInternalServiceAPIAs[Tin any, Tout any](url string, apitoken string, method string, data Tin, out *Tout) error { - reqURL := fmt.Sprintf("%s/api?call=%s", url, method) +func CallInternalServiceAPIAs[Tin any, Tout any](url string, apitoken string, method string, data Tin, out *Tout, headers ...string) error { + tempHeader := make(http.Header) + tempHeader.Set("MG-X-API-TOKEN", apitoken) + tempHeader.Set("Content-Type", "application/gob") + + for i := 1; i < len(headers); i += 2 { + tempHeader.Set(headers[i-1], headers[i]) + } buff := new(bytes.Buffer) - enc := gob.NewEncoder(buff) - enc.Encode(data) + ct := tempHeader.Get("Content-Type") + if ct == "application/gob" { + enc := gob.NewEncoder(buff) + enc.Encode(data) + } else if ct == "application/json" { + enc := json.NewEncoder(buff) + enc.Encode(data) + } + reqURL := fmt.Sprintf("%s/api?call=%s", url, method) req, err := http.NewRequest("POST", reqURL, buff) if err != nil { return err } - req.Header.Set("MG-X-API-TOKEN", apitoken) - req.Header.Set("Content-Type", "application/gob") - + req.Header = tempHeader resp, err := http.DefaultClient.Do(req) if err != nil { return err