fixed configuration issues

This commit is contained in:
2023-03-01 10:41:37 +00:00
parent 08a48b46dc
commit ad303c963e
2 changed files with 93 additions and 61 deletions

View File

@@ -2,7 +2,6 @@ package config
import ( import (
"encoding/json" "encoding/json"
"github.com/spf13/viper"
"io/ioutil" "io/ioutil"
"os" "os"
"os/user" "os/user"
@@ -27,7 +26,9 @@ type Config struct {
NATEscapeServerPort string NATEscapeServerPort string
NATEscapeBarrierPort string NATEscapeBarrierPort string
BackendURL string BackendURL string
BrowserCommand string
Rate float64 Rate float64
ScreenName string
} }
// Exists reports whether the named file or directory exists. // Exists reports whether the named file or directory exists.
@@ -44,10 +45,11 @@ func fileExists(name string) bool {
// make install // make install
func SetDefaults() error { func SetDefaults() error {
//Getting Current Directory from environment variable //Getting Current Directory from environment variable
curDir := os.Getenv("REMOTEGAMING") //curDir := os.Getenv("REMOTEGAMING")
//Setting current directory to default path //Setting current directory to default path
defaultPath = curDir + "/" defaultPath, _ = os.Getwd()
defaultPath = defaultPath + "/"
// Get system username // Get system username
user, err := user.Current() user, err := user.Current()
@@ -61,73 +63,105 @@ func SetDefaults() error {
return err return err
} }
//Setting default paths for the config file var c Config
defaults["SystemUsername"] = user.Username
defaults["BarrierHostName"] = name
defaults["Rooms"] = defaultPath + "room.json"
defaults["ScriptToExecute"] = ""
defaults["SSHPassword"] = ""
defaults["Rate"] = 0.0
defaults["BackendURL"] = "https://xplane-webrtc.akilan.io"
//Paths to search for config file c.BrowserCommand = "chromium-browser"
configPaths = append(configPaths, defaultPath) c.SystemUsername = user.Username
c.Rooms = defaultPath + "room.json"
c.BarrierHostName = name
c.ScriptToExecute = ""
c.SSHPassword = ""
c.Rate = 0.0
c.BackendURL = "https://xplane-webrtc.akilan.io"
c.ScreenName = "Entire screen"
//Create rooms.json file file, _ := json.MarshalIndent(c, "", " ")
os.Create(defaultPath + "room.json")
// If the config file exists remove and make a new one _ = ioutil.WriteFile(configFile, file, 0644)
if fileExists(defaultPath + "config.json") {
err = os.Remove(defaultPath + "config.json")
if err != nil {
return err
}
}
//Calling configuration file ////Setting default paths for the config file
_, err = ConfigInit() //defaults["SystemUsername"] = user.Username
if err != nil { //defaults["BarrierHostName"] = name
return err //defaults["Rooms"] = defaultPath + "room.json"
} //defaults["ScriptToExecute"] = ""
//defaults["SSHPassword"] = ""
//defaults["Rate"] = 0.0
//defaults["BackendURL"] = "https://xplane-webrtc.akilan.io"
//defaults["BrowserCommand"] = "chromium-browser"
//
////Paths to search for config file
//configPaths = append(configPaths, defaultPath)
//
////Create rooms.json file
//os.Create(defaultPath + "room.json")
//
//// If the config file exists remove and make a new one
//if fileExists(defaultPath + "config.json") {
// err = os.Remove(defaultPath + "config.json")
// if err != nil {
// return err
// }
//}
//
////Calling configuration file
//_, err = ConfigInit()
//if err != nil {
// return err
//}
return nil return nil
} }
func ConfigInit() (*Config, error) { func ConfigInit() (*Config, error) {
curDir := os.Getenv("REMOTEGAMING") //curDir := os.Getenv("REMOTEGAMING")
//Setting current directory to default path ////Setting current directory to default path
defaultPath = curDir + "/" //defaultPath = curDir + "/"
//Paths to search for config file ////Paths to search for config file
configPaths = append(configPaths, defaultPath) //configPaths = append(configPaths, defaultPath)
//
////Add all possible configurations paths
//for _, v := range configPaths {
// viper.AddConfigPath(v)
//}
//
////Read config file
//if err := viper.ReadInConfig(); err != nil {
// // If the error thrown is config file not found
// //Sets default configuration to viper
// for k, v := range defaults {
// viper.SetDefault(k, v)
// }
//
// viper.SetConfigName(configName)
// viper.SetConfigFile(configFile)
// viper.SetConfigType(configType)
//
// if err = viper.WriteConfig(); err != nil {
// return nil, err
// }
//}
//
//// Adds configuration to the struct
//var config Config
//if err := viper.Unmarshal(&config); err != nil {
// return nil, err
//}
//Add all possible configurations paths // Open our jsonFile
for _, v := range configPaths { jsonFile, err := os.Open(configFile)
viper.AddConfigPath(v) // if we os.Open returns an error then handle it
} if err != nil {
//Read config file
if err := viper.ReadInConfig(); err != nil {
// If the error thrown is config file not found
//Sets default configuration to viper
for k, v := range defaults {
viper.SetDefault(k, v)
}
viper.SetConfigName(configName)
viper.SetConfigFile(configFile)
viper.SetConfigType(configType)
if err = viper.WriteConfig(); err != nil {
return nil, err
}
}
// Adds configuration to the struct
var config Config
if err := viper.Unmarshal(&config); err != nil {
return nil, err return nil, err
} }
// defer the closing of our jsonFile so that we can parse it later on
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var config Config
json.Unmarshal(byteValue, &config)
return &config, nil return &config, nil
} }

View File

@@ -110,7 +110,7 @@ func main() {
if *headless { if *headless {
// Running starting a browser as a background process // Running starting a browser as a background process
go func() { go func() {
Config, err := config.ConfigInit() Config, err = config.ConfigInit()
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
@@ -134,10 +134,8 @@ func main() {
TaskExecute = Config.ScriptToExecute TaskExecute = Config.ScriptToExecute
} }
fmt.Println(addr)
// Starting screen share headless // Starting screen share headless
cmd := exec.Command("chromium-browser", "--no-sandbox", "--auto-select-desktop-capture-source=Entire screen", "--url", "https://"+*addr+":"+*port+"/?mode=headless", "--ignore-certificate-errors") cmd := exec.Command(Config.BrowserCommand, "--no-sandbox", "--auto-select-desktop-capture-source="+Config.ScreenName, "--url", "https://"+*addr+":"+*port+"/?mode=headless", "--ignore-certificate-errors")
if err := cmd.Start(); err != nil { if err := cmd.Start(); err != nil {
log.Fatalln(err) log.Fatalln(err)
} }