From 87c40eed1abc6ddafeade4171f60b44d5fb70242 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Nu=C8=9Biu?= Date: Thu, 16 Nov 2017 11:19:01 +0200 Subject: [PATCH] Implementing configuration settings from file --- README.md | 22 +++++++++++++++++++- server/server/config.go | 39 ++++++++++++++++++++++++++++++++++++ server/server/config_test.go | 38 +++++++++++++++++++++++++++++++++++ server/server/path.go | 11 +--------- server/simplFTP.go | 14 ++++++++++--- 5 files changed, 110 insertions(+), 14 deletions(-) create mode 100644 server/server/config.go create mode 100644 server/server/config_test.go diff --git a/README.md b/README.md index b5e00ee..53b29a4 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,24 @@ that I took in Fall 2017 at UPT. The scope of this project is to implement a simple server that handles multiple clients and allows the clients to execute commands on it. -The client is soon to come. \ No newline at end of file +## Configuration + +The server can be configured via command line flags (soon to come) and a configuration file. +If no configuration file is provided the server will run with the default settings. + +Sample Configuration File: +``` +{ + "Address": "localhost", + "Port": "8000", + "MaxDirDepth": 30, + "AbsoluteServePath": "./" +} +``` + +The **config.json** file contains the following settings: + +Address - The address on which to serve +Port - The port +MaxDirDepth - The maximum depth the user can go into directories. A value of 30 means the user can cd into max 30 dirs. +AbsoluteServePath - The path from where to serve the files. diff --git a/server/server/config.go b/server/server/config.go new file mode 100644 index 0000000..89039b5 --- /dev/null +++ b/server/server/config.go @@ -0,0 +1,39 @@ +// This file contains the configuration settings for the server. +package server + +import ( + "log" + + "github.com/spf13/viper" +) + +// LoadConfig tries to load the configuration file from the disk. +func LoadConfigFromFile() error { + viper.SetConfigName("config") + viper.AddConfigPath(viper.GetString("ConfigPath")) + + err := viper.ReadInConfig() // Find and read the config file + if err != nil { // Handle errors reading the config file + log.Printf("Fatal error reading config file: %s \n", err) + } + return err +} + +// SetDefaultConfiguration will set the default configuration settings. +func SetDefaultConfiguration() { + viper.SetDefault("Address", "localhost") + viper.SetDefault("Port", "8080") + viper.SetDefault("ConfigPath", ".") + viper.SetDefault("MaxDirDepth", 30) + viper.SetDefault("AbsoluteServePath", "./") +} + +// InitializedConfiguration initializes the configuration for the application. +func InitializedConfiguration() { + SetDefaultConfiguration() + LoadConfigFromFile() + + // TODO, Override from command line flags. + + BasePath = viper.GetString("AbsoluteServePath") +} diff --git a/server/server/config_test.go b/server/server/config_test.go new file mode 100644 index 0000000..14fe1b4 --- /dev/null +++ b/server/server/config_test.go @@ -0,0 +1,38 @@ +package server + +import ( + "testing" + + "github.com/spf13/viper" +) + +func TestLoadConfigFromFile(t *testing.T) { + // SetDefaultConfiguration must be called BEFORE LoadConfigFromFile. + SetDefaultConfiguration() + LoadConfigFromFile() + + Address := viper.GetString("Address") + if Address == "" { + t.Error("TestLoadConfigFromFile: Can't get Address!") + } + + Port := viper.GetString("Port") + if Port == "" { + t.Error("TestLoadConfigFromFile: Can't get Port!") + } + + ConfigPath := viper.GetString("ConfigPath") + if ConfigPath == "" { + t.Error("TestLoadConfigFromFile: Can't get ConfigPath!") + } + + DirDepth := viper.GetInt("MaxDirDepth") + if DirDepth == 0 { + t.Error("TestLoadConfigFromFile: Can't get DirDepth!") + } + + BasePath := viper.GetString("AbsoluteServePath") + if BasePath == "" { + t.Error("TestLoadConfigFromFile: Can't get AbsoluteServePath!") + } +} diff --git a/server/server/path.go b/server/server/path.go index fb06512..286d13b 100644 --- a/server/server/path.go +++ b/server/server/path.go @@ -8,18 +8,9 @@ import ( "os" ) -func containsSlash(dir string) bool { - for _, c := range dir { - if c == '/' { - return true - } - } - return false -} - // PATH is the constant which should contain the fixed path where the simpleFTP server will run // This will act like a root cage. It must end with a forward slash! -var BasePath = "/Users/denis/GoglandProjects/golangBook/" +var BasePath = "" // MakePathFromStringStack gets a StringStack and makes a path. func MakePathFromStringStack(stack *StringStack) string { diff --git a/server/simplFTP.go b/server/simplFTP.go index 79dfaef..ebdd806 100644 --- a/server/simplFTP.go +++ b/server/simplFTP.go @@ -5,16 +5,24 @@ import ( "net" "github.com/metonimie/simpleFTP/server/server" + "github.com/spf13/viper" ) func main() { - listener, err := net.Listen("tcp", "localhost:8080") + server.InitializedConfiguration() + + Addr := viper.GetString("Address") + Port := viper.GetString("Port") + DirDepth := viper.GetInt("MaxDirDepth") + + // Start the server + listener, err := net.Listen("tcp", Addr+":"+Port) if err != nil { log.Fatal(err) } log.Println("Hello world!") - log.Println("Running on:", "localhost", "port", "8080") + log.Println("Running on:", Addr, "port", Port) for { @@ -25,7 +33,7 @@ func main() { } client := server.FTPClient{} - client.SetStack(server.MakeStringStack(30)) + client.SetStack(server.MakeStringStack(DirDepth)) client.SetConnection(conn) go server.HandleConnection(&client)