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 (
"fmt"
"log"
"os"
serverconfig "github.com/deranjer/gvc/server/serverconfig"
@@ -10,32 +11,35 @@ import (
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() {
configPath, err := findConfig()
if err != nil {
fmt.Printf("Unable to find config file: %s", err)
}
// Initialize a new server struct and config struct
var server GVCServer
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)
if err != nil {
fmt.Printf("Error loading server config file into struct, please fix config, panic! \n%s", err)
os.Exit(0)
}
}
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
}
server.config = conf // Write the conf to our server struct
}

View File

@@ -3,73 +3,92 @@ package config
import (
"fmt"
"os"
"github.com/deranjer/store"
)
// ConfigPath is the global path to the config that is injected from the main server package.
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
func ValidateConfig(conf *GvcServerConfig, version string) error {
if conf.Version == "" { // No version found, should we update it?
fmt.Printf("No version found, inputing current server version: %s\n", version)
conf.Version = version
}
if conf.RootPath == "" {
if conf.RepoRootPath == "" {
path, err := os.Getwd()
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)
if err != nil {
return err
}
err = validateCompress(conf)
if err != nil {
return err
}
//TODO validate ignores (pretty similar to compress)
// Range through the repos and run some basic validation on all of them
// for i, repo := range conf.Repos {
// err := validateCompress(repo)
// if err != nil {
// return err
// }
// //TODO validate ignores (pretty similar to compress)
// }
return nil
}
// validateCompress checks the compression settings to make sure they are valid //TODO return error needed?
func validateCompress(conf *GvcServerConfig) error {
compress := conf.NoCompress
for i, file := range compress.Files {
if file == "" {
fmt.Println("empty file in compress files, removing... ")
compress.Files[i] = compress.Files[len(compress.Files)-1]
continue
}
// TODO: write more validation
}
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
if err != nil {
continue
}
fileType := file.Mode()
if fileType.IsRegular() { // Not a 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.Files = append(compress.Files, folder)
continue
}
}
for i, ext := range compress.Exts {
if ext == "" {
fmt.Println("empty ext in compress exts, removing... ")
compress.Exts[i] = compress.Exts[len(compress.Exts)-1]
continue
}
// TODO: validate there is a "." in front of the ext, if not add it?
}
err := store.Save(ConfigPath, &conf)
if err != nil {
fmt.Println("Error saving config back to toml file: ", err)
}
return nil
}
// func validateCompress(repo RepoConfig) error {
// compress := repo.NoCompress
// for i, file := range compress.Files {
// if file == "" {
// fmt.Println("empty file in compress files, removing... ")
// compress.Files[i] = compress.Files[len(compress.Files)-1]
// continue
// }
// // TODO: write more validation
// }
// 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
// if err != nil {
// continue
// }
// fileType := file.Mode()
// if fileType.IsRegular() { // Not a 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.Files = append(compress.Files, folder)
// continue
// }
// }
// for i, ext := range compress.Exts {
// if ext == "" {
// fmt.Println("empty ext in compress exts, removing... ")
// compress.Exts[i] = compress.Exts[len(compress.Exts)-1]
// continue
// }
// // TODO: validate there is a "." in front of the ext, if not add it?
// }
// err := store.Save(ConfigPath, &conf)
// if err != nil {
// fmt.Println("Error saving config back to toml file: ", err)
// }
// return nil
// }

View File

@@ -1,13 +1,19 @@
package config
// GvcServerConfig will hold the base server settings
type GvcServerConfig struct {
Version string `toml:"version"` // The server version
Repos []RepoConfig `toml:"repos"` // A struct of all the repos and their settings for the serve
Version string `toml:"version"` // The server version
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 {
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"`
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"`