Skip to content

Commit

Permalink
added easier use of abstractions
Browse files Browse the repository at this point in the history
  • Loading branch information
Akilan1999 committed Feb 10, 2024
1 parent c815ed3 commit 504cdb4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 30 deletions.
56 changes: 38 additions & 18 deletions abstractions/base.go
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 10 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -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
}
Expand Down
8 changes: 1 addition & 7 deletions config/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
9 changes: 5 additions & 4 deletions config/generate/generateFiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -150,7 +151,7 @@ func GeneratePluginDirectory() (err error) {
}

func GenerateClientTrackContainers() (err error) {
path, err := GetCurrentPath()
path, err := config.GetCurrentPath()
if err != nil {
return err
}
Expand Down

0 comments on commit 504cdb4

Please sign in to comment.