Skip to content

Instantly share code, notes, and snippets.

@mkideal
Created April 26, 2016 05:54
Show Gist options
  • Save mkideal/7ca16a1dc5a736a2149b31af2714d0f2 to your computer and use it in GitHub Desktop.
Save mkideal/7ca16a1dc5a736a2149b31af2714d0f2 to your computer and use it in GitHub Desktop.
demo http server written by golang with mkideal/cli
package main
import (
"fmt"
"net/http"
"github.com/mkideal/cli"
)
type argT struct {
cli.Helper
Host string `cli:"H,host" usage:"specify host" dft:"0.0.0.0"`
Port uint16 `cli:"p,port" usage:"specify port" dft:"8080"`
Dir string `cli:"d,dir" usage:"static files directory" dft:"./"`
}
func main() {
cli.SetUsageStyle(cli.ManualStyle)
cli.Run(new(argT), func(ctx *cli.Context) error {
argv := ctx.Argv().(*argT)
if argv.Help {
ctx.WriteUsage()
return nil
}
http.Handle("/", http.FileServer(http.Dir(argv.Dir)))
addr := fmt.Sprintf("%s:%d", argv.Host, argv.Port)
ctx.String("listening on %s\n", addr)
http.ListenAndServe(addr, nil)
return nil
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment