Implementing configuration settings from file
This commit is contained in:
parent
11905cd654
commit
87c40eed1a
5 changed files with 110 additions and 14 deletions
22
README.md
22
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
|
The scope of this project is to implement a simple server that handles multiple clients and allows the clients to
|
||||||
execute commands on it.
|
execute commands on it.
|
||||||
|
|
||||||
The client is soon to come.
|
## 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.
|
||||||
|
|
39
server/server/config.go
Normal file
39
server/server/config.go
Normal file
|
@ -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")
|
||||||
|
}
|
38
server/server/config_test.go
Normal file
38
server/server/config_test.go
Normal file
|
@ -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!")
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,18 +8,9 @@ import (
|
||||||
"os"
|
"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
|
// 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!
|
// 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.
|
// MakePathFromStringStack gets a StringStack and makes a path.
|
||||||
func MakePathFromStringStack(stack *StringStack) string {
|
func MakePathFromStringStack(stack *StringStack) string {
|
||||||
|
|
|
@ -5,16 +5,24 @@ import (
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/metonimie/simpleFTP/server/server"
|
"github.com/metonimie/simpleFTP/server/server"
|
||||||
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
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 {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("Hello world!")
|
log.Println("Hello world!")
|
||||||
log.Println("Running on:", "localhost", "port", "8080")
|
log.Println("Running on:", Addr, "port", Port)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
|
||||||
|
@ -25,7 +33,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
client := server.FTPClient{}
|
client := server.FTPClient{}
|
||||||
client.SetStack(server.MakeStringStack(30))
|
client.SetStack(server.MakeStringStack(DirDepth))
|
||||||
client.SetConnection(conn)
|
client.SetConnection(conn)
|
||||||
|
|
||||||
go server.HandleConnection(&client)
|
go server.HandleConnection(&client)
|
||||||
|
|
Loading…
Reference in a new issue