This commit is contained in:
Владимир Фёдоров 2026-03-02 01:43:46 +07:00
parent 5604732fcb
commit 3b9c77b422
5 changed files with 55 additions and 50 deletions

View File

@ -50,6 +50,7 @@ func main() {
linkService := link.NewLinkService() linkService := link.NewLinkService()
clientHost := config.GetHost()
proto.RegisterEveningDetectiveServer( proto.RegisterEveningDetectiveServer(
s, s,
app.NewServer( app.NewServer(
@ -57,6 +58,7 @@ func main() {
repository, repository,
storyService, storyService,
linkService, linkService,
clientHost,
), ),
), ),
) )
@ -112,9 +114,9 @@ func main() {
muxUser.Handle("/", fileServerUser) muxUser.Handle("/", fileServerUser)
// Serve user web server // Serve user web server
log.Println("Serving user web on http://0.0.0.0:8100") log.Println("Serving user web on http://0.0.0.0" + config.ClientPort)
go func() { go func() {
log.Fatalln(http.ListenAndServe(":8100", muxUser)) log.Fatalln(http.ListenAndServe(config.ClientPort, muxUser))
}() }()
muxAdmin := http.NewServeMux() muxAdmin := http.NewServeMux()

View File

@ -1,10 +1,15 @@
package config package config
import ( import (
"net"
"os" "os"
"path/filepath" "path/filepath"
) )
const (
ClientPort = ":8100"
)
func GetStoryFilepath() string { func GetStoryFilepath() string {
return getFilepath("STORY_FILENAME", "data/story/story.json") return getFilepath("STORY_FILENAME", "data/story/story.json")
} }
@ -13,6 +18,18 @@ func GetDBFilepath() string {
return getFilepath("DB_FILENAME", "data/db/store.db") return getFilepath("DB_FILENAME", "data/db/store.db")
} }
func GetHost() string {
host := os.Getenv("HOST")
if host != "" {
return host
}
ips, err := getLocalIPs()
if err != nil || len(ips) == 0 {
return "127.0.0.1" + ClientPort
}
return ips[0] + ClientPort
}
func getFilepath(env string, defaultFilepath string) string { func getFilepath(env string, defaultFilepath string) string {
filepath := selectFilepath(env, defaultFilepath) filepath := selectFilepath(env, defaultFilepath)
ensureDirExists(filepath) ensureDirExists(filepath)
@ -34,3 +51,29 @@ func ensureDirExists(filePath string) error {
} }
return os.MkdirAll(dir, 0755) return os.MkdirAll(dir, 0755)
} }
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
}

View File

@ -1,5 +1,5 @@
package link package link
type ILinkService interface { type ILinkService interface {
GetTeamClientLink(name string, password string) (string, error) GetTeamClientLink(host string, name string, password string) string
} }

View File

@ -2,7 +2,6 @@ package link
import ( import (
"fmt" "fmt"
"net"
"net/url" "net/url"
) )
@ -12,42 +11,6 @@ func NewLinkService() ILinkService {
return &service{} return &service{}
} }
func (s *service) GetTeamClientLink(name string, password string) (string, error) { func (s *service) GetTeamClientLink(host string, name string, password string) string {
ip := selectIP() return fmt.Sprintf("http://%s?name=%s&password=%s", host, url.PathEscape(name), password)
u := fmt.Sprintf("http://%s:8100?name=%s&password=%s", ip, url.PathEscape(name), password)
return u, nil
}
func selectIP() string {
ips, err := getLocalIPs()
if err != nil || len(ips) == 0 {
return "127.0.0.1"
}
return ips[0]
}
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
} }

View File

@ -22,17 +22,20 @@ type Services struct {
repository *Repository repository *Repository
storyService *story_service.StoryService storyService *story_service.StoryService
linkService link.ILinkService linkService link.ILinkService
clientHost string
} }
func NewServices( func NewServices(
repository *Repository, repository *Repository,
storyService *story_service.StoryService, storyService *story_service.StoryService,
linkService link.ILinkService, linkService link.ILinkService,
clientHost string,
) *Services { ) *Services {
return &Services{ return &Services{
repository: repository, repository: repository,
storyService: storyService, storyService: storyService,
linkService: linkService, linkService: linkService,
clientHost: clientHost,
} }
} }
@ -157,10 +160,7 @@ func (s *Services) GetTeams(ctx context.Context, _ *proto.GetTeamsReq) (*proto.G
if err != nil { if err != nil {
return nil, err return nil, err
} }
newTeam.Url, err = s.linkService.GetTeamClientLink(team.Name, team.Password) newTeam.Url = s.linkService.GetTeamClientLink(s.clientHost, team.Name, team.Password)
if err != nil {
return nil, err
}
newTeam.SpendTime = int64(len(actions)) newTeam.SpendTime = int64(len(actions))
currentApplications, err := s.repository.GetApplicationsByState(ctx, team.ID, "NEW") currentApplications, err := s.repository.GetApplicationsByState(ctx, team.ID, "NEW")
if err != nil { if err != nil {
@ -198,10 +198,7 @@ func (s *Services) DownloadTeamsQrCodesFile(ctx context.Context, req *proto.Down
return nil, err return nil, err
} }
for _, team := range teams { for _, team := range teams {
team.Link, err = s.linkService.GetTeamClientLink(team.Name, team.Password) team.Link = s.linkService.GetTeamClientLink(s.clientHost, team.Name, team.Password)
if err != nil {
return nil, err
}
} }
b, err := pdf_service.CreateTeamsPdf(teams) b, err := pdf_service.CreateTeamsPdf(teams)
if err != nil { if err != nil {