Skip to content

Commit

Permalink
extract listenAndServe method
Browse files Browse the repository at this point in the history
  • Loading branch information
missdeer committed Feb 5, 2024
1 parent e6762d6 commit b6a5b03
Showing 1 changed file with 38 additions and 35 deletions.
73 changes: 38 additions & 35 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,43 @@ func downloadBook(novelURL string, ch chan bool) {
ch <- false
}

func listenAndServe() {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
ifaces, err := net.Interfaces()
if err != nil {
log.Fatal(err)
}
var ips []string
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
fmt.Println(err)
continue
}
for _, addr := range addrs {
switch v := addr.(type) {
case *net.IPNet:
if v.IP.IsLoopback() || v.IP.IsLinkLocalMulticast() || v.IP.IsLinkLocalUnicast() {
break
}
ips = append(ips, "\t"+v.IP.String())
case *net.IPAddr:
if v.IP.IsLoopback() || v.IP.IsLinkLocalMulticast() || v.IP.IsLinkLocalUnicast() {
break
}
ips = append(ips, "\t"+v.IP.String())
}
}
}
fmt.Println("Local IP:")
fmt.Println(strings.Join(ips, "\n"))
fmt.Println("starting http server on", config.Opts.ListenAndServe)
log.Fatal(http.ListenAndServe(config.Opts.ListenAndServe, http.FileServer(http.Dir(dir))))
}

func main() {
luaVersion := lua.GetLuaRelease()
luajitVersion := lua.GetLuaJITVersion()
Expand Down Expand Up @@ -144,41 +181,7 @@ func main() {
}

if config.Opts.ListenAndServe != "" {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
ifaces, err := net.Interfaces()
if err != nil {
log.Fatal(err)
}
var ips []string
for _, i := range ifaces {

addrs, err := i.Addrs()
if err != nil {
fmt.Println(err)
continue
}
for _, addr := range addrs {
switch v := addr.(type) {
case *net.IPNet:
if v.IP.IsLoopback() || v.IP.IsLinkLocalMulticast() || v.IP.IsLinkLocalUnicast() {
break
}
ips = append(ips, "\t"+v.IP.String())
case *net.IPAddr:
if v.IP.IsLoopback() || v.IP.IsLinkLocalMulticast() || v.IP.IsLinkLocalUnicast() {
break
}
ips = append(ips, "\t"+v.IP.String())
}
}
}
fmt.Println("Local IP:")
fmt.Println(strings.Join(ips, "\n"))
fmt.Println("starting http server on", config.Opts.ListenAndServe)
log.Fatal(http.ListenAndServe(config.Opts.ListenAndServe, http.FileServer(http.Dir(dir))))
listenAndServe()
return
}

Expand Down

0 comments on commit b6a5b03

Please sign in to comment.