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 | |
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')
-rw-r--r-- | internal/httputil/http.go | 93 | ||||
-rw-r--r-- | internal/httputil/internalapi.go | 93 | ||||
-rw-r--r-- | internal/httputil/paths.go | 1 | ||||
-rw-r--r-- | internal/log.go | 4 | ||||
-rw-r--r-- | internal/log_unix.go | 10 | ||||
-rw-r--r-- | internal/log_windows.go | 4 | ||||
-rw-r--r-- | internal/transactionrequest_test.go | 6 |
7 files changed, 12 insertions, 199 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/" ) diff --git a/internal/log.go b/internal/log.go index 9e8656c5..8fe98f20 100644 --- a/internal/log.go +++ b/internal/log.go @@ -129,9 +129,9 @@ func checkFileHookParams(params map[string]interface{}) { } // Add a new FSHook to the logger. Each component will log in its own file -func setupFileHook(hook config.LogrusHook, level logrus.Level, componentName string) { +func setupFileHook(hook config.LogrusHook, level logrus.Level) { dirPath := (hook.Params["path"]).(string) - fullPath := filepath.Join(dirPath, componentName+".log") + fullPath := filepath.Join(dirPath, "dendrite.log") if err := os.MkdirAll(path.Dir(fullPath), os.ModePerm); err != nil { logrus.Fatalf("Couldn't create directory %s: %q", path.Dir(fullPath), err) diff --git a/internal/log_unix.go b/internal/log_unix.go index 85942704..3f15063d 100644 --- a/internal/log_unix.go +++ b/internal/log_unix.go @@ -31,7 +31,7 @@ import ( // SetupHookLogging configures the logging hooks defined in the configuration. // If something fails here it means that the logging was improperly configured, // so we just exit with the error -func SetupHookLogging(hooks []config.LogrusHook, componentName string) { +func SetupHookLogging(hooks []config.LogrusHook) { levelLogAddedMu.Lock() defer levelLogAddedMu.Unlock() for _, hook := range hooks { @@ -50,10 +50,10 @@ func SetupHookLogging(hooks []config.LogrusHook, componentName string) { switch hook.Type { case "file": checkFileHookParams(hook.Params) - setupFileHook(hook, level, componentName) + setupFileHook(hook, level) case "syslog": checkSyslogHookParams(hook.Params) - setupSyslogHook(hook, level, componentName) + setupSyslogHook(hook, level) case "std": setupStdLogHook(level) default: @@ -94,8 +94,8 @@ func setupStdLogHook(level logrus.Level) { stdLevelLogAdded[level] = true } -func setupSyslogHook(hook config.LogrusHook, level logrus.Level, componentName string) { - syslogHook, err := lSyslog.NewSyslogHook(hook.Params["protocol"].(string), hook.Params["address"].(string), syslog.LOG_INFO, componentName) +func setupSyslogHook(hook config.LogrusHook, level logrus.Level) { + syslogHook, err := lSyslog.NewSyslogHook(hook.Params["protocol"].(string), hook.Params["address"].(string), syslog.LOG_INFO, "dendrite") if err == nil { logrus.AddHook(&logLevelHook{level, syslogHook}) } diff --git a/internal/log_windows.go b/internal/log_windows.go index 39562328..e1f0098a 100644 --- a/internal/log_windows.go +++ b/internal/log_windows.go @@ -22,7 +22,7 @@ import ( // SetupHookLogging configures the logging hooks defined in the configuration. // If something fails here it means that the logging was improperly configured, // so we just exit with the error -func SetupHookLogging(hooks []config.LogrusHook, componentName string) { +func SetupHookLogging(hooks []config.LogrusHook) { logrus.SetReportCaller(true) for _, hook := range hooks { // Check we received a proper logging level @@ -40,7 +40,7 @@ func SetupHookLogging(hooks []config.LogrusHook, componentName string) { switch hook.Type { case "file": checkFileHookParams(hook.Params) - setupFileHook(hook, level, componentName) + setupFileHook(hook, level) default: logrus.Fatalf("Unrecognised logging hook type: %s", hook.Type) } diff --git a/internal/transactionrequest_test.go b/internal/transactionrequest_test.go index dd1bd350..93c6fb6f 100644 --- a/internal/transactionrequest_test.go +++ b/internal/transactionrequest_test.go @@ -198,8 +198,8 @@ func TestProcessTransactionRequestPDUSendFail(t *testing.T) { func createTransactionWithEDU(ctx *process.ProcessContext, edus []gomatrixserverlib.EDU) (TxnReq, nats.JetStreamContext, *config.Dendrite) { cfg := &config.Dendrite{} cfg.Defaults(config.DefaultOpts{ - Generate: true, - Monolithic: true, + Generate: true, + SingleDatabase: true, }) cfg.Global.JetStream.InMemory = true natsInstance := &jetstream.NATSInstance{} @@ -647,7 +647,7 @@ func init() { } type testRoomserverAPI struct { - rsAPI.RoomserverInternalAPITrace + rsAPI.RoomserverInternalAPI inputRoomEvents []rsAPI.InputRoomEvent queryStateAfterEvents func(*rsAPI.QueryStateAfterEventsRequest) rsAPI.QueryStateAfterEventsResponse queryEventsByID func(req *rsAPI.QueryEventsByIDRequest) rsAPI.QueryEventsByIDResponse |