diff options
author | Till <2353100+S7evinK@users.noreply.github.com> | 2023-02-14 12:47:47 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-14 12:47:47 +0100 |
commit | 11d9b9db0e96c51c1430d451d23cf5ae9f36e4ee (patch) | |
tree | a0837bfa69051295b76140e3940a45fc61854cab /internal/httputil | |
parent | cc59879faa57cac043cf5f1585773b301994bebf (diff) |
Remove polylith/API mode (#2967)
This removes most of the code used for polylith/API mode.
This removes the `/api` internal endpoints entirely.
Binary size change roughly 5%:
```
51437560 Feb 13 10:15 dendrite-monolith-server # old
48759008 Feb 13 10:15 dendrite-monolith-server # new
```
Diffstat (limited to 'internal/httputil')
-rw-r--r-- | internal/httputil/http.go | 93 | ||||
-rw-r--r-- | internal/httputil/internalapi.go | 93 | ||||
-rw-r--r-- | internal/httputil/paths.go | 1 |
3 files changed, 0 insertions, 187 deletions
diff --git a/internal/httputil/http.go b/internal/httputil/http.go deleted file mode 100644 index ad26de51..00000000 --- a/internal/httputil/http.go +++ /dev/null @@ -1,93 +0,0 @@ -// Copyright 2020 The Matrix.org Foundation C.I.C. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package httputil - -import ( - "bytes" - "context" - "encoding/json" - "fmt" - "io" - "net/http" - "net/url" - "strings" - - "github.com/opentracing/opentracing-go" - "github.com/opentracing/opentracing-go/ext" -) - -// PostJSON performs a POST request with JSON on an internal HTTP API. -// The error will match the errtype if returned from the remote API, or -// will be a different type if there was a problem reaching the API. -func PostJSON[reqtype, restype any, errtype error]( - ctx context.Context, span opentracing.Span, httpClient *http.Client, - apiURL string, request *reqtype, response *restype, -) error { - jsonBytes, err := json.Marshal(request) - if err != nil { - return err - } - - parsedAPIURL, err := url.Parse(apiURL) - if err != nil { - return err - } - - parsedAPIURL.Path = InternalPathPrefix + strings.TrimLeft(parsedAPIURL.Path, "/") - apiURL = parsedAPIURL.String() - - req, err := http.NewRequest(http.MethodPost, apiURL, bytes.NewReader(jsonBytes)) - if err != nil { - return err - } - - // Mark the span as being an RPC client. - ext.SpanKindRPCClient.Set(span) - carrier := opentracing.HTTPHeadersCarrier(req.Header) - tracer := opentracing.GlobalTracer() - - if err = tracer.Inject(span.Context(), opentracing.HTTPHeaders, carrier); err != nil { - return err - } - - req.Header.Set("Content-Type", "application/json") - - res, err := httpClient.Do(req.WithContext(ctx)) - if res != nil { - defer (func() { err = res.Body.Close() })() - } - if err != nil { - return err - } - var body []byte - body, err = io.ReadAll(res.Body) - if err != nil { - return err - } - if res.StatusCode != http.StatusOK { - if len(body) == 0 { - return fmt.Errorf("HTTP %d from %s (no response body)", res.StatusCode, apiURL) - } - var reserr errtype - if err = json.Unmarshal(body, &reserr); err != nil { - return fmt.Errorf("HTTP %d from %s - %w", res.StatusCode, apiURL, err) - } - return reserr - } - if err = json.Unmarshal(body, response); err != nil { - return fmt.Errorf("json.Unmarshal: %w", err) - } - return nil -} diff --git a/internal/httputil/internalapi.go b/internal/httputil/internalapi.go deleted file mode 100644 index 22f436e3..00000000 --- a/internal/httputil/internalapi.go +++ /dev/null @@ -1,93 +0,0 @@ -// Copyright 2022 The Matrix.org Foundation C.I.C. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package httputil - -import ( - "context" - "encoding/json" - "fmt" - "net/http" - "reflect" - - "github.com/matrix-org/util" - "github.com/opentracing/opentracing-go" -) - -type InternalAPIError struct { - Type string - Message string -} - -func (e InternalAPIError) Error() string { - return fmt.Sprintf("internal API returned %q error: %s", e.Type, e.Message) -} - -func MakeInternalRPCAPI[reqtype, restype any](metricsName string, enableMetrics bool, f func(context.Context, *reqtype, *restype) error) http.Handler { - return MakeInternalAPI(metricsName, enableMetrics, func(req *http.Request) util.JSONResponse { - var request reqtype - var response restype - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.MessageResponse(http.StatusBadRequest, err.Error()) - } - if err := f(req.Context(), &request, &response); err != nil { - return util.JSONResponse{ - Code: http.StatusInternalServerError, - JSON: &InternalAPIError{ - Type: reflect.TypeOf(err).String(), - Message: fmt.Sprintf("%s", err), - }, - } - } - return util.JSONResponse{ - Code: http.StatusOK, - JSON: &response, - } - }) -} - -func MakeInternalProxyAPI[reqtype, restype any](metricsName string, enableMetrics bool, f func(context.Context, *reqtype) (*restype, error)) http.Handler { - return MakeInternalAPI(metricsName, enableMetrics, func(req *http.Request) util.JSONResponse { - var request reqtype - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.MessageResponse(http.StatusBadRequest, err.Error()) - } - response, err := f(req.Context(), &request) - if err != nil { - return util.JSONResponse{ - Code: http.StatusInternalServerError, - JSON: err, - } - } - return util.JSONResponse{ - Code: http.StatusOK, - JSON: response, - } - }) -} - -func CallInternalRPCAPI[reqtype, restype any](name, url string, client *http.Client, ctx context.Context, request *reqtype, response *restype) error { - span, ctx := opentracing.StartSpanFromContext(ctx, name) - defer span.Finish() - - return PostJSON[reqtype, restype, InternalAPIError](ctx, span, client, url, request, response) -} - -func CallInternalProxyAPI[reqtype, restype any, errtype error](name, url string, client *http.Client, ctx context.Context, request *reqtype) (restype, error) { - span, ctx := opentracing.StartSpanFromContext(ctx, name) - defer span.Finish() - - var response restype - return response, PostJSON[reqtype, restype, errtype](ctx, span, client, url, request, &response) -} diff --git a/internal/httputil/paths.go b/internal/httputil/paths.go index 62eff041..d0687542 100644 --- a/internal/httputil/paths.go +++ b/internal/httputil/paths.go @@ -21,7 +21,6 @@ const ( PublicMediaPathPrefix = "/_matrix/media/" PublicStaticPath = "/_matrix/static/" PublicWellKnownPrefix = "/.well-known/matrix/" - InternalPathPrefix = "/api/" DendriteAdminPathPrefix = "/_dendrite/" SynapseAdminPathPrefix = "/_synapse/" ) |