Browse Source

Merge commit '1a9d5120287e9fd36002c1a9d26691ddde73a1d6' into HEAD

feature/update-route-registration
Jenkins 3 years ago
committed by Fabian Vowie
parent
commit
fe5405dd51
No known key found for this signature in database GPG Key ID: C27317C33B27C410
  1. 13
      main.go
  2. 5
      main_test.go
  3. 3
      pipelines/pipeline.go
  4. 20
      settings/settings.go
  5. 3
      settings/settings_test.go

13
main.go

@ -45,19 +45,22 @@ func RegisterPipelineRoutes(r *mux.Router, pipelines []pipelines.IPipeline, stor
}
func main() {
settings := settings.LoadSettings(afero.NewOsFs())
appSettings, err := settings.LoadSettings(afero.NewOsFs())
if err != nil {
panic(err)
}
var storageProvider storage.IStorageProvider
if settings.StorageProvider.Type == 0 {
storageProvider = storage.GetFileSystemStorageProvider(settings.StorageProvider.BasePath, "")
if appSettings.StorageProvider.Type == 0 {
storageProvider = storage.GetFileSystemStorageProvider(appSettings.StorageProvider.BasePath, "")
} else {
panic("Invalid file system provided!")
}
pipes := pipelines.LoadPipelines()
authMiddleware := auth.CreateAuthenticationMiddleware(settings.Token)
authMiddleware := auth.CreateAuthenticationMiddleware(appSettings.Token)
r := mux.NewRouter()
r.Use(authMiddleware.Middleware)
@ -65,7 +68,7 @@ func main() {
RegisterPipelineRoutes(r, pipes, storageProvider)
err := http.ListenAndServe(":8000", r)
err = http.ListenAndServe(appSettings.Endpoint, r)
if err != nil {
panic(err)
}

5
main_test.go

@ -2,7 +2,6 @@ package main
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
@ -29,9 +28,7 @@ func TestIndexRoute(t *testing.T) {
func TestEndpointRoute(t *testing.T) {
data := pipelines.Pipeline{}
err := faker.FakeData(&data)
if err != nil {
fmt.Println(err)
}
assert.Nil(t, err)
t.Run("Registered pipelines are valid routes", func(t *testing.T) {
router := mux.NewRouter()

3
pipelines/pipeline.go

@ -44,8 +44,6 @@ type Pipeline struct {
}
func (p Pipeline) Run(srcPath, bucketName string, storageProvider storage.IStorageProvider) (string, error) {
fmt.Println("path: ", storageProvider.GetPath(bucketName, srcPath))
src, err := imaging.Open(storageProvider.GetPath(bucketName, srcPath))
if err != nil {
return "", errors.New(fmt.Sprintf("error opening file for processing: %s", err))
@ -134,7 +132,6 @@ func LoadPipelines() []IPipeline {
err := filepath.Walk(path+"/config", func(path string, info fs.FileInfo, err error) error {
if err == nil && info.IsDir() == false {
fmt.Println(path)
data, _ := os.ReadFile(path)
files = append(files, data)
}

20
settings/settings.go

@ -28,12 +28,15 @@ type StorageSettings struct {
func parseSettings(data []byte) Settings {
settings := Settings{}
json.Unmarshal(data, &settings)
err := json.Unmarshal(data, &settings)
if err != nil {
return Settings{}
}
return settings
}
func LoadSettings(fileSystem afero.Fs) Settings {
func LoadSettings(fileSystem afero.Fs) (Settings, error) {
workingDirectory, _ := os.Getwd()
path := filepath.Join(workingDirectory, "settings.json")
@ -41,7 +44,7 @@ func LoadSettings(fileSystem afero.Fs) Settings {
data, err := afero.ReadFile(fileSystem, path)
if err == nil {
return parseSettings(data)
return parseSettings(data), nil
}
// If file does not exist, create default settings
@ -55,7 +58,14 @@ func LoadSettings(fileSystem afero.Fs) Settings {
}
serializedSettings, err := json.MarshalIndent(defaultSettings, "", "\t")
afero.WriteFile(fileSystem, path, serializedSettings, os.ModePerm)
if err != nil {
return Settings{}, err
}
err = afero.WriteFile(fileSystem, path, serializedSettings, os.ModePerm)
if err != nil {
return Settings{}, err
}
return defaultSettings
return defaultSettings, nil
}

3
settings/settings_test.go

@ -39,7 +39,8 @@ func TestSettingsLoading(t *testing.T) {
doesFileExist, _ := afero.Exists(fileSystem, path)
assert.False(t, doesFileExist)
LoadSettings(fileSystem)
_, err := LoadSettings(fileSystem)
assert.Nil(t, err)
// Settings file should be present after calling LoadSettings
doesFileExist, _ = afero.Exists(fileSystem, path)

Loading…
Cancel
Save