generated from VLADIMIR/template
clear
This commit is contained in:
parent
1964f4241e
commit
5604732fcb
@ -5,6 +5,7 @@ import (
|
|||||||
"embed"
|
"embed"
|
||||||
"evening_detective/internal/app"
|
"evening_detective/internal/app"
|
||||||
"evening_detective/internal/config"
|
"evening_detective/internal/config"
|
||||||
|
"evening_detective/internal/modules/link"
|
||||||
"evening_detective/internal/services"
|
"evening_detective/internal/services"
|
||||||
"evening_detective/internal/services/story_service"
|
"evening_detective/internal/services/story_service"
|
||||||
proto "evening_detective/proto"
|
proto "evening_detective/proto"
|
||||||
@ -46,12 +47,16 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
linkService := link.NewLinkService()
|
||||||
|
|
||||||
proto.RegisterEveningDetectiveServer(
|
proto.RegisterEveningDetectiveServer(
|
||||||
s,
|
s,
|
||||||
app.NewServer(
|
app.NewServer(
|
||||||
services.NewServices(
|
services.NewServices(
|
||||||
repository,
|
repository,
|
||||||
storyService,
|
storyService,
|
||||||
|
linkService,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|||||||
@ -1,19 +1,36 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import "os"
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
func GetStoryFilepath() string {
|
func GetStoryFilepath() string {
|
||||||
storyFilename := os.Getenv("STORY_FILENAME")
|
return getFilepath("STORY_FILENAME", "data/story/story.json")
|
||||||
if storyFilename != "" {
|
|
||||||
return storyFilename
|
|
||||||
}
|
|
||||||
return "./data/story/story.json"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDBFilepath() string {
|
func GetDBFilepath() string {
|
||||||
storyFilename := os.Getenv("DB_FILENAME")
|
return getFilepath("DB_FILENAME", "data/db/store.db")
|
||||||
if storyFilename != "" {
|
}
|
||||||
return storyFilename
|
|
||||||
}
|
func getFilepath(env string, defaultFilepath string) string {
|
||||||
return "data/db/store.db"
|
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
|
package models
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"net"
|
|
||||||
"net/url"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Team struct {
|
type Team struct {
|
||||||
ID int64
|
ID int64
|
||||||
Name string
|
Name string
|
||||||
Password string
|
Password string
|
||||||
}
|
Link 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
|
|
||||||
}
|
}
|
||||||
|
|||||||
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)
|
y := (padding + 15) + yDelta*float64(i%countOnPage/3)
|
||||||
x := padding + xDelta*float64(i%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 {
|
if err := printTextCenter(pdf, "Подключите Wi-Fi", xDelta-6, x+3, y); err != nil {
|
||||||
return nil, err
|
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 {
|
if err := printTextCenter(pdf, "Пароль: 12345678", xDelta-6, x+3, 30+y); err != nil {
|
||||||
return nil, err
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
if err := printTextCenter(pdf, "Войдите в приложение по qr", xDelta-6, x+3, 55+y); err != nil {
|
if err := printTextCenter(pdf, "Войдите в приложение по qr", xDelta-6, x+3, 55+y); err != nil {
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"evening_detective/internal/models"
|
"evening_detective/internal/models"
|
||||||
|
"evening_detective/internal/modules/link"
|
||||||
"evening_detective/internal/modules/password"
|
"evening_detective/internal/modules/password"
|
||||||
"evening_detective/internal/services/pdf_service"
|
"evening_detective/internal/services/pdf_service"
|
||||||
"evening_detective/internal/services/story_service"
|
"evening_detective/internal/services/story_service"
|
||||||
@ -20,15 +21,18 @@ import (
|
|||||||
type Services struct {
|
type Services struct {
|
||||||
repository *Repository
|
repository *Repository
|
||||||
storyService *story_service.StoryService
|
storyService *story_service.StoryService
|
||||||
|
linkService link.ILinkService
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServices(
|
func NewServices(
|
||||||
repository *Repository,
|
repository *Repository,
|
||||||
storyService *story_service.StoryService,
|
storyService *story_service.StoryService,
|
||||||
|
linkService link.ILinkService,
|
||||||
) *Services {
|
) *Services {
|
||||||
return &Services{
|
return &Services{
|
||||||
repository: repository,
|
repository: repository,
|
||||||
storyService: storyService,
|
storyService: storyService,
|
||||||
|
linkService: linkService,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -153,7 +157,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 = team.GetTeamUrl()
|
newTeam.Url, err = s.linkService.GetTeamClientLink(team.Name, team.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -193,6 +197,12 @@ func (s *Services) DownloadTeamsQrCodesFile(ctx context.Context, req *proto.Down
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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)
|
b, err := pdf_service.CreateTeamsPdf(teams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user