Created
September 25, 2014 10:19
-
-
Save sudix/04ab63f5a0852256e818 to your computer and use it in GitHub Desktop.
This file contains 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" | |
"net/http" | |
"os" | |
"github.com/codegangsta/cli" | |
) | |
var ( | |
directory string | |
port int | |
) | |
func setAppInfo(app *cli.App) { | |
app.Name = "simple server" | |
app.Usage = "simple content delivery server" | |
app.Version = "0.0.1" | |
} | |
func setFlags(app *cli.App) { | |
app.Flags = []cli.Flag{ | |
cli.StringFlag{ | |
Name: "directory, d", | |
Value: "~/", | |
Usage: "contents directory", | |
}, | |
cli.IntFlag{ | |
Name: "port, p", | |
Value: 8080, | |
Usage: "proxy's port.", | |
}, | |
} | |
} | |
func main() { | |
app := cli.NewApp() | |
setAppInfo(app) | |
setFlags(app) | |
app.Action = func(c *cli.Context) { | |
directory = c.String("directory") | |
port = c.Int("port") | |
http.Handle("/", http.FileServer(http.Dir(directory))) | |
fmt.Printf("Listening %d port\n", port) | |
host := fmt.Sprintf("127.0.0.1:%d", port) | |
http.ListenAndServe(host, nil) | |
} | |
app.Run(os.Args) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
こちらの環境(Ubuntu Linux 14.04 + golang 1.3)ですと "~/" がホームディレクトリに展開されない様でして、ブラウザでアクセスすると”404 page not found” となってしまいます。代わりに 'Value: os.Getenv("HOME")' としておいた方が良いでのではないかと(HOME変数が未定義の場合を考えなくてはなりませんが…)。