-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.go
53 lines (40 loc) · 1.06 KB
/
function.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package gcp
import (
"context"
"net/http"
"os"
"github.com/rs/zerolog"
"github.com/nskondratev/tg-bot-template/internal/app/bot"
"github.com/nskondratev/tg-bot-template/internal/boot"
"github.com/nskondratev/tg-bot-template/internal/env"
"github.com/nskondratev/tg-bot-template/internal/logger"
"github.com/nskondratev/tg-bot-template/internal/metrics"
)
var (
b *bot.Bot
log zerolog.Logger
)
func init() {
var err error
ctx := context.Background()
log = logger.Must(env.String("LOG_LEVEL", "debug"), os.Stdout)
stats, err := metrics.New(ctx)
if err != nil {
panic("failed to create metrics client: " + err.Error())
}
b, err = boot.InitBot(ctx, log, stats)
if err != nil {
panic("failed to init bot: " + err.Error())
}
}
func BotUpdate(w http.ResponseWriter, r *http.Request) {
log.Info().Msg("New update")
up, err := b.UpdateFromRequest(r)
if err != nil {
log.Err(err).Msg("failed to get update from request")
http.Error(w, "bad input", http.StatusBadRequest)
return
}
b.HandleUpdate(r.Context(), up)
w.WriteHeader(http.StatusOK)
}