Skip to content

Commit

Permalink
change init folder config
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Ratier committed Feb 20, 2024
1 parent 903445f commit 23be03a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ func parseConfig() (c config.Config) {
flag.StringVar(&c.StaticFiles, "static-files", "client", "Location of the static files to serve (index.html, etc.)")
flag.IntVar(&c.HistoryMaxRetention, "history-retention", 0, "Maximum number of calls to keep in the history per session (0 = no limit)")
flag.StringVar(&c.PersistenceDirectory, "persistence-directory", "", "If defined, the directory where the sessions will be synchronized")
flag.StringVar(&c.InitFilePath, "initialization-file", "", "If defined, mocks in this file will be added to current sessions")
flag.StringVar(&c.InitFile, "initialization-file", "", "If defined, mocks in this file will be added to current sessions.")
flag.StringVar(&c.InitFolder, "initialization-folder", "init/", "Folder where initialization file will be loaded from")
flag.BoolVar(&c.TLSEnable, "tls-enable", false, "Enable TLS using the provided certificate")
flag.StringVar(&c.TLSCertFile, "tls-cert-file", "/etc/smocker/tls/certs/cert.pem", "Path to TLS certificate file ")
flag.StringVar(&c.TLSKeyFile, "tls-private-key-file", "/etc/smocker/tls/private/key.pem", "Path to TLS key file")
Expand Down
3 changes: 2 additions & 1 deletion server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ type Config struct {
StaticFiles string
HistoryMaxRetention int
PersistenceDirectory string
InitFilePath string
InitFile string
InitFolder string
TLSEnable bool
TLSCertFile string
TLSKeyFile string
Expand Down
2 changes: 1 addition & 1 deletion server/mock_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewMockServer(cfg config.Config) (*http.Server, services.Mocks) {
if err != nil {
log.Error("Unable to load sessions: ", err)
}
mockServices := services.NewMocks(sessions, cfg.HistoryMaxRetention, persistence, cfg.InitFilePath)
mockServices := services.NewMocks(sessions, cfg.HistoryMaxRetention, persistence, cfg.InitFile, cfg.InitFolder)

mockServerEngine.HideBanner = true
mockServerEngine.HidePort = true
Expand Down
19 changes: 12 additions & 7 deletions server/services/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
Expand Down Expand Up @@ -42,23 +43,26 @@ type mocks struct {
mu sync.Mutex
historyRetention int
persistence Persistence
initFilePath string
initFile string
initFolder string
}

func NewMocks(sessions types.Sessions, historyRetention int, persistence Persistence, initFilePath string) Mocks {
func NewMocks(sessions types.Sessions, historyRetention int, persistence Persistence, initFolder string, initFilePath string) Mocks {
s := &mocks{
sessions: types.Sessions{},
historyRetention: historyRetention,
persistence: persistence,
initFilePath: initFilePath,
initFile: initFilePath,
initFolder: initFolder,
}
if sessions != nil {
s.sessions = sessions
}
return s
}

func (s *mocks) GetMocksFromInitializationFile(filePath string) (*types.Mocks, error) {
func (s *mocks) GetMocksFromInitializationFile(folder, file string) (*types.Mocks, error) {
filePath := filepath.Join(folder, file)
mocksFile, err := os.Open(filePath)
if err != nil {
return nil, err
Expand Down Expand Up @@ -241,10 +245,11 @@ func (s *mocks) NewSession(name string) *types.Session {
s.mu.Lock()
defer s.mu.Unlock()

if s.initFilePath != "" {
fmt.Printf("Init\n\ns.initFilePath: %s\n", s.initFilePath)
if s.initFile != "" {
fmt.Printf("Init\n\ns.initFile: %s\n", s.initFile)
fmt.Printf("Init\n\ns.initFolder: %s\n", s.initFolder)

initMocks, err := s.GetMocksFromInitializationFile(s.initFilePath)
initMocks, err := s.GetMocksFromInitializationFile(s.initFolder, s.initFile)
if err != nil {
fmt.Printf("Error is: %s\n\n", err)
return nil
Expand Down

0 comments on commit 23be03a

Please sign in to comment.