From bc141e1559926131ceffae91cc64dd2682b7c5f8 Mon Sep 17 00:00:00 2001 From: Gwendal Leclerc Date: Mon, 2 Oct 2023 22:44:05 +0200 Subject: [PATCH] fix: antd version + persisitance error --- package.json | 4 +- server/middlewares.go | 25 ++++++---- server/services/mocks.go | 1 - server/services/persistence.go | 86 +++++++++++++++++++++------------- tsconfig.json | 2 +- yarn.lock | 57 ++++++++-------------- 6 files changed, 91 insertions(+), 84 deletions(-) diff --git a/package.json b/package.json index 1ce8fd6..e22a598 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ } }, "dependencies": { - "@ant-design/icons": "^4.1.0", + "@ant-design/icons": "^4.x", "antd": "^4.16.9", "classnames": "^2.2.6", "codemirror": "^5.55.0", @@ -110,4 +110,4 @@ "jest": { "preset": "ts-jest" } -} +} \ No newline at end of file diff --git a/server/middlewares.go b/server/middlewares.go index 0671f45..af744ee 100644 --- a/server/middlewares.go +++ b/server/middlewares.go @@ -7,7 +7,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net" "net/http" "runtime" @@ -66,7 +65,7 @@ func HistoryMiddleware(s services.Mocks) echo.MiddlewareFunc { if err != nil { log.WithError(err).Error("Unable to uncompress response body") } else { - responseBytes, err = ioutil.ReadAll(r) + responseBytes, err = io.ReadAll(r) if err != nil { log.WithError(err).Error("Unable to read uncompressed response body") responseBytes = responseBody.Bytes() @@ -104,14 +103,9 @@ func HistoryMiddleware(s services.Mocks) echo.MiddlewareFunc { func loggerMiddleware() echo.MiddlewareFunc { return func(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { - req, res := c.Request(), c.Response() + req := c.Request() start := time.Now() - if err := next(c); err != nil { - c.Error(err) - } - end := time.Now() - p := req.URL.Path if p == "" { p = "/" @@ -123,18 +117,29 @@ func loggerMiddleware() echo.MiddlewareFunc { } headers := fmt.Sprintf("%+v", req.Header) + entry := log.WithFields(log.Fields{ "start": start.Format(time.RFC3339), - "end": end.Format(time.RFC3339), "remote-ip": c.RealIP(), "host": req.Host, "uri": req.RequestURI, "method": req.Method, "path": p, "headers": headers, + "bytes-in": bytesIn, + }) + entry.Debug("Handling request...") + + if err := next(c); err != nil { + c.Error(err) + } + + res := c.Response() + end := time.Now() + entry = entry.WithFields(log.Fields{ + "end": end.Format(time.RFC3339), "status": res.Status, "latency": end.Sub(start).String(), - "bytes-in": bytesIn, "bytes-out": res.Size, }) diff --git a/server/services/mocks.go b/server/services/mocks.go index ef4e473..00436d2 100644 --- a/server/services/mocks.go +++ b/server/services/mocks.go @@ -183,7 +183,6 @@ func (s *mocks) GetHistoryByPath(sessionID, filterPath string) (types.History, e } func (s *mocks) NewSession(name string) *types.Session { - if strings.TrimSpace(name) == "" { name = fmt.Sprintf("Session #%d", len(s.sessions)+1) } diff --git a/server/services/persistence.go b/server/services/persistence.go index bca829c..7a5584c 100644 --- a/server/services/persistence.go +++ b/server/services/persistence.go @@ -1,7 +1,7 @@ package services import ( - "io/ioutil" + "io" "os" "path/filepath" "sync" @@ -26,8 +26,9 @@ type Persistence interface { StoreSessions(types.Sessions) } +var mu sync.Mutex + type persistence struct { - mu sync.Mutex persistenceDirectory string } @@ -39,8 +40,8 @@ func NewPersistence(persistenceDirectory string) Persistence { } func (p *persistence) StoreMocks(sessionID string, mocks types.Mocks) { - p.mu.Lock() - defer p.mu.Unlock() + mu.Lock() + defer mu.Unlock() if p.persistenceDirectory == "" { return } @@ -56,8 +57,8 @@ func (p *persistence) StoreMocks(sessionID string, mocks types.Mocks) { } func (p *persistence) StoreHistory(sessionID string, history types.History) { - p.mu.Lock() - defer p.mu.Unlock() + mu.Lock() + defer mu.Unlock() if p.persistenceDirectory == "" { return } @@ -73,8 +74,8 @@ func (p *persistence) StoreHistory(sessionID string, history types.History) { } func (p *persistence) StoreSession(summary []types.SessionSummary, session *types.Session) { - p.mu.Lock() - defer p.mu.Unlock() + mu.Lock() + defer mu.Unlock() if p.persistenceDirectory == "" { return } @@ -99,18 +100,14 @@ func (p *persistence) StoreSession(summary []types.SessionSummary, session *type } func (p *persistence) StoreSessions(sessions types.Sessions) { - p.mu.Lock() - defer p.mu.Unlock() + mu.Lock() + defer mu.Unlock() if p.persistenceDirectory == "" { return } - if err := p.cleanAll(); err != nil { - log.Error("unable to clean directory: ", err) - return - } var sessionsGroup errgroup.Group - for _, session := range sessions { - session := session + for _, ses := range sessions { + session := *ses sessionsGroup.Go(func() error { if err := p.createSessionDirectory(session.ID); err != nil { return err @@ -134,11 +131,14 @@ func (p *persistence) StoreSessions(sessions types.Sessions) { if err := sessionsGroup.Wait(); err != nil { log.Error("unable to store sessions: ", err) } + if err := p.cleanOutdatedSessions(sessions); err != nil { + log.Error("unable to clean outdated sessions: ", err) + } } func (p *persistence) LoadSessions() (types.Sessions, error) { - p.mu.Lock() - defer p.mu.Unlock() + mu.Lock() + defer mu.Unlock() if p.persistenceDirectory == "" { return nil, nil } @@ -147,10 +147,13 @@ func (p *persistence) LoadSessions() (types.Sessions, error) { } file, err := os.Open(filepath.Join(p.persistenceDirectory, sessionsFileName)) if err != nil { + if err == os.ErrNotExist { + return types.Sessions{}, nil + } return nil, err } defer file.Close() - bytes, err := ioutil.ReadAll(file) + bytes, err := io.ReadAll(file) if err != nil { return nil, err } @@ -161,15 +164,16 @@ func (p *persistence) LoadSessions() (types.Sessions, error) { } var sessionsGroup errgroup.Group var sessionsLock sync.Mutex - for _, session := range sessions { - session := session + for _, ses := range sessions { + session := *ses sessionsGroup.Go(func() error { historyFile, err := os.Open(filepath.Join(p.persistenceDirectory, session.ID, historyFileName)) if err != nil { + log.WithError(err).Errorf("Unable to open history file for session %q", session.ID) return err } defer historyFile.Close() - bytes, err := ioutil.ReadAll(historyFile) + bytes, err := io.ReadAll(historyFile) if err != nil { return err } @@ -186,10 +190,11 @@ func (p *persistence) LoadSessions() (types.Sessions, error) { sessionsGroup.Go(func() error { mocksFile, err := os.Open(filepath.Join(p.persistenceDirectory, session.ID, mocksFileName)) if err != nil { + log.WithError(err).Errorf("Unable to open mocks file for session %q", session.ID) return err } defer mocksFile.Close() - bytes, err := ioutil.ReadAll(mocksFile) + bytes, err := io.ReadAll(mocksFile) if err != nil { return err } @@ -213,10 +218,15 @@ func (p *persistence) LoadSessions() (types.Sessions, error) { if err := sessionsGroup.Wait(); err != nil { return nil, err } + if err := p.cleanOutdatedSessions(sessions); err != nil { + return nil, err + } + return sessions, nil } func (p *persistence) createSessionDirectory(sessionID string) error { + log.Debugf("Create directory for session %q", sessionID) sessionDirectory := filepath.Join(p.persistenceDirectory, sessionID) if err := os.MkdirAll(sessionDirectory, os.ModePerm); err != nil && !os.IsExist(err) { return err @@ -225,11 +235,12 @@ func (p *persistence) createSessionDirectory(sessionID string) error { } func (p *persistence) persistHistory(sessionID string, h types.History) error { + log.Debugf("Persist history for session %q", sessionID) history, err := yaml.Marshal(h) if err != nil { return err } - err = ioutil.WriteFile(filepath.Join(p.persistenceDirectory, sessionID, historyFileName), history, os.ModePerm) + err = os.WriteFile(filepath.Join(p.persistenceDirectory, sessionID, historyFileName), history, os.ModePerm) if err != nil { return err } @@ -237,6 +248,7 @@ func (p *persistence) persistHistory(sessionID string, h types.History) error { } func (p *persistence) persistMocks(sessionID string, m types.Mocks) error { + log.Debugf("Persist mocks for session %q", sessionID) // we need to reverse mocks before storage in order to have a reusable mocks file as mocks are stored as a stack orderedMocks := make(types.Mocks, 0, len(m)) for i := len(m) - 1; i >= 0; i-- { @@ -246,7 +258,7 @@ func (p *persistence) persistMocks(sessionID string, m types.Mocks) error { if err != nil { return err } - err = ioutil.WriteFile(filepath.Join(p.persistenceDirectory, sessionID, mocksFileName), mocks, os.ModePerm) + err = os.WriteFile(filepath.Join(p.persistenceDirectory, sessionID, mocksFileName), mocks, os.ModePerm) if err != nil { return err } @@ -254,27 +266,35 @@ func (p *persistence) persistMocks(sessionID string, m types.Mocks) error { } func (p *persistence) persistSessionsSummary(summary []types.SessionSummary) error { + log.Debug("Persist sessions summary") sessions, err := yaml.Marshal(summary) if err != nil { return err } - err = ioutil.WriteFile(filepath.Join(p.persistenceDirectory, sessionsFileName), sessions, os.ModePerm) + err = os.WriteFile(filepath.Join(p.persistenceDirectory, sessionsFileName), sessions, os.ModePerm) if err != nil { return err } return nil } -func (p *persistence) cleanAll() error { - if err := os.MkdirAll(p.persistenceDirectory, os.ModePerm); err != nil && !os.IsExist(err) { - return err - } - files, err := ioutil.ReadDir(p.persistenceDirectory) +func (p *persistence) cleanOutdatedSessions(sessions types.Sessions) error { + sessionsIDs := make(map[string]bool, len(sessions)) + for _, ses := range sessions { + session := *ses + sessionsIDs[session.ID] = true + } + log.Debug("Clean old sessions") + entries, err := os.ReadDir(p.persistenceDirectory) if err != nil { return err } - for _, file := range files { - os.RemoveAll(filepath.Join(p.persistenceDirectory, file.Name())) + for _, entry := range entries { + if entry.IsDir() && !sessionsIDs[entry.Name()] { + path := filepath.Join(p.persistenceDirectory, entry.Name()) + log.Debugf("Removing directory: %q", path) + os.RemoveAll(path) + } } return nil } diff --git a/tsconfig.json b/tsconfig.json index 033b22d..98151e1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -20,5 +20,5 @@ "esModuleInterop": true }, "include": ["client/**/*"], - "exclude": ["node_modules", "build"] + "exclude": ["node_modules", "build", "client/coverage"] } diff --git a/yarn.lock b/yarn.lock index d52ccab..09628cd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -21,13 +21,6 @@ html-tags "^3.1.0" svg-tags "^1.0.0" -"@ant-design/colors@^3.1.0": - version "3.2.2" - resolved "https://registry.yarnpkg.com/@ant-design/colors/-/colors-3.2.2.tgz#5ad43d619e911f3488ebac303d606e66a8423903" - integrity sha512-YKgNbG2dlzqMhA9NtI3/pbY16m3Yl/EeWBRa+lB1X1YaYxHrxNexiQYCLTWO/uDvAjLFMEDU+zR901waBtMtjQ== - dependencies: - tinycolor2 "^1.4.1" - "@ant-design/colors@^6.0.0": version "6.0.0" resolved "https://registry.yarnpkg.com/@ant-design/colors/-/colors-6.0.0.tgz#9b9366257cffcc47db42b9d0203bb592c13c0298" @@ -40,17 +33,10 @@ resolved "https://registry.yarnpkg.com/@ant-design/icons-svg/-/icons-svg-4.1.0.tgz#480b025f4b20ef7fe8f47d4a4846e4fee84ea06c" integrity sha512-Fi03PfuUqRs76aI3UWYpP864lkrfPo0hluwGqh7NJdLhvH4iRDc3jbJqZIvRDLHKbXrvAfPPV3+zjUccfFvWOQ== -"@ant-design/icons@^4.1.0": - version "4.2.1" - resolved "https://registry.yarnpkg.com/@ant-design/icons/-/icons-4.2.1.tgz#6f3ea5d98ab782072e4e9cbb70f25e4403ae1a6b" - integrity sha512-245ZI40MOr5GGws+sNSiJIRRoEf/J2xvPSMgwRYf3bv8mVGQZ6XTQI/OMeV16KtiSZ3D+mBKXVYSBz2fhigOXQ== - dependencies: - "@ant-design/colors" "^3.1.0" - "@ant-design/icons-svg" "^4.0.0" - "@babel/runtime" "^7.10.1" - classnames "^2.2.6" - insert-css "^2.0.0" - rc-util "^5.0.1" +"@ant-design/icons-svg@^4.3.0": + version "4.3.1" + resolved "https://registry.yarnpkg.com/@ant-design/icons-svg/-/icons-svg-4.3.1.tgz#4b2f65a17d4d32b526baa6414aca2117382bf8da" + integrity sha512-4QBZg8ccyC6LPIRii7A0bZUk3+lEDCLnhB+FVsflGdcWPPmV+j3fire4AwwoqHV/BibgvBmR9ZIo4s867smv+g== "@ant-design/icons@^4.6.2": version "4.6.2" @@ -63,6 +49,18 @@ classnames "^2.2.6" rc-util "^5.9.4" +"@ant-design/icons@^4.x": + version "4.8.1" + resolved "https://registry.yarnpkg.com/@ant-design/icons/-/icons-4.8.1.tgz#44f6c81f609811d68d48a123eb5dcc477f8fbcb7" + integrity sha512-JRAuiqllnMsiZIO8OvBOeFconprC3cnMpJ9MvXrHh+H5co9rlg8/aSHQfLf5jKKe18lUgRaIwC2pz8YxH9VuCA== + dependencies: + "@ant-design/colors" "^6.0.0" + "@ant-design/icons-svg" "^4.3.0" + "@babel/runtime" "^7.11.2" + classnames "^2.2.6" + lodash "^4.17.15" + rc-util "^5.9.4" + "@ant-design/react-slick@~0.28.1": version "0.28.3" resolved "https://registry.yarnpkg.com/@ant-design/react-slick/-/react-slick-0.28.3.tgz#ad5cf1cf50363c1a3842874d69d0ce1f26696e71" @@ -4127,15 +4125,10 @@ caniuse-api@^3.0.0: lodash.memoize "^4.1.2" lodash.uniq "^4.5.0" -caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001109, caniuse-lite@^1.0.30001111: - version "1.0.30001271" - resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001271.tgz" - integrity sha512-BBruZFWmt3HFdVPS8kceTBIguKxu4f99n5JNp06OlPD/luoAMIaIK5ieV5YjnBLH3Nysai9sxj9rpJj4ZisXOA== - -caniuse-lite@^1.0.30001265: - version "1.0.30001269" - resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001269.tgz" - integrity sha512-UOy8okEVs48MyHYgV+RdW1Oiudl1H6KolybD6ZquD0VcrPSgj25omXO1S7rDydjpqaISCwA8Pyx+jUQKZwWO5w== +caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001109, caniuse-lite@^1.0.30001111, caniuse-lite@^1.0.30001265: + version "1.0.30001542" + resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001542.tgz" + integrity sha512-UrtAXVcj1mvPBFQ4sKd38daP8dEcXXr5sQe6QNNinaPd0iA/cxg9/l3VrSdL73jgw5sKyuQ6jNgiKO12W3SsVA== capture-exit@^2.0.0: version "2.0.0" @@ -7774,11 +7767,6 @@ ini@^1.3.5, ini@~1.3.0: resolved "https://registry.yarnpkg.com/ini/-/ini-1.3.8.tgz#a29da425b48806f34767a4efce397269af28432c" integrity sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew== -insert-css@^2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/insert-css/-/insert-css-2.0.0.tgz#eb5d1097b7542f4c79ea3060d3aee07d053880f4" - integrity sha1-610Ql7dUL0x56jBg067gfQU4gPQ= - internal-ip@^4.3.0: version "4.3.0" resolved "https://registry.yarnpkg.com/internal-ip/-/internal-ip-4.3.0.tgz#845452baad9d2ca3b69c635a137acb9a0dad0907" @@ -13603,11 +13591,6 @@ tiny-warning@^1.0.0, tiny-warning@^1.0.3: resolved "https://registry.yarnpkg.com/tiny-warning/-/tiny-warning-1.0.3.tgz#94a30db453df4c643d0fd566060d60a875d84754" integrity sha512-lBN9zLN/oAf68o3zNXYrdCt1kP8WsiGW8Oo2ka41b2IM5JL/S1CTyX1rW0mb/zSuJun0ZUrDxx4sqvYS2FWzPA== -tinycolor2@^1.4.1: - version "1.4.1" - resolved "https://registry.yarnpkg.com/tinycolor2/-/tinycolor2-1.4.1.tgz#f4fad333447bc0b07d4dc8e9209d8f39a8ac77e8" - integrity sha1-9PrTM0R7wLB9TcjpIJ2POaisd+g= - tmpl@1.0.x: version "1.0.4" resolved "https://registry.yarnpkg.com/tmpl/-/tmpl-1.0.4.tgz#23640dd7b42d00433911140820e5cf440e521dd1"