generated from VLADIMIR/template
add url
This commit is contained in:
@@ -9,6 +9,8 @@ import (
|
||||
"evening_detective/internal/services/story_service"
|
||||
"evening_detective/proto"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/metadata"
|
||||
@@ -119,6 +121,10 @@ func (s *Services) GetTeams(ctx context.Context, _ *proto.GetTeamsReq) (*proto.G
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
newTeam.Url, err = getTeamUrl(team)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
newTeam.SpendTime = int64(20 * len(actions))
|
||||
applications, err := s.repository.GetApplications(ctx, team.ID, "NEW")
|
||||
if err != nil {
|
||||
@@ -182,3 +188,39 @@ func log(team *models.Team, action string, v any) {
|
||||
fmt.Printf("Team %s: %s %s\n", team.Name, action, string(vJson))
|
||||
}
|
||||
}
|
||||
|
||||
func getTeamUrl(team *models.Team) (string, error) {
|
||||
ips, err := getLocalIPs()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
ip := ips[0]
|
||||
u := fmt.Sprintf("http://%s:8100?name=%s&password=%s", ip, url.PathEscape(team.Name), team.Password)
|
||||
return u, nil
|
||||
}
|
||||
|
||||
func getLocalIPs() ([]string, error) {
|
||||
var ips []string
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, addr := range addrs {
|
||||
ipNet, ok := addr.(*net.IPNet)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
ip := ipNet.IP
|
||||
if ip.IsLoopback() || ip.IsLinkLocalMulticast() || ip.IsLinkLocalUnicast() {
|
||||
continue
|
||||
}
|
||||
|
||||
if ipv4 := ip.To4(); ipv4 != nil {
|
||||
ips = append(ips, ipv4.String())
|
||||
}
|
||||
}
|
||||
|
||||
return ips, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user