Adding configuration support for pic

This commit is contained in:
Denis-Cosmin Nutiu 2017-11-24 22:50:23 +02:00
parent 05f4d8173d
commit 12b11a0fa1
3 changed files with 34 additions and 9 deletions

View file

@ -5,6 +5,10 @@ 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.
## Commands
//todo
## Configuration
The server can be configured via command line flags with the -ConfigPath option,
@ -17,13 +21,25 @@ Sample Configuration File:
"address": "localhost",
"port": "8080",
"maxDirDepth": 30,
"absoluteServePath": "/Users/denis/Dropbox/Pictures/CuteAvatars"
"absoluteServePath": "/Users/denis/Dropbox/Pictures/CuteAvatars",
"pic": {
"x": 0,
"y": 0
}
}
```
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.
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.
pic - The X and Y max size for the pic command. A value of 0 means original size
If one of the settings are changed, the server will reload the configuration.
Except for the absoluteServePath.

View file

@ -9,6 +9,7 @@ import (
"strconv"
"strings"
"github.com/spf13/viper"
"github.com/zyxar/image2ascii/ascii"
)
@ -21,8 +22,8 @@ func SendAsciiPic(c Client, path string) error {
}
defer f.Close()
opt := ascii.Options{
Width: 0,
Height: 0,
Width: viper.GetInt("pic.x"),
Height: viper.GetInt("pic.y"),
Color: false,
Invert: false,
Flipx: false,

View file

@ -6,6 +6,7 @@ import (
"flag"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
@ -31,6 +32,8 @@ func setDefaultConfiguration() {
viper.SetDefault("configPath", ConfigPath)
viper.SetDefault("maxDirDepth", 30)
viper.SetDefault("absoluteServePath", "./")
viper.SetDefault("pic.x", 0)
viper.SetDefault("pic.y", 0)
}
// InitializedConfiguration initializes the configuration for the application.
@ -39,7 +42,12 @@ func InitializedConfiguration() {
flag.Parse()
setDefaultConfiguration()
loadConfigFromFile()
BasePath = viper.GetString("AbsoluteServePath")
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
log.Println("Reloaded configuration file successfully!")
})
BasePath = viper.GetString("absoluteServePath")
}