This commit is contained in:
Владимир Фёдоров 2026-03-02 01:27:50 +07:00
parent 1964f4241e
commit 5604732fcb
8 changed files with 104 additions and 63 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@ -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,
),
),
)

View File

@ -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 getFilepath("DB_FILENAME", "data/db/store.db")
}
return "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)
}

View File

@ -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
}

View File

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

View File

@ -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
}

View File

@ -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 {

View File

@ -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