From f7901489d13c90c0f4d4273e93eacc990087e6b8 Mon Sep 17 00:00:00 2001
From: Sebastien Douheret <sebastien.douheret@iot.bzh>
Date: Fri, 22 Sep 2017 10:06:18 +0200
Subject: Allow to run xds-server without syncthing support.

To disable synchting support, just don't declare
"syncthing": {...} section in config.json file.
---
 lib/folder/folder-st-disable.go | 82 +++++++++++++++++++++++++++++++++++++++++
 lib/model/folders.go            | 39 ++++++++++++--------
 2 files changed, 105 insertions(+), 16 deletions(-)
 create mode 100644 lib/folder/folder-st-disable.go

(limited to 'lib')

diff --git a/lib/folder/folder-st-disable.go b/lib/folder/folder-st-disable.go
new file mode 100644
index 0000000..f90b776
--- /dev/null
+++ b/lib/folder/folder-st-disable.go
@@ -0,0 +1,82 @@
+package folder
+
+import (
+	"github.com/iotbzh/xds-server/lib/xdsconfig"
+	uuid "github.com/satori/go.uuid"
+)
+
+// IFOLDER interface implementation for disabled Syncthing folders
+// It's a "fallback" interface used to keep syncthing folders config even
+// when syncthing is not running.
+
+// STFolderDisable .
+type STFolderDisable struct {
+	globalConfig *xdsconfig.Config
+	config       FolderConfig
+}
+
+// NewFolderSTDisable Create a new instance of STFolderDisable
+func NewFolderSTDisable(gc *xdsconfig.Config) *STFolderDisable {
+	f := STFolderDisable{
+		globalConfig: gc,
+	}
+	return &f
+}
+
+// NewUID Get a UUID
+func (f *STFolderDisable) NewUID(suffix string) string {
+	return uuid.NewV1().String() + "_" + suffix
+}
+
+// Add a new folder
+func (f *STFolderDisable) Add(cfg FolderConfig) (*FolderConfig, error) {
+	f.config = cfg
+	f.config.Status = StatusDisable
+	f.config.IsInSync = false
+	return &f.config, nil
+}
+
+// GetConfig Get public part of folder config
+func (f *STFolderDisable) GetConfig() FolderConfig {
+	return f.config
+}
+
+// GetFullPath returns the full path of a directory (from server POV)
+func (f *STFolderDisable) GetFullPath(dir string) string {
+	return ""
+}
+
+// ConvPathCli2Svr Convert path from Client to Server
+func (f *STFolderDisable) ConvPathCli2Svr(s string) string {
+	return ""
+}
+
+// ConvPathSvr2Cli Convert path from Server to Client
+func (f *STFolderDisable) ConvPathSvr2Cli(s string) string {
+	return ""
+}
+
+// Remove a folder
+func (f *STFolderDisable) Remove() error {
+	return nil
+}
+
+// RegisterEventChange requests registration for folder change event
+func (f *STFolderDisable) RegisterEventChange(cb *EventCB, data *EventCBData) error {
+	return nil
+}
+
+// UnRegisterEventChange remove registered callback
+func (f *STFolderDisable) UnRegisterEventChange() error {
+	return nil
+}
+
+// Sync Force folder files synchronization
+func (f *STFolderDisable) Sync() error {
+	return nil
+}
+
+// IsInSync Check if folder files are in-sync
+func (f *STFolderDisable) IsInSync() (bool, error) {
+	return false, nil
+}
diff --git a/lib/model/folders.go b/lib/model/folders.go
index 7c08a88..576c4a2 100644
--- a/lib/model/folders.go
+++ b/lib/model/folders.go
@@ -78,6 +78,8 @@ func (f *Folders) LoadConfig() error {
 			// Don't exit on such error, just log it
 			f.Log.Errorf(err.Error())
 		}
+	} else {
+		f.Log.Infof("Syncthing support is disabled.")
 	}
 
 	// Merge syncthing folders into XDS folders
@@ -101,20 +103,22 @@ func (f *Folders) LoadConfig() error {
 
 	// Detect ghost project
 	// (IOW existing in xds file config and not in syncthing database)
-	for i, xf := range flds {
-		// only for syncthing project
-		if xf.Type != folder.TypeCloudSync {
-			continue
-		}
-		found := false
-		for _, stf := range stFlds {
-			if stf.ID == xf.ID {
-				found = true
-				break
+	if f.SThg != nil {
+		for i, xf := range flds {
+			// only for syncthing project
+			if xf.Type != folder.TypeCloudSync {
+				continue
+			}
+			found := false
+			for _, stf := range stFlds {
+				if stf.ID == xf.ID {
+					found = true
+					break
+				}
+			}
+			if !found {
+				flds[i].Status = folder.StatusErrorConfig
 			}
-		}
-		if !found {
-			flds[i].Status = folder.StatusErrorConfig
 		}
 	}
 
@@ -196,10 +200,13 @@ func (f *Folders) createUpdate(newF folder.FolderConfig, create bool, initial bo
 	switch newF.Type {
 	// SYNCTHING
 	case folder.TypeCloudSync:
-		if f.SThg == nil {
-			return nil, fmt.Errorf("CloudSync type not supported (syncthing not initialized)")
+		if f.SThg != nil {
+			fld = f.SThg.NewFolderST(f.Conf)
+		} else {
+			f.Log.Debugf("Disable project %v (syncthing not initialized)", newF.ID)
+			fld = folder.NewFolderSTDisable(f.Conf)
 		}
-		fld = f.SThg.NewFolderST(f.Conf)
+
 	// PATH MAP
 	case folder.TypePathMap:
 		fld = folder.NewFolderPathMap(f.Conf)
-- 
cgit