From 504cdb41cf55e9b6c225d8b23b0598aaf430f341 Mon Sep 17 00:00:00 2001 From: Akilan Date: Sat, 10 Feb 2024 22:05:47 +0000 Subject: [PATCH] added easier use of abstractions --- abstractions/base.go | 56 ++++++++++++++++++++++---------- config/config.go | 11 ++++++- config/generate/generate.go | 8 +---- config/generate/generateFiles.go | 9 ++--- 4 files changed, 54 insertions(+), 30 deletions(-) diff --git a/abstractions/base.go b/abstractions/base.go index e2d0c62..9986b91 100644 --- a/abstractions/base.go +++ b/abstractions/base.go @@ -1,32 +1,52 @@ package abstractions import ( - "github.com/Akilan1999/p2p-rendering-computation/config" - "github.com/Akilan1999/p2p-rendering-computation/config/generate" - "github.com/Akilan1999/p2p-rendering-computation/server" - "github.com/gin-gonic/gin" + Config "github.com/Akilan1999/p2p-rendering-computation/config" + "github.com/Akilan1999/p2p-rendering-computation/config/generate" + "github.com/Akilan1999/p2p-rendering-computation/server" + "github.com/gin-gonic/gin" + "os" ) // Init Initialises p2prc -func Init(customConfig interface{}) (config *config.Config, err error) { - // set the config file with default paths - config, err = generate.SetDefaults("P2PRC", false, customConfig, false) - if err != nil { - return - } - return +func Init(customConfig interface{}) (config *Config.Config, err error) { + + // Get config file path + // Checks P2PRC path initially + // - Get PATH if environment varaible + path, err := Config.GetPathP2PRC("P2PRC") + if err != nil { + return + } + // check if the config file exists + if _, err = os.Stat(path + "config.json"); err != nil { + // Initialize with base p2prc config files + // set the config file with default paths + config, err = generate.SetDefaults("P2PRC", false, customConfig, false) + if err != nil { + return + } + } else { + // If the configs are available then use them over generating new ones. + config, err = Config.ConfigInit(nil, nil) + if err != nil { + return + } + } + + return } // Start p2prc in a server mode func Start() (*gin.Engine, error) { - engine, err := server.Server() - if err != nil { - return nil, err - } - return engine, nil + engine, err := server.Server() + if err != nil { + return nil, err + } + return engine, nil } func MapPort(port string) (entireAddres string, mapPort string, err error) { - entireAddres, mapPort, err = server.MapPort(port) - return + entireAddres, mapPort, err = server.MapPort(port) + return } diff --git a/config/config.go b/config/config.go index 66ec1bf..62572c5 100644 --- a/config/config.go +++ b/config/config.go @@ -39,6 +39,12 @@ type Config struct { //NetworkInterfaceIPV6Index int } +// GetCurrentPath Getting P2PRC Directory from environment variable +func GetCurrentPath() (string, error) { + curDir := os.Getenv("PWD") + return curDir + "/", nil +} + // GetPathP2PRC Getting P2PRC Directory from environment variable func GetPathP2PRC(Envname string) (string, error) { if Envname != "" { @@ -49,7 +55,10 @@ func GetPathP2PRC(Envname string) (string, error) { } curDir := os.Getenv(defaultEnvName) if curDir == "" { - return curDir, nil + // if the OS env path is not found then you use + // the current directory path. + currentPath, _ := GetCurrentPath() + return currentPath, nil } return curDir + "/", nil } diff --git a/config/generate/generate.go b/config/generate/generate.go index 393c29f..695021f 100644 --- a/config/generate/generate.go +++ b/config/generate/generate.go @@ -31,17 +31,11 @@ func SetEnvName(EnvName string) error { return nil } -// GetCurrentPath Getting P2PRC Directory from environment variable -func GetCurrentPath() (string, error) { - curDir := os.Getenv("PWD") - return curDir + "/", nil -} - // SetDefaults This function to be called only during a // make install func SetDefaults(envName string, forceDefault bool, CustomConfig interface{}, NoBoilerPlate bool, ConfigUpdate ...*config.Config) (*config.Config, error) { //Setting current directory to default path - defaultPath, err := GetCurrentPath() + defaultPath, err := config.GetCurrentPath() if err != nil { return nil, err } diff --git a/config/generate/generateFiles.go b/config/generate/generateFiles.go index af14980..78c58f7 100644 --- a/config/generate/generateFiles.go +++ b/config/generate/generateFiles.go @@ -5,6 +5,7 @@ import ( "crypto/rsa" "crypto/x509" "encoding/pem" + "github.com/Akilan1999/p2p-rendering-computation/config" "github.com/Akilan1999/p2p-rendering-computation/p2p" "github.com/go-git/go-git/v5" "golang.org/x/crypto/ssh" @@ -61,7 +62,7 @@ func GenerateIPTableFile(rootNodes []p2p.IpAddress) (err error) { // CreateIPTableFolderStructure Create folder structure for IPTable func CreateIPTableFolderStructure() (err error) { - path, err := GetCurrentPath() + path, err := config.GetCurrentPath() if err != nil { return err } @@ -87,7 +88,7 @@ func CreateIPTableFolderStructure() (err error) { // GenerateDockerFiles Generate default docker files func GenerateDockerFiles() (err error) { - path, err := GetCurrentPath() + path, err := config.GetCurrentPath() if err != nil { return err } @@ -132,7 +133,7 @@ func GenerateDockerFiles() (err error) { // GeneratePluginDirectory Generates plugin directory structure func GeneratePluginDirectory() (err error) { - path, err := GetCurrentPath() + path, err := config.GetCurrentPath() if err != nil { return err } @@ -150,7 +151,7 @@ func GeneratePluginDirectory() (err error) { } func GenerateClientTrackContainers() (err error) { - path, err := GetCurrentPath() + path, err := config.GetCurrentPath() if err != nil { return err }