Skip to content

Commit

Permalink
fix: antd version + persisitance error
Browse files Browse the repository at this point in the history
  • Loading branch information
gwleclerc committed Oct 2, 2023
1 parent 1122a6f commit bc141e1
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 84 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -110,4 +110,4 @@
"jest": {
"preset": "ts-jest"
}
}
}
25 changes: 15 additions & 10 deletions server/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"runtime"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 = "/"
Expand All @@ -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,
})

Expand Down
1 change: 0 additions & 1 deletion server/services/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
86 changes: 53 additions & 33 deletions server/services/persistence.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package services

import (
"io/ioutil"
"io"
"os"
"path/filepath"
"sync"
Expand All @@ -26,8 +26,9 @@ type Persistence interface {
StoreSessions(types.Sessions)
}

var mu sync.Mutex

type persistence struct {
mu sync.Mutex
persistenceDirectory string
}

Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -225,18 +235,20 @@ 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
}
return nil
}

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-- {
Expand All @@ -246,35 +258,43 @@ 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
}
return nil
}

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
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
"esModuleInterop": true
},
"include": ["client/**/*"],
"exclude": ["node_modules", "build"]
"exclude": ["node_modules", "build", "client/coverage"]
}
Loading

0 comments on commit bc141e1

Please sign in to comment.