-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
157 lines (129 loc) · 3.27 KB
/
main.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"context"
"fmt"
stdLog "log"
"os"
"os/signal"
"github.com/labstack/echo-contrib/prometheus"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/gommon/log"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/skar404/spotify_share/bot"
"github.com/skar404/spotify_share/commands"
"github.com/skar404/spotify_share/global"
"github.com/skar404/spotify_share/handler"
"github.com/skar404/spotify_share/telegram"
)
func main() {
mode := global.AppMode
log.Infof("starting app, config: mode=%s, debug=%t", mode, global.Debug)
initStopSignal()
// App env
webhookToken := global.WebhookToken
telegramToken := global.TelegramToken
clientId := global.ClientId
clientSecret := global.ClientSecret
url := constructDBUrl()
conn, err := mongo.Connect(context.Background(), options.Client().ApplyURI(url))
if err != nil {
panic(err)
}
defer conn.Disconnect(context.Background())
db := conn.Database(global.DBName)
// Initialize handler
h := &handler.Handler{
DBConn: conn,
DB: db,
}
lockChanel := make(chan bool)
if mode == "CLI" {
runCLI(clientId, clientSecret)
} else if mode == "WEB" {
// set webhook
} else if mode == "GET_UPDATES" {
// only dev mode:
// ... run `for true` and lock web server
go func() {
runGetUpdate(telegramToken, h)
lockChanel <- true
}()
}
if mode != "CLI" {
runHttpServer(webhookToken, h)
<-lockChanel
}
}
func initStopSignal() {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt)
go func() {
for sig := range c {
log.Infof("stop apps sig=%v", sig)
os.Exit(1)
}
}()
}
func runCLI(clientId, clientSecret string) {
token, refreshToken, err := commands.CreateToken(clientId, clientSecret)
if err != nil {
fmt.Printf("error create token %e\n", err)
return
}
fmt.Printf("token=%s, refreshTo¬ken=%s\n", token, refreshToken)
}
func runGetUpdate(telegramToken string, h *handler.Handler) {
tg := &telegram.Client
updateId := 0
for true {
raw, err := tg.GetUpdates(updateId)
if err != nil {
stdLog.Println(err)
continue
}
for _, item := range raw.Result {
if updateId > item.UpdateId {
continue
}
bot.Router(&item, h)
updateId = item.UpdateId + 1
}
}
}
func constructDBUrl() string {
url := fmt.Sprintf("mongodb://%s:%s@%s/%s?",
global.DBUser,
global.DBPass,
global.DBHost,
global.DBName)
if global.DBAuthSource != "" {
url = fmt.Sprintf("%s&authSource=%s", url, global.DBAuthSource)
}
if global.DBRs != "" {
url = fmt.Sprintf("%s&replicaSet=%s", url, global.DBRs)
}
if global.DBCACERT != "" {
url = fmt.Sprintf("%s&tls=true&tlsCaFile=%s", url, global.DBCACERT)
}
if global.DBAuthenticationDatabase != "" {
url = fmt.Sprintf("%s&authenticationDatabase=%s", url, global.DBAuthenticationDatabase)
}
return url
}
func runHttpServer(webhookToken string, handler *handler.Handler) {
e := echo.New()
e.Debug = global.Debug
// Enable metrics middleware
p := prometheus.NewPrometheus("echo", nil)
p.Use(e)
// Middleware
e.Logger.SetLevel(log.INFO)
e.Use(middleware.Logger())
e.Use(middleware.Recover())
// Routes
e.GET("/spotify", handler.OAuthSpotify)
e.GET("/ping", handler.Ping)
e.Logger.Fatal(e.Start(fmt.Sprintf("%s:1323", global.AppHost)))
}