diff --git a/main.go b/main.go index e14ffa5..75be5ad 100644 --- a/main.go +++ b/main.go @@ -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") diff --git a/server/config/config.go b/server/config/config.go index 740e5cf..3529fd2 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -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 diff --git a/server/mock_server.go b/server/mock_server.go index 5353570..401e558 100644 --- a/server/mock_server.go +++ b/server/mock_server.go @@ -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 diff --git a/server/services/mocks.go b/server/services/mocks.go index dedd1ec..a23f341 100644 --- a/server/services/mocks.go +++ b/server/services/mocks.go @@ -4,6 +4,7 @@ import ( "fmt" "io" "os" + "path/filepath" "regexp" "strings" "sync" @@ -42,15 +43,17 @@ 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 @@ -58,7 +61,8 @@ func NewMocks(sessions types.Sessions, historyRetention int, persistence Persist 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 @@ -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