Refactor network handling and endpoint retrieval; deprecate unused commands in startup
This commit is contained in:
parent
8388b78a0e
commit
6b6edab97c
3 changed files with 95 additions and 37 deletions
15
cmd/root.go
15
cmd/root.go
|
|
@ -7,7 +7,6 @@ import (
|
|||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/yaoapp/kun/exception"
|
||||
"github.com/yaoapp/yao/cmd/studio"
|
||||
"github.com/yaoapp/yao/cmd/sui"
|
||||
"github.com/yaoapp/yao/config"
|
||||
"github.com/yaoapp/yao/pack"
|
||||
|
|
@ -132,7 +131,7 @@ var suiCmd = &cobra.Command{
|
|||
// Command initialize
|
||||
func init() {
|
||||
|
||||
studioCmd.AddCommand(studio.RunCmd)
|
||||
// studioCmd.AddCommand(studio.RunCmd)
|
||||
|
||||
// Sui
|
||||
suiCmd.AddCommand(sui.WatchCmd)
|
||||
|
|
@ -145,15 +144,15 @@ func init() {
|
|||
inspectCmd,
|
||||
startCmd,
|
||||
runCmd,
|
||||
getCmd,
|
||||
dumpCmd,
|
||||
restoreCmd,
|
||||
// getCmd,
|
||||
// dumpCmd,
|
||||
// restoreCmd,
|
||||
// socketCmd,
|
||||
// websocketCmd,
|
||||
packCmd,
|
||||
studioCmd,
|
||||
// packCmd,
|
||||
// studioCmd,
|
||||
suiCmd,
|
||||
upgradeCmd,
|
||||
// upgradeCmd,
|
||||
)
|
||||
// rootCmd.SetHelpCommand(helpCmd)
|
||||
rootCmd.PersistentFlags().StringVarP(&appPath, "app", "a", "", L("Application directory"))
|
||||
|
|
|
|||
51
cmd/start.go
51
cmd/start.go
|
|
@ -116,12 +116,6 @@ var startCmd = &cobra.Command{
|
|||
fmt.Println(color.WhiteString(L("Data")), color.GreenString(" %s", dataRoot))
|
||||
fmt.Println(color.WhiteString(L("Listening")), color.GreenString(" %s:%d", config.Conf.Host, config.Conf.Port))
|
||||
|
||||
root, _ := adminRoot()
|
||||
urls := []string{fmt.Sprintf("http://%s:%s", host, port)}
|
||||
if host == "0.0.0.0" {
|
||||
urls, _ = setup.URLs(config.Conf)
|
||||
}
|
||||
|
||||
// print the messages under the development mode
|
||||
if mode == "development" {
|
||||
|
||||
|
|
@ -153,12 +147,45 @@ var startCmd = &cobra.Command{
|
|||
|
||||
}
|
||||
|
||||
for _, url := range urls {
|
||||
fmt.Println(color.CyanString("\n%s", url))
|
||||
root, _ := adminRoot()
|
||||
endpoints := []setup.Endpoint{{URL: fmt.Sprintf("http://%s:%s", "127.0.0.1", port), Interface: "localhost"}}
|
||||
switch host {
|
||||
case "0.0.0.0":
|
||||
// All interfaces
|
||||
if values, err := setup.Endpoints(config.Conf); err == nil {
|
||||
endpoints = append(endpoints, values...)
|
||||
}
|
||||
break
|
||||
case "127.0.0.1":
|
||||
// Localhost only
|
||||
break
|
||||
default:
|
||||
// Filter by the host IP
|
||||
matched := false
|
||||
endpoints = []setup.Endpoint{}
|
||||
if values, err := setup.Endpoints(config.Conf); err == nil {
|
||||
for _, value := range values {
|
||||
if strings.HasPrefix(value.URL, fmt.Sprintf("http://%s:", host)) {
|
||||
endpoints = append(endpoints, value)
|
||||
matched = true
|
||||
}
|
||||
}
|
||||
}
|
||||
if !matched {
|
||||
fmt.Println(color.RedString(L("Host %s not found"), host))
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println(color.WhiteString("\n---------------------------------"))
|
||||
fmt.Println(color.WhiteString(L("Access Points")))
|
||||
fmt.Println(color.WhiteString("---------------------------------"))
|
||||
for _, endpoint := range endpoints {
|
||||
fmt.Println(color.CyanString("\n%s", endpoint.Interface))
|
||||
fmt.Println(color.WhiteString("--------------------------"))
|
||||
fmt.Println(color.WhiteString(L("Website")), color.GreenString(" %s", url))
|
||||
fmt.Println(color.WhiteString(L("Admin")), color.GreenString(" %s/%s/login/admin", url, strings.Trim(root, "/")))
|
||||
fmt.Println(color.WhiteString(L("API")), color.GreenString(" %s/api", url))
|
||||
fmt.Println(color.WhiteString(L("Website")), color.GreenString(" %s", endpoint.URL))
|
||||
fmt.Println(color.WhiteString(L("Admin")), color.GreenString(" %s/%s/login/admin", endpoint.URL, strings.Trim(root, "/")))
|
||||
fmt.Println(color.WhiteString(L("API")), color.GreenString(" %s/api", endpoint.URL))
|
||||
}
|
||||
fmt.Println("")
|
||||
|
||||
|
|
@ -419,7 +446,7 @@ func printApis(silent bool) {
|
|||
}
|
||||
|
||||
fmt.Println(color.WhiteString("\n---------------------------------"))
|
||||
fmt.Println(color.WhiteString(L("API List")))
|
||||
fmt.Println(color.WhiteString(L("APIs List")))
|
||||
fmt.Println(color.WhiteString("---------------------------------"))
|
||||
|
||||
for _, api := range api.APIs { // API信息
|
||||
|
|
|
|||
|
|
@ -9,19 +9,23 @@ import (
|
|||
"github.com/yaoapp/yao/config"
|
||||
)
|
||||
|
||||
// URLs get admin url
|
||||
func URLs(cfg config.Config) ([]string, error) {
|
||||
|
||||
ips, err := Ips()
|
||||
// Endpoints get endpoints
|
||||
func Endpoints(cfg config.Config) ([]Endpoint, error) {
|
||||
networks, err := getNetworks()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for i := range ips {
|
||||
ips[i] = fmt.Sprintf("http://%s:%d", ips[i], cfg.Port)
|
||||
var endpoints []Endpoint
|
||||
for _, network := range networks {
|
||||
endpoint := Endpoint{
|
||||
URL: fmt.Sprintf("http://%s:%d", network.IPv4, cfg.Port),
|
||||
Interface: network.Interface,
|
||||
}
|
||||
endpoints = append(endpoints, endpoint)
|
||||
}
|
||||
|
||||
return ips, nil
|
||||
return endpoints, nil
|
||||
}
|
||||
|
||||
func printError(message string, args ...interface{}) {
|
||||
|
|
@ -33,21 +37,49 @@ func printInfo(message string, args ...interface{}) {
|
|||
fmt.Println(color.GreenString(message, args...))
|
||||
}
|
||||
|
||||
// Ips get the local ip list
|
||||
func Ips() ([]string, error) {
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
func getNetworks() ([]Network, error) {
|
||||
interfaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
iplist := []string{"127.0.0.1"}
|
||||
for _, address := range addrs {
|
||||
// check the address type and if it is not a loopback the display it
|
||||
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
||||
if ipnet.IP.To4() != nil {
|
||||
iplist = append(iplist, ipnet.IP.String())
|
||||
var networks []Network
|
||||
for _, iface := range interfaces {
|
||||
// 跳过 loopback 接口(如 lo0)
|
||||
if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
// 获取每个接口的地址信息
|
||||
addrs, err := iface.Addrs()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 过滤只获取 IPv4 地址
|
||||
for _, addr := range addrs {
|
||||
if ipnet, ok := addr.(*net.IPNet); ok && ipnet.IP.To4() != nil {
|
||||
// 将网卡名称和 IPv4 地址存储到 Network 结构体中
|
||||
network := Network{
|
||||
IPv4: ipnet.IP.String(),
|
||||
Interface: iface.Name,
|
||||
}
|
||||
// 添加到结果切片
|
||||
networks = append(networks, network)
|
||||
}
|
||||
}
|
||||
}
|
||||
return iplist, nil
|
||||
return networks, nil
|
||||
}
|
||||
|
||||
// Network network
|
||||
type Network struct {
|
||||
IPv4 string
|
||||
Interface string
|
||||
}
|
||||
|
||||
// Endpoint endpoint
|
||||
type Endpoint struct {
|
||||
URL string
|
||||
Interface string
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue