Skip to content

Instantly share code, notes, and snippets.

@gongo
Created September 25, 2013 03:53
Show Gist options
  • Save gongo/6694993 to your computer and use it in GitHub Desktop.
Save gongo/6694993 to your computer and use it in GitHub Desktop.
指定したディレクトリ以下のファイルを指定したポートでブラウザから閲覧できるようにするやつの練習
package main
import (
"fmt"
"github.com/mattn/go-options"
"net/http"
"os"
"path/filepath"
)
var opts = options.Options{
{"p", "8080", "Set the port"},
{"d", ".", "Set the document root"},
{"h", false, "Show this message"},
}
func main() {
if err := opts.Parse(); err != nil || opts.Bool("h") {
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
opts.Usage()
}
dir, _ := filepath.Abs(opts.String("d"))
port := opts.String("p")
fmt.Println("Start server (document root = " + dir + ")")
err := http.ListenAndServe(":"+port, http.FileServer(http.Dir(dir)))
if err != nil {
panic(err)
}
}
// $ ./static_serve -h
// Usage: ./static_serve [options] [--] [args]
// -p="8080": Set the port
// -d=".": Set the document root
// -h(false): Show this message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment