From bba06b2161088168baf1a70e02c2303b824ab0ad Mon Sep 17 00:00:00 2001 From: milarin Date: Thu, 9 Feb 2023 13:36:13 +0100 Subject: [PATCH] initial commit --- go.mod | 5 +++++ go.sum | 2 ++ main.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..53a9710 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.milar.in/milarin/httpserver + +go 1.19 + +require git.milar.in/milarin/adverr v1.1.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..63f97c1 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +git.milar.in/milarin/adverr v1.1.0 h1:jD9WnOvs40lfMhvqQ7cllOaRJNBMWr1f07/s9jAadp0= +git.milar.in/milarin/adverr v1.1.0/go.mod h1:joU9sBb7ySyNv4SpTXB0Z4o1mjXsArBw4N27wjgzj9E= diff --git a/main.go b/main.go new file mode 100644 index 0000000..f2ec209 --- /dev/null +++ b/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "net/http" + "flag" + "fmt" + "path/filepath" + + "git.milar.in/milarin/adverr" +) + +var ( + root = flag.String("r", ".", "root directory") + iface = flag.String("i", "localhost", "network interface") + port = flag.Uint64("p", 8080, "port") +) + +func main() { + flag.Parse() + + abspath, err := filepath.Abs(*root) + if err != nil { + adverr.Fatalln(err, 1) + } + + fmt.Printf("Root Directory: %s\nURL: http://%s:%d/\n", abspath, *iface, *port) + connStr := fmt.Sprintf("%s:%d", *iface, *port) + handler := http.FileServer(http.Dir(*root)) + if err := http.ListenAndServe(connStr, handler); err != nil { + adverr.Fatalln(err, 1) + } +}