From 94fb8f24282be3aa41f275cb21d1eca76c1899a1 Mon Sep 17 00:00:00 2001 From: ache Date: Sun, 28 Feb 2021 21:59:58 +0100 Subject: Config + 1.16 + MultiSelection --- config/config.go | 17 ++++++++++--- config/config.json | 4 ++- go.mod | 3 +++ server.go | 71 +++++++++++++++++++++++++++++++++++------------------- src/dir.tmpl | 14 ++++++++--- 5 files changed, 76 insertions(+), 33 deletions(-) create mode 100644 go.mod diff --git a/config/config.go b/config/config.go index 48fbff4..ddf3f0a 100644 --- a/config/config.go +++ b/config/config.go @@ -9,15 +9,18 @@ import ( type config struct { Hostnames []string `json:"hosts"` Port int `json:"port"` - AllowLocalPath bool `json:"allow-local-path"` + AllowHidden bool `json:"allow-hidden"` Auth bool `json:"auth"` RootPath string `json:"root-path"` } var Config config -func ReadConfig() error { - file, err := os.Open("config.json") +func ReadConfig(path string) error { + if path == "" { + path = "config.json" + } + file, err := os.Open(path) if err != nil { return err @@ -31,3 +34,11 @@ func ReadConfig() error { return nil } + +func SetDefaultValue() { + Config.Hostnames = []string{"localhost"} + Config.Port = 8080 + Config.AllowHidden = false + Config.Auth = false + Config.RootPath = "." +} diff --git a/config/config.json b/config/config.json index 2bd396d..74dac99 100644 --- a/config/config.json +++ b/config/config.json @@ -1,4 +1,6 @@ { "host":["localhost"], - "port": 8080 + "port": 8080, + "allow-hidden": false, + "auth": true } diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..99339b9 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module davy + +go 1.16 diff --git a/server.go b/server.go index 1bc72d2..aed70d8 100644 --- a/server.go +++ b/server.go @@ -9,10 +9,10 @@ import ( "html/template" "flag" "path" - "syscall" "net/url" "errors" - "./config" + "strconv" + "davy/config" ) var tmpl *template.Template; @@ -127,11 +127,16 @@ func handleGet( w http.ResponseWriter, r *http.Request, headOnly bool) { cols = append(cols, Entry{"..", path.Dir(initPath), 0, ""}) initPath += "/" } + for _, file := range fileInfo { if file.IsDir() { - cols = append(cols, Entry{file.Name(), initPath + file.Name(), 0, ""}) + if config.Config.AllowHidden || file.Name()[0] != '.' { + cols = append(cols, Entry{file.Name(), initPath + file.Name(), 0, ""}) + } } else { - files = append(files, Entry{file.Name(), initPath + file.Name(), file.Size(), normaliseSize(file.Size())}) + if config.Config.AllowHidden || file.Name()[0] != '.' { + files = append(files, Entry{file.Name(), initPath + file.Name(), file.Size(), normaliseSize(file.Size())}) + } } } type Collection struct { @@ -154,7 +159,7 @@ func handleGet( w http.ResponseWriter, r *http.Request, headOnly bool) { func handleDelete( w http.ResponseWriter, r *http.Request) { filename := getPath(r) - fmt.Printf("Trying to get [%s]\n", filename) + fmt.Printf("Trying to delete [%s]\n", filename) if verifyEmptyBody(w, r) != nil { return } @@ -179,24 +184,33 @@ func handleDelete( w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) // Default success code ¯\_(ツ)_/¯ cf : 9.6.1.8 } func handlePut( w http.ResponseWriter, r *http.Request) { + + filename := r.URL.Path[1:] if filename == "" { filename = "." } + fmt.Printf("Trying to create [%s]\n", filename) + stat, err := os.Stat(path.Dir(filename)) - if err == syscall.ENOENT { + if os.IsNotExist(err) { w.WriteHeader(http.StatusConflict) fmt.Fprintf(w, "Can't find [%s]\n)", path.Dir(filename)) - return + fmt.Fprintf(w, "Trying to create it") + err := os.MkdirAll(path.Dir(filename), os.ModePerm) + if err != nil && os.IsNotExist(err) { + return + } } else if err != nil{ w.WriteHeader(http.StatusInternalServerError) fmt.Fprintln(w, err.Error()) + fmt.Printf("Error Abording on [%s] about %s\n", filename, err) return - } else if ! stat.IsDir(){ + } else if !stat.IsDir(){ w.WriteHeader(http.StatusConflict) - fmt.Fprintln(w, "¯\\_(ツ)_/¯") + fmt.Fprintf(w, "¯\\_(ツ)_/¯ [Error: %s]\n", err) return } @@ -380,22 +394,20 @@ func handleMethod( w http.ResponseWriter, r *http.Request) { } } func main() { - var dir string + var dirRoot string var help, version bool helpString := `This is the help` versionString := "0.0.0" - config.ReadConfig() - const ( - defaultDir = "." + defaultRoot = "." usageDir = "the directory to serve" usageHelp = "show some help" usageVersion = "show the version" ) - flag.StringVar(&dir, "directory", defaultDir, usageDir) - flag.StringVar(&dir, "d", defaultDir, usageDir+" (shorthand)") + flag.StringVar(&dirRoot, "directory", "", usageDir) + flag.StringVar(&dirRoot, "d", "", usageDir + " (shorthand)") flag.BoolVar(&help, "help", false, usageHelp) flag.BoolVar(&help, "h", false, usageHelp+" (shorthand)") flag.BoolVar(&version, "version", false, usageVersion) @@ -403,12 +415,6 @@ func main() { flag.Parse() - fmt.Println(config.Config.Port) - - if dir == "" { - dir = defaultDir - } - if help { fmt.Println(helpString) return @@ -418,6 +424,21 @@ func main() { return } + if config.ReadConfig("") != nil { + if config.ReadConfig("config/config.json") != nil { + fmt.Println("Config file error.") + fmt.Println("Charging default value") + config.SetDefaultValue() + } + } + + if dirRoot == "" { + dirRoot = config.Config.RootPath + if dirRoot == "" { + dirRoot = defaultRoot + } + } + fmap := template.FuncMap{ "formatURL": encodeURL, "formatSize": normaliseSize, @@ -435,14 +456,14 @@ func main() { http.ServeFile(w, r, baseWd + "/static/ban.svg") }) - if err := os.Chdir(dir) ; err != nil { - fmt.Println("Error with directory [" + dir + "]") + if err := os.Chdir(dirRoot) ; err != nil { + fmt.Println("Error with directory [" + dirRoot + "]") return } - fmt.Println("Launch server") + fmt.Println("Launch server on port:", config.Config.Port) http.HandleFunc("/", handleMethod) - log.Fatal(http.ListenAndServe(":8080", nil)) + log.Fatal(http.ListenAndServe(":" + strconv.Itoa(config.Config.Port), nil)) fmt.Println("Bye bye") } diff --git a/src/dir.tmpl b/src/dir.tmpl index f88b0ed..2dceade 100644 --- a/src/dir.tmpl +++ b/src/dir.tmpl @@ -27,6 +27,10 @@
+
+
+ +
@@ -52,7 +56,8 @@