basic server config setup, starting to setup echo routes

This commit is contained in:
2020-06-06 17:24:23 -04:00
parent 4104193be3
commit 23dd5090e3
7 changed files with 102 additions and 31 deletions

View File

@@ -0,0 +1,6 @@
version = "0.1"
port = 80
ip = ""
rawport = 0
reporootpath = "F:\\repos"

7
server/engine/engine.go Normal file
View File

@@ -0,0 +1,7 @@
package engine
import "github.com/labstack/echo"
func (server *GVCServer) Info(context echo.Context) {
}

View File

@@ -0,0 +1,12 @@
package engine
import (
config "github.com/deranjer/gvc/server/serverconfig"
"github.com/labstack/echo"
)
// GVCServer contains all the information needed for our server so we can pass it around as needed
type GVCServer struct {
Config config.GvcServerConfig //contains our full server config
Echo *echo.Echo // Contains our web server instance
}

View File

@@ -2,44 +2,51 @@ package main
import (
"fmt"
"log"
"os"
"github.com/deranjer/gvc/server/engine"
serverconfig "github.com/deranjer/gvc/server/serverconfig"
"github.com/deranjer/store"
"github.com/labstack/echo"
"github.com/labstack/gommon/log"
)
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() {
// Initialize a new server struct and config struct
var server GVCServer
var err error
var configPath string
var conf serverconfig.GvcServerConfig
configPath, err := serverconfig.FindConfig() // TODO: maybe make the findconfig a func of the struct?
configPath, err = serverconfig.FindConfig()
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,
RepoRootPath: "repos", //default repos directory will be cwd\repos
}
configPath = serverconfig.DefaultConfigPath // set the root path for our config so we can save that back to TOML
err = store.Save(serverconfig.DefaultConfigPath, &conf) // Save our new default config back to TOML so it can be read in
if err != nil {
log.Fatalf("unable to save config to toml file: %s", err)
}
}
server.configPath = configPath // set the root path for our config
err = store.Load(configPath, &conf)
err = store.Load(configPath, &conf) // existing conf was found or default conf created, so reading it in
if err != nil {
fmt.Printf("Error loading server config file into struct, please fix config, panic! \n%s", err)
os.Exit(0)
log.Fatalf("Error loading server config file into struct, please fix config, panic! \n%s", err)
}
server.config = conf // Write the conf to our server struct
err = serverconfig.ValidateConfig(&conf, configPath, version) // now that our config has been loaded, lets make sure it is valid
if err != nil {
log.Fatalf("unable to validate config, exiting: %s", err)
}
// Setup a new server instance
var server engine.GVCServer
server.Config = conf
log.Info("Logger starting...")
// Setup the web server
e := echo.New()
//Start the routes
//server.Echo.GET("/info")
}

6
server/serverConfig.toml Normal file
View File

@@ -0,0 +1,6 @@
version = "0.1"
port = 80
ip = ""
rawport = 0
reporootpath = "F:\\repos"

View File

@@ -2,46 +2,74 @@ package config
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/deranjer/store"
)
// 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"
// DefaultConfigPath contains the default location for the serverConfig.toml
var DefaultConfigPath = "config" + string(os.PathSeparator) + "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
configRootLoc, err := os.Stat("serverConfig.toml") //First checking root directory
if err != nil {
configFile, err := os.Stat(defaultConfigPath)
configFile, err := os.Stat(DefaultConfigPath) // If not in root dir, check default dir
if err != nil {
return "", err
}
if !configFile.IsDir() {
return fmt.Sprintf(defaultConfigPath), nil
return DefaultConfigPath, nil
}
return "", fmt.Errorf("config not found: %s", err)
}
if !configFile.IsDir() {
return "serverConfig.toml", nil
if !configRootLoc.IsDir() { // Make sure the .toml file isn't a directory
return "serverConfig.toml", nil //setting the config path to root
}
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 {
// ValidateConfig will go through the entire config and do basic sanity checks and write valid values to the server struct if some are missing
func ValidateConfig(conf *GvcServerConfig, configPath string, 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.RepoRootPath == "" {
fmt.Println("Repo root path: ", conf.RepoRootPath)
path, err := os.Getwd()
if err != nil {
return fmt.Errorf("unable to get current working directory, and rootpath for the repos is not set: %s", err)
}
fmt.Printf("No repo root path found, inputting current working directory: %s\n", path)
path = path + string(os.PathSeparator) + "repos"
err = os.Mkdir(path, 0644)
if err != nil {
return fmt.Errorf("unable to create 'repos' dir in current working dir: %s", err)
}
fmt.Printf("No repo root path found, inputting current working directory + repos: %s\n", path)
conf.RepoRootPath = path //Writing path back to config struct
} else {
repoFolder, err := os.Stat(conf.RepoRootPath)
if err != nil { //If folder is not found, try to create
if !filepath.IsAbs(conf.RepoRootPath) {
fmt.Println("repo folder does not appear to be absolute path, using relative to create direcotry...", conf.RepoRootPath)
} else {
fmt.Println("repo folder appears to be absolute path, creating dir: ", conf.RepoRootPath)
}
err = os.MkdirAll(conf.RepoRootPath, 0644)
if err != nil {
return fmt.Errorf("unable to make repo directory: %s", err)
}
} else { // If path exists, ensure it is directory
if !repoFolder.IsDir() { //If path leads to a file, not folder, return err
return fmt.Errorf("provided repo path leads to a file, not folder, cannot continue, please fix path")
}
}
}
// Range through the repos and run some basic validation on all of them
// for i, repo := range conf.Repos {
@@ -51,6 +79,11 @@ func ValidateConfig(conf *GvcServerConfig, version string) error {
// }
// //TODO validate ignores (pretty similar to compress)
// }
// Done making changes, so if any changes, write it to the toml file
err := store.Save(configPath, &conf) // Save our new config back to TOML so it can be read in
if err != nil {
log.Fatalf("unable to save config to toml file after validation: %s", err)
}
return nil
}