MM-59283: Minor improvement in getClientConfig (#27825)

Overall, there is no bug here. But opportunities for
improvement.
- Used the maps.Clone function.
- Handled the error while writing to the network.

https://mattermost.atlassian.net/browse/MM-59283
```release-note
NONE
```
Этот коммит содержится в:
Agniva De Sarker
2024-08-01 21:09:58 +05:30
коммит произвёл GitHub
родитель 50ebf8cc13
Коммит 28939d84da
2 изменённых файлов: 6 добавлений и 10 удалений

Просмотреть файл

@@ -258,7 +258,9 @@ func getClientConfig(c *Context, w http.ResponseWriter, r *http.Request) {
config = c.App.Srv().Platform().ClientConfigWithComputed() config = c.App.Srv().Platform().ClientConfigWithComputed()
} }
w.Write([]byte(model.MapToJSON(config))) if err := json.NewEncoder(w).Encode(config); err != nil {
c.Logger.Warn("Error while writing response", mlog.Err(err))
}
} }
func getEnvironmentConfig(c *Context, w http.ResponseWriter, r *http.Request) { func getEnvironmentConfig(c *Context, w http.ResponseWriter, r *http.Request) {

Просмотреть файл

@@ -13,6 +13,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"maps"
"net/http" "net/http"
"reflect" "reflect"
"strconv" "strconv"
@@ -307,10 +308,7 @@ func (ps *PlatformService) EnsureAsymmetricSigningKey() error {
// LimitedClientConfigWithComputed gets the configuration in a format suitable for sending to the client. // LimitedClientConfigWithComputed gets the configuration in a format suitable for sending to the client.
func (ps *PlatformService) LimitedClientConfigWithComputed() map[string]string { func (ps *PlatformService) LimitedClientConfigWithComputed() map[string]string {
respCfg := map[string]string{} respCfg := maps.Clone(ps.LimitedClientConfig())
for k, v := range ps.LimitedClientConfig() {
respCfg[k] = v
}
// These properties are not configurable, but nevertheless represent configuration expected // These properties are not configurable, but nevertheless represent configuration expected
// by the client. // by the client.
@@ -321,10 +319,7 @@ func (ps *PlatformService) LimitedClientConfigWithComputed() map[string]string {
// ClientConfigWithComputed gets the configuration in a format suitable for sending to the client. // ClientConfigWithComputed gets the configuration in a format suitable for sending to the client.
func (ps *PlatformService) ClientConfigWithComputed() map[string]string { func (ps *PlatformService) ClientConfigWithComputed() map[string]string {
respCfg := map[string]string{} respCfg := maps.Clone(ps.ClientConfig())
for k, v := range ps.clientConfig.Load().(map[string]string) {
respCfg[k] = v
}
// These properties are not configurable, but nevertheless represent configuration expected // These properties are not configurable, but nevertheless represent configuration expected
// by the client. // by the client.
@@ -340,7 +335,6 @@ func (ps *PlatformService) ClientConfigWithComputed() map[string]string {
} else { } else {
respCfg["SchemaVersion"] = strconv.Itoa(ver) respCfg["SchemaVersion"] = strconv.Itoa(ver)
} }
return respCfg return respCfg
} }