Skip to content

Instantly share code, notes, and snippets.

@bertabus-zz
Last active June 6, 2016 03:02
Show Gist options
  • Select an option

  • Save bertabus-zz/436be4b5a87239deee659aeee4e73bf8 to your computer and use it in GitHub Desktop.

Select an option

Save bertabus-zz/436be4b5a87239deee659aeee4e73bf8 to your computer and use it in GitHub Desktop.
// This is a simple fileserver that serves wherever it is called.
// similar to a "terminal here" feature but for a fileserver.
// This makes it simple to quickly share some files someone,
// the default port is 8080 and it prints the ip addresses of
// pertinent interfaces.
//
// for example
//
// user@pc % pwd
// /home/user/Desktop
// user@pc % ServeFiles
// wlp3s0 -> 192.168.1.108/24
// wlp3s0 -> fe80::7e5c:f8ff:fe8f:8784/64
// listening at localhost:8080
package main
import (
"flag"
"fmt"
"net"
"net/http"
)
var port, root string
func init() {
//Declare Command line Arguments
flag.StringVar(&port, "port", "8080", "Define what TCP port to bind to")
flag.StringVar(&root, "root", ".", "Define the root filesystem path")
}
func main() {
flag.Parse()
//Print out ip addresses for each interface
interfaces, _ := net.Interfaces()
for _, inter := range interfaces {
fmt.Println(inter.Name, inter.HardwareAddr)
if addrs, err := inter.Addrs(); err == nil {
for _, addr := range addrs {
fmt.Println(inter.Name, "->", addr)
}
}
}
//Print out help info
fmt.Println("listening at localhost:" + port)
//Serve files
http.Handle("/", http.FileServer(http.Dir(root)))
http.ListenAndServe(":"+port, nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment