switching server over to Echo, setting up server struct

This commit is contained in:
2020-06-05 23:32:31 -04:00
parent db2221c515
commit b015680962
7 changed files with 179 additions and 53 deletions

View File

@@ -1,62 +1,41 @@
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"github.com/deranjer/gvc/messages"
"github.com/firstrow/tcp_server"
serverconfig "github.com/deranjer/gvc/server/serverconfig"
"github.com/deranjer/store"
)
var version = "0.1"
//This is a generic error message so we make it 'global'
var errorMsg = messages.Command{
CmdID: messages.ERROR,
Body: []byte("Unrecognized Message"),
}
func main() {
errMsgJSON, _ := json.Marshal(errorMsg) //creating a []byte to easily send error message
server := tcp_server.New("localhost:9999")
server.OnNewClient(func(c *tcp_server.Client) {
// new client connected
fmt.Println("New client connected....")
newMessage := messages.Command{
CmdID: messages.CONNECTED,
Body: []byte("Connected"),
}
jsonMessage, err := json.Marshal(newMessage)
if err != nil {
log.Fatalf("error creating new marshaller: %s", err)
}
c.Send(jsonMessage)
})
server.OnNewMessage(func(c *tcp_server.Client, message []byte) {
var newMessage messages.Command
//messageBytes := []byte(message)
err := json.Unmarshal(message, &newMessage)
if err != nil {
c.Send(errMsgJSON)
fmt.Println("error reading message, closing connection to client")
c.Close()
}
fmt.Println("new Message from client: ", message)
switch newMessage.CmdID {
case messages.INFO:
fmt.Println("Sending server info to client")
detailsMessage := fmt.Sprintf("Server Details are as follows: Version: %s \n", version)
c.Send(detailsMessage)
default:
fmt.Println("unrecognized message! ", message)
}
})
server.OnClientConnectionClosed(func(c *tcp_server.Client, err error) {
// connection with client lost
fmt.Println("Connection with client closed")
})
server.Listen()
configPath, err := findConfig()
if err != nil {
fmt.Printf("Unable to find config file: %s", err)
}
var conf serverconfig.GvcServerConfig
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
}
}

View File

@@ -0,0 +1,75 @@
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
// 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 == "" {
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)
}
fmt.Printf("No 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)
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
}

View File

View File

@@ -0,0 +1,30 @@
package config
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
}
//GvcServerConfig will be the struct that holds the entire server settings
type RepoConfig struct {
KnownClients []Clients `toml:"clients"` //The remote servers for the 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"`
DefaultIgnores FileTypes `toml:"defaultignore"` //These are the recommended ignores that clients can pull
NoCompress FileTypes `toml:"nocompress"` //For binary compression some files should be ignored because the performance hit isn't worth the size savings
}
//Clients will be a slice of clients that have authenticated to the server
type Clients struct {
Name string `toml:"name"`
Key string `toml:"key"` //TODO will change this once we figure out authentication
LastCommit string `toml:"lastcommit"` //Last commit that this client pushed to the server? not sure if useful
}
//FileTypes is for ignoring files to add or ignoring compress, or for locked files, all use the same type of struct (files, folders and exts)
type FileTypes struct {
Files []string `toml:"files"`
Exts []string `toml:"exts"`
Folders []string `toml:"folders"`
}