This commit is contained in:
milarin 2023-02-10 10:11:02 +01:00
parent bba06b2161
commit a1e1413919

21
main.go
View File

@ -1,18 +1,19 @@
package main package main
import ( import (
"net/http"
"flag" "flag"
"fmt" "fmt"
"log"
"net/http"
"path/filepath" "path/filepath"
"git.milar.in/milarin/adverr" "git.milar.in/milarin/adverr"
) )
var ( var (
root = flag.String("r", ".", "root directory") root = flag.String("r", ".", "root directory")
iface = flag.String("i", "localhost", "network interface") iface = flag.String("i", "localhost", "network interface")
port = flag.Uint64("p", 8080, "port") port = flag.Uint64("p", 8080, "port")
) )
func main() { func main() {
@ -22,11 +23,21 @@ func main() {
if err != nil { if err != nil {
adverr.Fatalln(err, 1) adverr.Fatalln(err, 1)
} }
*root = abspath
fmt.Printf("Root Directory: %s\nURL: http://%s:%d/\n", *root, *iface, *port)
fmt.Printf("Root Directory: %s\nURL: http://%s:%d/\n", abspath, *iface, *port)
connStr := fmt.Sprintf("%s:%d", *iface, *port) connStr := fmt.Sprintf("%s:%d", *iface, *port)
handler := http.FileServer(http.Dir(*root)) handler := LogHandler(http.FileServer(http.Dir(*root)))
if err := http.ListenAndServe(connStr, handler); err != nil { if err := http.ListenAndServe(connStr, handler); err != nil {
adverr.Fatalln(err, 1) adverr.Fatalln(err, 1)
} }
} }
func LogHandler(handler http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s", r.Method, r.URL)
handler.ServeHTTP(w, r)
}
}