Improve API4 initialization (#18680)
* Improve API4 initialization - Refactored openGraphDataCache to be inside app layer. - Moved the cache instance from global variable to be inside server. - Moved out the app instantiation from the global commands package to be instantiated on every call. Only the server instance is passed. - Moved InitLocal to be called from inside Init. ```release-note NONE ``` * Remove commented line ```release-note NONE ```
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
43a99a5783
Коммит
c27814393d
@@ -5,30 +5,13 @@ package api4
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v6/model"
|
||||
"github.com/mattermost/mattermost-server/v6/services/cache"
|
||||
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
||||
)
|
||||
|
||||
const OpenGraphMetadataCacheSize = 10000
|
||||
|
||||
var openGraphDataCache = cache.NewLRU(cache.LRUOptions{
|
||||
Size: OpenGraphMetadataCacheSize,
|
||||
})
|
||||
|
||||
func (api *API) InitOpenGraph() {
|
||||
api.BaseRoutes.OpenGraph.Handle("", api.APISessionRequired(getOpenGraphMetadata)).Methods("POST")
|
||||
|
||||
// Dump the image cache if the proxy settings have changed. (need switch URLs to the correct proxy)
|
||||
api.app.AddConfigListener(func(before, after *model.Config) {
|
||||
if (before.ImageProxySettings.Enable != after.ImageProxySettings.Enable) ||
|
||||
(before.ImageProxySettings.ImageProxyType != after.ImageProxySettings.ImageProxyType) ||
|
||||
(before.ImageProxySettings.RemoteImageProxyURL != after.ImageProxySettings.RemoteImageProxyURL) ||
|
||||
(before.ImageProxySettings.RemoteImageProxyOptions != after.ImageProxySettings.RemoteImageProxyOptions) {
|
||||
openGraphDataCache.Purge()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func getOpenGraphMetadata(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
@@ -46,20 +29,13 @@ func getOpenGraphMetadata(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
var ogJSONGeneric []byte
|
||||
err := openGraphDataCache.Get(url, &ogJSONGeneric)
|
||||
if err == nil {
|
||||
w.Write(ogJSONGeneric)
|
||||
return
|
||||
}
|
||||
|
||||
og := c.App.GetOpenGraphMetadata(url)
|
||||
ogJSON, err := og.ToJSON()
|
||||
openGraphDataCache.SetWithExpiry(url, ogJSON, 1*time.Hour)
|
||||
buf, err := c.App.GetOpenGraphMetadata(url)
|
||||
if err != nil {
|
||||
mlog.Warn("GetOpenGraphMetadata request failed",
|
||||
mlog.String("requestURL", url),
|
||||
mlog.Err(err))
|
||||
w.Write([]byte(`{"url": ""}`))
|
||||
return
|
||||
}
|
||||
|
||||
w.Write(ogJSON)
|
||||
w.Write(buf)
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user