From f61736266da333a43ae99e0f246f6a0c8dfdbc1d Mon Sep 17 00:00:00 2001 From: Piotr Biernat Date: Wed, 29 Jul 2020 16:21:55 +0200 Subject: [PATCH] v0.5 --- main.go | 28 ++++++++++++++++ modd.conf | 4 +++ public/dir/otherFile.html | 1 + public/favicon.ico | Bin 0 -> 5686 bytes public/someFile.html | 1 + server.go | 68 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 102 insertions(+) create mode 100644 main.go create mode 100644 modd.conf create mode 100644 public/dir/otherFile.html create mode 100644 public/favicon.ico create mode 100644 public/someFile.html create mode 100644 server.go diff --git a/main.go b/main.go new file mode 100644 index 0000000..a052a76 --- /dev/null +++ b/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "flag" + "fmt" + "os" +) + +func main() { + var fdir string + flag.StringVar(&fdir, "d", "", "path to dir to serve") + flag.Parse() + + if fdir == "" { + printHelp() + os.Exit(0) + } + + s := Server{ + port: ":8080", + dirPath: fdir, + } + s.Serve() +} + +func printHelp() { + fmt.Println("Usage: -d path to dir to serve") +} diff --git a/modd.conf b/modd.conf new file mode 100644 index 0000000..3eca06b --- /dev/null +++ b/modd.conf @@ -0,0 +1,4 @@ +**/*.go !**/*_test.go { + prep: go build -o server main.go server.go + daemon +sigterm: ./server -d public +} \ No newline at end of file diff --git a/public/dir/otherFile.html b/public/dir/otherFile.html new file mode 100644 index 0000000..d4745c1 --- /dev/null +++ b/public/dir/otherFile.html @@ -0,0 +1 @@ +No content in this file \ No newline at end of file diff --git a/public/favicon.ico b/public/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..8d225846dbcda496ba803b828c4786b21cc84d01 GIT binary patch literal 5686 zcmeHL4Qx|Y6h33c%}oagh()0_%1;O(L4mbFbVLmNO!o_pK(h=QmG>Gj0Dp1$i%G?a*Xb00*Zl+=^xHy#gX9;;9klhC_gGfM^tl54Hq zN-B=8KPo?fAIN`q)__`Pf%@y10W~k(nR{Vd#p#Gnr1As!|Bv%uUN{Fw^JXYieKe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f --- /dev/null +++ b/server.go @@ -0,0 +1,68 @@ +package main + +import ( + "io/ioutil" + "log" + "net/http" + "os" + "strings" +) + +// Server base struct +type Server struct { + port string + dirPath string +} + +func (s *Server) initHandler() { + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + path := s.dirPath + r.URL.Path + file, err := os.Lstat(path) + if err != nil { + log.Println("File " + path + " not exists") + http.NotFound(w, r) + return + } + + switch mode := file.Mode(); { + case mode.IsRegular(): + serveFile(w, r, path) + case mode.IsDir(): + serveDir(w, r, path) + } + }) + + log.Printf("Serving %v directory\n", s.dirPath) +} + +// Serve function +func (s *Server) Serve() { + s.initHandler() + + log.Print("Listening on", s.port) + log.Fatal(http.ListenAndServe(s.port, nil)) +} + +func serveDir(w http.ResponseWriter, r *http.Request, path string) { + log.Println("Serving " + path + " dir...") + dirList, err := ioutil.ReadDir(path) + if err != nil { + log.Fatalln(err) + return + } + + var outputDirList []string + for _, file := range dirList { + fileURL := strings.TrimRight(r.RequestURI, "/") + "/" + file.Name() + outputDirList = append(outputDirList, ""+file.Name()+"") + } + + response := strings.Join(outputDirList, "
") + w.Write([]byte(string(response))) +} + +func serveFile(w http.ResponseWriter, r *http.Request, path string) { + log.Println("Serving " + path + " file...") + + http.ServeFile(w, r, path) +}