InternalServerAPI에 추가 header를 인자로 받도록 추가
This commit is contained in:
51
server.go
51
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 {
|
func CallInternalServiceAPI[T any](url string, apitoken string, method string, data T, headers ...string) error {
|
||||||
reqURL := fmt.Sprintf("%s/api?call=%s", url, method)
|
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)
|
buff := new(bytes.Buffer)
|
||||||
enc := gob.NewEncoder(buff)
|
ct := tempHeader.Get("Content-Type")
|
||||||
enc.Encode(data)
|
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)
|
req, err := http.NewRequest("POST", reqURL, buff)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("MG-X-API-TOKEN", apitoken)
|
req.Header = tempHeader
|
||||||
req.Header.Set("Content-Type", "application/gob")
|
|
||||||
|
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if resp != nil && resp.Body != nil {
|
if resp != nil && resp.Body != nil {
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
@ -822,21 +834,32 @@ func CallInternalServiceAPI[T any](url string, apitoken string, method string, d
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func CallInternalServiceAPIAs[Tin any, Tout any](url string, apitoken string, method string, data Tin, out *Tout) error {
|
func CallInternalServiceAPIAs[Tin any, Tout any](url string, apitoken string, method string, data Tin, out *Tout, headers ...string) error {
|
||||||
reqURL := fmt.Sprintf("%s/api?call=%s", url, method)
|
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)
|
buff := new(bytes.Buffer)
|
||||||
enc := gob.NewEncoder(buff)
|
ct := tempHeader.Get("Content-Type")
|
||||||
enc.Encode(data)
|
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)
|
req, err := http.NewRequest("POST", reqURL, buff)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("MG-X-API-TOKEN", apitoken)
|
req.Header = tempHeader
|
||||||
req.Header.Set("Content-Type", "application/gob")
|
|
||||||
|
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
Reference in New Issue
Block a user