|
1
|
|
|
package app |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"context" |
|
5
|
|
|
"errors" |
|
6
|
|
|
"fmt" |
|
7
|
|
|
"net/http" |
|
8
|
|
|
"os/signal" |
|
9
|
|
|
"syscall" |
|
10
|
|
|
"time" |
|
11
|
|
|
|
|
12
|
|
|
"github.com/cronnoss/tk-api/internal/logger" |
|
13
|
|
|
"github.com/cronnoss/tk-api/internal/server" |
|
14
|
|
|
"github.com/cronnoss/tk-api/internal/storage" |
|
15
|
|
|
"github.com/cronnoss/tk-api/internal/storage/models" |
|
16
|
|
|
"golang.org/x/sync/errgroup" |
|
17
|
|
|
) |
|
18
|
|
|
|
|
19
|
|
|
type TicketConf struct { |
|
20
|
|
|
Logger logger.Conf `toml:"logger"` |
|
21
|
|
|
Storage storage.Conf `toml:"storage"` |
|
22
|
|
|
HTTP struct { |
|
23
|
|
|
Host string `toml:"host"` |
|
24
|
|
|
Port string `toml:"port"` |
|
25
|
|
|
} `toml:"http-server"` |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
type Ticket struct { |
|
29
|
|
|
conf TicketConf |
|
30
|
|
|
log server.Logger |
|
31
|
|
|
storage Storage |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
type Storage interface { |
|
35
|
|
|
Connect(ctx context.Context) error |
|
36
|
|
|
Close(ctx context.Context) error |
|
37
|
|
|
GetShows(ctx context.Context) ([]models.Show, error) |
|
38
|
|
|
CreateShows(ctx context.Context, shows []models.Show) ([]models.Show, error) |
|
39
|
|
|
CreateShow(ctx context.Context, shows models.Show) (models.Show, error) |
|
40
|
|
|
GetEvents(ctx context.Context) ([]models.Event, error) |
|
41
|
|
|
CreateEvents(ctx context.Context, events []models.Event) ([]models.Event, error) |
|
42
|
|
|
CreateEvent(ctx context.Context, event models.Event) (models.Event, error) |
|
43
|
|
|
GetPlaces(ctx context.Context) ([]models.Place, error) |
|
44
|
|
|
CreatePlaces(ctx context.Context, places []models.Place) ([]models.Place, error) |
|
45
|
|
|
CreatePlace(ctx context.Context, place models.Place) (models.Place, error) |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
type Server interface { |
|
49
|
|
|
Start(context.Context) error |
|
50
|
|
|
Stop(context.Context) error |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
func (t *Ticket) GetShows(ctx context.Context) ([]models.Show, error) { |
|
54
|
|
|
ctx, cancel := context.WithTimeout(ctx, 2*time.Second) |
|
55
|
|
|
defer cancel() |
|
56
|
|
|
return t.storage.GetShows(ctx) |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
func (t *Ticket) CreateShows(ctx context.Context, shows []models.Show) ([]models.Show, error) { |
|
60
|
|
|
ctx, cancel := context.WithTimeout(ctx, 2*time.Second) |
|
61
|
|
|
defer cancel() |
|
62
|
|
|
return t.storage.CreateShows(ctx, shows) |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
func (t *Ticket) CreateShow(ctx context.Context, show models.Show) (models.Show, error) { |
|
66
|
|
|
ctx, cancel := context.WithTimeout(ctx, 2*time.Second) |
|
67
|
|
|
defer cancel() |
|
68
|
|
|
return t.storage.CreateShow(ctx, show) |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
func (t *Ticket) GetEvents(ctx context.Context) ([]models.Event, error) { |
|
72
|
|
|
ctx, cancel := context.WithTimeout(ctx, 2*time.Second) |
|
73
|
|
|
defer cancel() |
|
74
|
|
|
return t.storage.GetEvents(ctx) |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
func (t *Ticket) CreateEvents(ctx context.Context, events []models.Event) ([]models.Event, error) { |
|
78
|
|
|
ctx, cancel := context.WithTimeout(ctx, 2*time.Second) |
|
79
|
|
|
defer cancel() |
|
80
|
|
|
return t.storage.CreateEvents(ctx, events) |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
func (t *Ticket) CreateEvent(ctx context.Context, event models.Event) (models.Event, error) { |
|
84
|
|
|
ctx, cancel := context.WithTimeout(ctx, 2*time.Second) |
|
85
|
|
|
defer cancel() |
|
86
|
|
|
return t.storage.CreateEvent(ctx, event) |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
func (t *Ticket) GetPlaces(ctx context.Context) ([]models.Place, error) { |
|
90
|
|
|
ctx, cancel := context.WithTimeout(ctx, 2*time.Second) |
|
91
|
|
|
defer cancel() |
|
92
|
|
|
return t.storage.GetPlaces(ctx) |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
func (t *Ticket) CreatePlaces(ctx context.Context, places []models.Place) ([]models.Place, error) { |
|
96
|
|
|
ctx, cancel := context.WithTimeout(ctx, 2*time.Second) |
|
97
|
|
|
defer cancel() |
|
98
|
|
|
return t.storage.CreatePlaces(ctx, places) |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
func (t *Ticket) CreatePlace(ctx context.Context, place models.Place) (models.Place, error) { |
|
102
|
|
|
ctx, cancel := context.WithTimeout(ctx, 2*time.Second) |
|
103
|
|
|
defer cancel() |
|
104
|
|
|
return t.storage.CreatePlace(ctx, place) |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
func NewTicket(log server.Logger, conf TicketConf, storage Storage) (*Ticket, error) { |
|
108
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
|
109
|
|
|
defer cancel() |
|
110
|
|
|
|
|
111
|
|
|
if err := storage.Connect(ctx); err != nil { |
|
112
|
|
|
server.Exitfail(fmt.Sprintf("Can't connect to storage:%v", err)) |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return &Ticket{log: log, conf: conf, storage: storage}, nil |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
func (t Ticket) Run(httpsrv Server) { |
|
119
|
|
|
ctx, cancel := signal.NotifyContext(context.Background(), |
|
120
|
|
|
syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP) |
|
121
|
|
|
defer cancel() |
|
122
|
|
|
|
|
123
|
|
|
g, ctxEG := errgroup.WithContext(ctx) |
|
124
|
|
|
|
|
125
|
|
|
func1 := func() error { |
|
126
|
|
|
return httpsrv.Start(ctxEG) |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
go func() { |
|
130
|
|
|
<-ctx.Done() |
|
131
|
|
|
|
|
132
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) |
|
133
|
|
|
defer cancel() |
|
134
|
|
|
|
|
135
|
|
|
if err := httpsrv.Stop(ctx); err != nil { |
|
136
|
|
|
if !errors.Is(err, http.ErrServerClosed) && |
|
137
|
|
|
!errors.Is(err, context.Canceled) { |
|
138
|
|
|
t.log.Errorf("failed to stop HTTP-server:%v\n", err) |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
if err := t.storage.Close(ctx); err != nil { |
|
143
|
|
|
t.log.Errorf("failed to close db:%v\n", err) |
|
144
|
|
|
} |
|
145
|
|
|
}() |
|
146
|
|
|
|
|
147
|
|
|
g.Go(func1) |
|
148
|
|
|
|
|
149
|
|
|
if err := g.Wait(); err != nil { |
|
150
|
|
|
if !errors.Is(err, http.ErrServerClosed) && |
|
151
|
|
|
!errors.Is(err, context.Canceled) { |
|
152
|
|
|
t.log.Errorf("%v\n", err) |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|