Skip to content

Instantly share code, notes, and snippets.

@cshuaimin
Created August 10, 2019 03:50
Show Gist options
  • Save cshuaimin/0a7d8b4a79bdd320ef065af778bb3682 to your computer and use it in GitHub Desktop.
Save cshuaimin/0a7d8b4a79bdd320ef065af778bb3682 to your computer and use it in GitHub Desktop.
Transfer files over HTTP in the LAN
package main
import (
"fmt"
"net"
"net/http"
"os"
)
func main() {
args := os.Args
root := "."
port := "8080"
if len(args) >= 2 {
if args[1] == "-h" {
fmt.Println("Serve files over HTTP.\n> sf [path] [port]")
return
}
root = args[1]
}
if len(args) >= 3 {
port = args[2]
}
fmt.Println("URLs:")
addrs, err := net.InterfaceAddrs()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
for _, addr := range addrs {
ip := addr.(*net.IPNet).IP
if !ip.IsLinkLocalUnicast() {
host := ip.String()
if ip.To4() == nil {
host = fmt.Sprintf("[%s]", host)
}
fmt.Printf(" http://%s:%s\n", host, port)
}
}
serve(root, port)
}
func serve(root string, port string) {
http.Handle("/", http.FileServer(http.Dir(root)))
fmt.Println(http.ListenAndServe(":"+port, nil))
os.Exit(1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment