Created
April 26, 2016 05:54
-
-
Save mkideal/7ca16a1dc5a736a2149b31af2714d0f2 to your computer and use it in GitHub Desktop.
demo http server written by golang with mkideal/cli
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" | |
"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