Created
September 25, 2013 03:53
-
-
Save gongo/6694993 to your computer and use it in GitHub Desktop.
指定したディレクトリ以下のファイルを指定したポートでブラウザから閲覧できるようにするやつの練習
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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