Skip to content

Instantly share code, notes, and snippets.

@mazza
Last active September 28, 2018 04:03
Show Gist options
  • Save mazza/4a1bd31f4cd1ac89c7431251b9884ecd to your computer and use it in GitHub Desktop.
Save mazza/4a1bd31f4cd1ac89c7431251b9884ecd to your computer and use it in GitHub Desktop.
Shell script for a quick/didactic showing of Go powers (http.FileServer) + stuff
#!/usr/bin/env bash
cd "`dirname $0`"
# check if the programs we use are available
command -v go >/dev/null 2>&1 || {
echo "go should be installed! ( Go https://golang.org )"
xdg-open "https://golang.org/dl/" >/dev/null 2>&1
exit 1
}
command -v gox >/dev/null 2>&1 || {
echo "gox package should be installed!"
xdg-open "https://github.com/mitchellh/gox" >/dev/null 2>&1
echo "install with command:"
echo " go get -u github.com/mitchellh/gox"
exit 1
}
export PS4="# "
set -x
rm -rf serve archive go-serve{.7z,.zip,.tar.*} serve.bin serve.go
# exit
mkdir serve archive
cat > serve.go <<'EOF'
package main
import (
"flag"
"fmt"
"log"
"net"
"net/http"
)
func main() {
port := flag.String("p", "8090", "port to serve on")
path := flag.String("d", ".", "the path of static file to host")
flag.Parse()
fmt.Println("Your IPs in this network (you may open on your web browser):")
if each, err := net.Interfaces(); err == nil {
for _, i := range each {
if addrs, err := i.Addrs(); err == nil {
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
default:
continue
}
if !ip.IsLoopback() {
if ip.To4() == nil {
fmt.Printf(" http://[%v]:%v\n", ip, *port)
} else {
fmt.Printf(" http://%v:%v\n", ip, *port)
}
}
}
} else {
fmt.Println(fmt.Errorf("error at \"addrs, err := i.Addrs()\n%v\n\"", err))
}
}
} else {
fmt.Println(fmt.Errorf("error at \"each, err := net.Interfaces():\n%v\n\"", err))
}
http.Handle("/", http.FileServer(http.Dir(*path)))
fmt.Println()
log.Printf("Serving %s on HTTP port: %s\n", *path, *port)
log.Fatal(http.ListenAndServe(":"+*port, nil))
}
EOF
command -v gofmt >/dev/null 2>&1 && {
gofmt -w *.go
}
mkdir serve
cp *.go ./serve/
gox -output="serve/serve-{{.OS}}_{{.Arch}}"
command -v md5sum >/dev/null 2>&1 && {
echo
md5sum ./serve/* | tee -i ./serve/md5.txt
}
du -sh ./serve/serve* | tee -i ./serve/du.txt
echo
command -v zip >/dev/null 2>&1 && zip -r go-serve.zip serve
command -v 7z >/dev/null 2>&1 && {
Ultra="-t7z -m0=lzma2 -mx=9 -mfb=64 -md=32m -ms=on"
echo
# Basic 7z compression:
# 7z a go-serve.7z serve-*
# Create a tarball.tar.7z:
tar cf - serve | 7z a $Ultra -si archive/go-serve.tar.7z
# Extract tar.7z:
# 7z x -so go-serve.tar.7z | tar xf - # to extract
# Create .7z
7z a $Ultra archive/go-serve.7z serve
}
tar cfJ archive/go-serve.tar.xz serve
tar -cvjSf archive/go-serve.tar.bz2 serve
tar zcf archive/go-serve.tar.gz serve
echo
du -sh archive/*
command -v md5sum >/dev/null 2>&1 && {
echo
md5sum archive/*
}
( sleep 2 && xdg-open "http://127.0.0.1:8090" >/dev/null 2>&1 )&
go build -o serve.bin && ./serve.bin
package main
import (
"flag"
"fmt"
"log"
"net"
"net/http"
)
func main() {
port := flag.String("p", "8090", "port to serve on")
path := flag.String("d", ".", "the path of static file to host")
flag.Parse()
fmt.Println("Your IPs in this network (you may open on your web browser):")
if each, err := net.Interfaces(); err == nil {
for _, i := range each {
if addrs, err := i.Addrs(); err == nil {
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
default:
continue
}
if !ip.IsLoopback() {
if ip.To4() == nil {
fmt.Printf(" http://[%v]:%v\n", ip, *port)
} else {
fmt.Printf(" http://%v:%v\n", ip, *port)
}
}
}
} else {
fmt.Println(fmt.Errorf("error at \"addrs, err := i.Addrs()\n%v\n\"", err))
}
}
} else {
fmt.Println(fmt.Errorf("error at \"each, err := net.Interfaces():\n%v\n\"", err))
}
http.Handle("/", http.FileServer(http.Dir(*path)))
fmt.Println()
log.Printf("Serving %s on HTTP port: %s\n", *path, *port)
log.Fatal(http.ListenAndServe(":"+*port, nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment