Add flag to specify custom address

This commit is contained in:
Radhi Fadlillah 2019-08-27 12:36:24 +07:00
parent c22f904bdd
commit 806a3cff40
3 changed files with 65 additions and 63 deletions

View file

@ -17,14 +17,16 @@ func serveCmd() *cobra.Command {
} }
cmd.Flags().IntP("port", "p", 8080, "Port that used by server") cmd.Flags().IntP("port", "p", 8080, "Port that used by server")
cmd.Flags().StringP("address", "a", "", "Address the server listens to")
return cmd return cmd
} }
func serveHandler(cmd *cobra.Command, args []string) { func serveHandler(cmd *cobra.Command, args []string) {
port, _ := cmd.Flags().GetInt("port") port, _ := cmd.Flags().GetInt("port")
address, _ := cmd.Flags().GetString("address")
err := webserver.ServeApp(db, dataDir, port) err := webserver.ServeApp(db, dataDir, address, port)
if err != nil { if err != nil {
logrus.Fatalf("Server error: %v\n", err) logrus.Fatalf("Server error: %v\n", err)
} }

File diff suppressed because one or more lines are too long

View file

@ -15,7 +15,7 @@ import (
var httpClient = &http.Client{Timeout: time.Minute} var httpClient = &http.Client{Timeout: time.Minute}
// ServeApp serves wb interface in specified port // ServeApp serves wb interface in specified port
func ServeApp(DB database.DB, dataDir string, port int) error { func ServeApp(DB database.DB, dataDir string, address string, port int) error {
// Create handler // Create handler
hdl := handler{ hdl := handler{
DB: DB, DB: DB,
@ -68,7 +68,7 @@ func ServeApp(DB database.DB, dataDir string, port int) error {
} }
// Create server // Create server
url := fmt.Sprintf(":%d", port) url := fmt.Sprintf("%s:%d", address, port)
svr := &http.Server{ svr := &http.Server{
Addr: url, Addr: url,
Handler: router, Handler: router,