diff --git a/.DS_Store b/.DS_Store index 235e2b6..67bc4ee 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/cmd/evening_detective/main.go b/cmd/evening_detective/main.go index 740d75b..0299b46 100644 --- a/cmd/evening_detective/main.go +++ b/cmd/evening_detective/main.go @@ -5,6 +5,7 @@ import ( "embed" "evening_detective/internal/app" "evening_detective/internal/config" + "evening_detective/internal/modules/link" "evening_detective/internal/services" "evening_detective/internal/services/story_service" proto "evening_detective/proto" @@ -46,12 +47,16 @@ func main() { if err != nil { log.Fatalln(err) } + + linkService := link.NewLinkService() + proto.RegisterEveningDetectiveServer( s, app.NewServer( services.NewServices( repository, storyService, + linkService, ), ), ) diff --git a/internal/config/config.go b/internal/config/config.go index 01c2997..fe8c788 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,19 +1,36 @@ package config -import "os" +import ( + "os" + "path/filepath" +) func GetStoryFilepath() string { - storyFilename := os.Getenv("STORY_FILENAME") - if storyFilename != "" { - return storyFilename - } - return "./data/story/story.json" + return getFilepath("STORY_FILENAME", "data/story/story.json") } func GetDBFilepath() string { - storyFilename := os.Getenv("DB_FILENAME") - if storyFilename != "" { - return storyFilename - } - return "data/db/store.db" + return getFilepath("DB_FILENAME", "data/db/store.db") +} + +func getFilepath(env string, defaultFilepath string) string { + filepath := selectFilepath(env, defaultFilepath) + ensureDirExists(filepath) + return filepath +} + +func selectFilepath(env string, defaultFilepath string) string { + filepath := os.Getenv(env) + if filepath != "" { + return filepath + } + return defaultFilepath +} + +func ensureDirExists(filePath string) error { + dir := filepath.Dir(filePath) + if dir == "" || dir == "." || dir == "/" { + return nil + } + return os.MkdirAll(dir, 0755) } diff --git a/internal/models/team.go b/internal/models/team.go index 61eb48c..8449dab 100644 --- a/internal/models/team.go +++ b/internal/models/team.go @@ -1,53 +1,8 @@ package models -import ( - "fmt" - "net" - "net/url" -) - type Team struct { ID int64 Name string Password string -} - -func (t *Team) GetTeamUrl() (string, error) { - ip := selectIP() - u := fmt.Sprintf("http://%s:8100?name=%s&password=%s", ip, url.PathEscape(t.Name), t.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 + Link string } diff --git a/internal/modules/link/interface.go b/internal/modules/link/interface.go new file mode 100644 index 0000000..7cb3fd1 --- /dev/null +++ b/internal/modules/link/interface.go @@ -0,0 +1,5 @@ +package link + +type ILinkService interface { + GetTeamClientLink(name string, password string) (string, error) +} diff --git a/internal/modules/link/service.go b/internal/modules/link/service.go new file mode 100644 index 0000000..6f26fb7 --- /dev/null +++ b/internal/modules/link/service.go @@ -0,0 +1,53 @@ +package link + +import ( + "fmt" + "net" + "net/url" +) + +type service struct{} + +func NewLinkService() ILinkService { + return &service{} +} + +func (s *service) GetTeamClientLink(name string, password string) (string, error) { + ip := selectIP() + 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 +} diff --git a/internal/services/pdf_service/service.go b/internal/services/pdf_service/service.go index f5df0cd..7867899 100644 --- a/internal/services/pdf_service/service.go +++ b/internal/services/pdf_service/service.go @@ -40,10 +40,6 @@ func CreateTeamsPdf(teams []*models.Team) ([]byte, error) { y := (padding + 15) + yDelta*float64(i%countOnPage/3) x := padding + xDelta*float64(i%3) - url, err := team.GetTeamUrl() - if err != nil { - return nil, err - } if err := printTextCenter(pdf, "Подключите Wi-Fi", xDelta-6, x+3, y); err != nil { return nil, err } @@ -53,7 +49,7 @@ func CreateTeamsPdf(teams []*models.Team) ([]byte, error) { if err := printTextCenter(pdf, "Пароль: 12345678", xDelta-6, x+3, 30+y); err != nil { return nil, err } - if err := printQR(pdf, url, x+21, 65+y); err != nil { + if err := printQR(pdf, team.Link, x+21, 65+y); err != nil { return nil, err } if err := printTextCenter(pdf, "Войдите в приложение по qr", xDelta-6, x+3, 55+y); err != nil { diff --git a/internal/services/services.go b/internal/services/services.go index 3753a97..e097b56 100644 --- a/internal/services/services.go +++ b/internal/services/services.go @@ -5,6 +5,7 @@ import ( "encoding/base64" "encoding/json" "evening_detective/internal/models" + "evening_detective/internal/modules/link" "evening_detective/internal/modules/password" "evening_detective/internal/services/pdf_service" "evening_detective/internal/services/story_service" @@ -20,15 +21,18 @@ import ( type Services struct { repository *Repository storyService *story_service.StoryService + linkService link.ILinkService } func NewServices( repository *Repository, storyService *story_service.StoryService, + linkService link.ILinkService, ) *Services { return &Services{ repository: repository, storyService: storyService, + linkService: linkService, } } @@ -153,7 +157,7 @@ func (s *Services) GetTeams(ctx context.Context, _ *proto.GetTeamsReq) (*proto.G if err != nil { return nil, err } - newTeam.Url, err = team.GetTeamUrl() + newTeam.Url, err = s.linkService.GetTeamClientLink(team.Name, team.Password) if err != nil { return nil, err } @@ -193,6 +197,12 @@ func (s *Services) DownloadTeamsQrCodesFile(ctx context.Context, req *proto.Down if err != nil { return nil, err } + for _, team := range teams { + team.Link, err = s.linkService.GetTeamClientLink(team.Name, team.Password) + if err != nil { + return nil, err + } + } b, err := pdf_service.CreateTeamsPdf(teams) if err != nil { return nil, err