Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions cmd/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package cmd

import (
"fmt"
"net"
"os"
"os/signal"
"syscall"
Expand Down Expand Up @@ -49,14 +51,13 @@ func createMockCmd() (c *cobra.Command) {
func (o *mockOption) runE(c *cobra.Command, args []string) (err error) {
reader := mock.NewLocalFileReader(args[0])
server := mock.NewInMemoryServer(o.port)

c.Println("start listen", o.port)
if err = server.Start(reader, o.prefix); err != nil {
return
}

clean := make(chan os.Signal, 1)
signal.Notify(clean, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT)
printLocalIPs(c, o.port)

select {
case <-c.Context().Done():
Expand All @@ -65,3 +66,28 @@ func (o *mockOption) runE(c *cobra.Command, args []string) (err error) {
err = server.Stop()
return
}

func printLocalIPs(c *cobra.Command, port int) {
if ips, err := getLocalIPs(); err == nil {
for _, ip := range ips {
c.Printf("server is available at http://%s:%d\n", ip, port)
}
}
}

func getLocalIPs() ([]string, error) {
var ips []string
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil, fmt.Errorf("failed to get interface addresses: %v", err)
}

for _, addr := range addrs {
if ipNet, ok := addr.(*net.IPNet); ok {
if ipNet.IP.To4() != nil && !ipNet.IP.IsLoopback() {
ips = append(ips, ipNet.IP.String())
}
}
}
return ips, nil
}
1 change: 1 addition & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ func (o *serverOption) runE(cmd *cobra.Command, args []string) (err error) {
serverLogger.Info("HTTP server started", "addr", httplis.Addr())
serverLogger.Info("gRPC server started", "addr", lis.Addr())
serverLogger.Info("Server is running.")
printLocalIPs(cmd, o.httpPort)

err = o.httpServer.Serve(httplis)
err = util.IgnoreErrServerClosed(err)
Expand Down
Loading
Loading