more server work on structs and config

This commit is contained in:
2020-06-06 09:49:29 -04:00
parent b015680962
commit 4104193be3
3 changed files with 105 additions and 76 deletions

View File

@@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"log"
"os" "os"
serverconfig "github.com/deranjer/gvc/server/serverconfig" serverconfig "github.com/deranjer/gvc/server/serverconfig"
@@ -10,32 +11,35 @@ import (
var version = "0.1" var version = "0.1"
// GVCServer contains all the information needed for our server so we can pass it around as needed
type GVCServer struct {
configPath string
config serverconfig.GvcServerConfig
}
func main() { func main() {
configPath, err := findConfig() // Initialize a new server struct and config struct
if err != nil { var server GVCServer
fmt.Printf("Unable to find config file: %s", err)
}
var conf serverconfig.GvcServerConfig var conf serverconfig.GvcServerConfig
configPath, err := serverconfig.FindConfig() // TODO: maybe make the findconfig a func of the struct?
if err != nil {
fmt.Printf("Unable to find config file: %s\n", err)
fmt.Println("Since no config found, creating a default config to use...")
pwd, err := os.Getwd()
if err != nil {
log.Fatal("unable to get current working dir, exiting")
}
conf = serverconfig.GvcServerConfig{
Version: "0.1",
Port: 80,
RepoRootPath: pwd,
}
}
server.configPath = configPath // set the root path for our config
err = store.Load(configPath, &conf) err = store.Load(configPath, &conf)
if err != nil { if err != nil {
fmt.Printf("Error loading server config file into struct, please fix config, panic! \n%s", err) fmt.Printf("Error loading server config file into struct, please fix config, panic! \n%s", err)
os.Exit(0) os.Exit(0)
} }
} server.config = conf // Write the conf to our server struct
func findConfig() (string, error) {
configFile, err := os.Stat("serverConfig.toml")
if err != nil {
configFile, err := os.Stat("config" + os.PathListSeparator + "serverConfig.toml")
if err != nil {
return "", err
}
if !configFile.IsDir() {
return fmt.Sprintf("config" + os.PathListSeparator + "serverConfig.toml"), nil
}
return "", fmt.Sprintf()
}
if !configFile.IsDir() {
return "serverConfig.toml", nil
}
} }

View File

@@ -3,73 +3,92 @@ package config
import ( import (
"fmt" "fmt"
"os" "os"
"github.com/deranjer/store"
) )
// ConfigPath is the global path to the config that is injected from the main server package. // ConfigPath is the global path to the config that is injected from the main server package.
var ConfigPath string var ConfigPath string
// default config path
var defaultConfigPath = "config" + string(os.PathListSeparator) + "serverConfig.toml"
// FindConfig Search for the config it 2 places, root and in the config/ folder
func FindConfig() (string, error) {
configFile, err := os.Stat("serverConfig.toml") //First checking root directory
if err != nil {
configFile, err := os.Stat(defaultConfigPath)
if err != nil {
return "", err
}
if !configFile.IsDir() {
return fmt.Sprintf(defaultConfigPath), nil
}
return "", fmt.Errorf("config not found: %s", err)
}
if !configFile.IsDir() {
return "serverConfig.toml", nil
}
return "", fmt.Errorf("serverConfig.toml does not appear to be in the correct file format")
}
// ValidateConfig will go through the entire config and do basic sanity checks // ValidateConfig will go through the entire config and do basic sanity checks
func ValidateConfig(conf *GvcServerConfig, version string) error { func ValidateConfig(conf *GvcServerConfig, version string) error {
if conf.Version == "" { // No version found, should we update it? if conf.Version == "" { // No version found, should we update it?
fmt.Printf("No version found, inputing current server version: %s\n", version) fmt.Printf("No version found, inputing current server version: %s\n", version)
conf.Version = version conf.Version = version
} }
if conf.RootPath == "" { if conf.RepoRootPath == "" {
path, err := os.Getwd() path, err := os.Getwd()
if err != nil { if err != nil {
return fmt.Errorf("unable to get current working directory, and rootpath of repo is not set: %s", err) return fmt.Errorf("unable to get current working directory, and rootpath for the repos is not set: %s", err)
} }
fmt.Printf("No root path found, inputting current working directory: %s\n", path) fmt.Printf("No repo root path found, inputting current working directory: %s\n", path)
} }
err := validateRemotes(conf) // Range through the repos and run some basic validation on all of them
if err != nil { // for i, repo := range conf.Repos {
return err // err := validateCompress(repo)
} // if err != nil {
err = validateCompress(conf) // return err
if err != nil { // }
return err // //TODO validate ignores (pretty similar to compress)
} // }
//TODO validate ignores (pretty similar to compress)
return nil return nil
} }
// validateCompress checks the compression settings to make sure they are valid //TODO return error needed? // validateCompress checks the compression settings to make sure they are valid //TODO return error needed?
func validateCompress(conf *GvcServerConfig) error { // func validateCompress(repo RepoConfig) error {
compress := conf.NoCompress // compress := repo.NoCompress
for i, file := range compress.Files { // for i, file := range compress.Files {
if file == "" { // if file == "" {
fmt.Println("empty file in compress files, removing... ") // fmt.Println("empty file in compress files, removing... ")
compress.Files[i] = compress.Files[len(compress.Files)-1] // compress.Files[i] = compress.Files[len(compress.Files)-1]
continue // continue
} // }
// TODO: write more validation // // TODO: write more validation
} // }
for i, folder := range compress.Folders { // for i, folder := range compress.Folders {
file, err := os.Stat(folder) // TODO: check to see if it returns ABS or not, might need to convert // file, err := os.Stat(folder) // TODO: check to see if it returns ABS or not, might need to convert
if err != nil { // if err != nil {
continue // continue
} // }
fileType := file.Mode() // fileType := file.Mode()
if fileType.IsRegular() { // Not a folder // if fileType.IsRegular() { // Not a folder
fmt.Println("compressed folder in array is not actually a folder, moving it to file compress: ", folder) // fmt.Println("compressed folder in array is not actually a folder, moving it to file compress: ", folder)
compress.Folders[i] = compress.Folders[len(compress.Folders)-1] // compress.Folders[i] = compress.Folders[len(compress.Folders)-1]
compress.Files = append(compress.Files, folder) // compress.Files = append(compress.Files, folder)
continue // continue
} // }
} // }
for i, ext := range compress.Exts { // for i, ext := range compress.Exts {
if ext == "" { // if ext == "" {
fmt.Println("empty ext in compress exts, removing... ") // fmt.Println("empty ext in compress exts, removing... ")
compress.Exts[i] = compress.Exts[len(compress.Exts)-1] // compress.Exts[i] = compress.Exts[len(compress.Exts)-1]
continue // continue
} // }
// TODO: validate there is a "." in front of the ext, if not add it? // // TODO: validate there is a "." in front of the ext, if not add it?
} // }
err := store.Save(ConfigPath, &conf) // err := store.Save(ConfigPath, &conf)
if err != nil { // if err != nil {
fmt.Println("Error saving config back to toml file: ", err) // fmt.Println("Error saving config back to toml file: ", err)
} // }
return nil // return nil
} // }

View File

@@ -1,13 +1,19 @@
package config package config
// GvcServerConfig will hold the base server settings
type GvcServerConfig struct { type GvcServerConfig struct {
Version string `toml:"version"` // The server version Version string `toml:"version"` // The server version
Repos []RepoConfig `toml:"repos"` // A struct of all the repos and their settings for the serve Port int `toml:"port"` // The port that the server will listed on
BindIP string `toml:"ip"` // What IP to bind the server to. If empty will bind to all interfaces
RawPort int `toml:"rawport"` // The optional TCP port that the server will send raw files over
RepoRootPath string `toml:"reporootpath"` // This will be the root path where (by default) all new repos will be stored at
Repos []RepoConfig `toml:"repos"` // A struct of all the repos and their settings for the serve
} }
//GvcServerConfig will be the struct that holds the entire server settings // RepoConfig will be the struct that holds the config for a single repo
type RepoConfig struct { type RepoConfig struct {
KnownClients []Clients `toml:"clients"` //The remote servers for the repo KnownClients []Clients `toml:"clients"` //The remote servers for the repo
RootPath string `toml:"rootpath"` // The absolute path to the root of this particular repo
DefaultBranch string `toml:"defaultbranch"` DefaultBranch string `toml:"defaultbranch"`
LocalBranches []string `toml:"localbranches"` // LocalBranches constains a string list of branches on the server. Names must be unique. \\TODO: someday add folders like git for branches LocalBranches []string `toml:"localbranches"` // LocalBranches constains a string list of branches on the server. Names must be unique. \\TODO: someday add folders like git for branches
Locked FileTypes `toml:"locked"` Locked FileTypes `toml:"locked"`