From dc29c7e989f8761d902e8ffcbc19c131f893303a Mon Sep 17 00:00:00 2001 From: ache Date: Thu, 21 Feb 2019 10:33:46 +0100 Subject: Config package --- config/config.go | 33 +++++++++++++++++++++++++++++++++ config/config.json | 4 ++++ server.go | 7 ++++++- 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 config/config.go create mode 100644 config/config.json diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..b46b0e1 --- /dev/null +++ b/config/config.go @@ -0,0 +1,33 @@ +package config + +import ( + "encoding/json" + "io/ioutil" + "os" +) + +type config struct { + Hostnames []string `json:"host"` + Port int `json:"port"` + AllowLocalPath bool `json:"allow-local-path"` + Auth bool `json:"auth"` + RootPath string `json:"root-path"` +} + +var Config config + +func ReadConfig() error { + file, err := os.Open("config.json") + + if err != nil { + return err + } else { + defer file.Close() + } + + content, _ := ioutil.ReadAll(file) + + json.Unmarshal(content, &Config) + + return nil +} diff --git a/config/config.json b/config/config.json new file mode 100644 index 0000000..2bd396d --- /dev/null +++ b/config/config.json @@ -0,0 +1,4 @@ +{ + "host":["localhost"], + "port": 8080 +} diff --git a/server.go b/server.go index 3b5c732..ad32199 100644 --- a/server.go +++ b/server.go @@ -10,6 +10,7 @@ import ( "flag" "path" "syscall" + "./config" ) var tmpl *template.Template @@ -115,7 +116,7 @@ func handleGet( w http.ResponseWriter, r *http.Request, headOnly bool) { ListCols []Entry ListFiles []Entry } - tmpl.Execute(w, Collection{ "Title" , cols, files}) + tmpl.Execute(w, Collection{ filename, cols, files}) case stat.Mode().IsRegular(): if !headOnly { @@ -324,6 +325,8 @@ func main() { helpString := `This is the help` versionString := "0.0.0" + config.ReadConfig() + const ( defaultDir = "." usageDir = "the directory to serve" @@ -339,6 +342,8 @@ func main() { flag.Parse() + fmt.Println(config.Config.Port) + if dir == "" { dir = defaultDir } -- cgit v1.2.3