Moved server packages to a separate folder

This commit is contained in:
2019-11-18 18:39:59 +09:00
parent db366360c7
commit f3c14a4ed6
37 changed files with 28 additions and 23 deletions

View File

@ -0,0 +1,43 @@
package middleware
import (
"net"
"time"
"github.com/akyoto/cache"
)
var ipToHosts = cache.New(60 * time.Minute)
// GetHostsForIP returns all host names for the given IP (if cached).
func GetHostsForIP(ip string) ([]string, bool) {
hosts, found := ipToHosts.Get(ip)
if !found {
hosts = findHostsForIP(ip)
}
if hosts == nil {
return nil, found
}
return hosts.([]string), found
}
// Finds all host names for the given IP
func findHostsForIP(ip string) []string {
hosts, err := net.LookupAddr(ip)
if err != nil {
return nil
}
if len(hosts) == 0 {
return nil
}
// Cache host names
ipToHosts.Set(ip, hosts, 60*time.Minute)
return hosts
}