generated from VLADIMIR/template
clear
This commit is contained in:
parent
1964f4241e
commit
5604732fcb
@ -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,
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
5
internal/modules/link/interface.go
Normal file
5
internal/modules/link/interface.go
Normal file
@ -0,0 +1,5 @@
|
||||
package link
|
||||
|
||||
type ILinkService interface {
|
||||
GetTeamClientLink(name string, password string) (string, error)
|
||||
}
|
||||
53
internal/modules/link/service.go
Normal file
53
internal/modules/link/service.go
Normal 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
|
||||
}
|
||||
@ -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 {
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user