git.sophuwu.com > urlshort
added args to change the listen port and ip
sophuwu sophie@skisiel.com
Thu, 10 Apr 2025 01:53:38 +0200
commit

9e88e901b7799532258fc44bbdf7fcc9df64818f

parent

3aff2431affebd2af7b52ed31d17249da9d6b5fb

1 files changed, 33 insertions(+), 6 deletions(-)

jump to
M main.gomain.go

@@ -343,25 +343,52 @@ url.Uses++

_ = writeURL(url) } +const helpstring = ` [options] +options: + -i[str] + str is the address (or hostname thereof) the server will to listen to. + if -i option is given with no str, the server will listen on all + possible addresses. + if this option is not present, 127.0.0.1 will be used by default + -p[num] + num is the port number to use for http traffic +` + +func parseArgs(iface, port *string) { + for _,arg := range os.Args[1:] { + if len(arg)>1 && arg[0]=='-' { + switch arg[1] { + case 'i': + *iface=arg[2:] + continue + case 'p': + *port=arg[2:] + continue + } + } + println("usage: "+os.Args[0]+helpstring) + os.Exit(1) + } +} + func main() { var ( - port string = ":8088" + iface string = "127.0.0.1" + port string = "8088" err error server http.Server ) - if len(os.Args) == 2 { - port = ":" + os.Args[1] - } + parseArgs(&iface,&port) - println("Starting server on port " + port) + println("Starting server on " + iface + ":" + port) openFiles() http.HandleFunc("/", httpHandler) server = http.Server{ - Addr: port, + Addr: iface + ":" + port, Handler: nil, }