2021-09-10 08:52:44 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2022-03-27 23:14:13 +02:00
|
|
|
"net/http"
|
|
|
|
|
"runtime/debug"
|
|
|
|
|
|
2022-01-05 01:40:37 +01:00
|
|
|
"github.com/LightningTipBot/LightningTipBot/internal"
|
2022-01-05 00:09:11 +01:00
|
|
|
"github.com/LightningTipBot/LightningTipBot/internal/api"
|
2022-01-05 01:40:37 +01:00
|
|
|
"github.com/LightningTipBot/LightningTipBot/internal/api/admin"
|
2022-04-29 22:05:28 +02:00
|
|
|
"github.com/LightningTipBot/LightningTipBot/internal/api/userpage"
|
2022-01-05 00:09:11 +01:00
|
|
|
"github.com/LightningTipBot/LightningTipBot/internal/lndhub"
|
|
|
|
|
"github.com/LightningTipBot/LightningTipBot/internal/lnurl"
|
2021-12-26 16:30:27 +01:00
|
|
|
"github.com/LightningTipBot/LightningTipBot/internal/runtime/mutex"
|
|
|
|
|
|
|
|
|
|
_ "net/http/pprof"
|
|
|
|
|
|
2022-03-27 23:14:13 +02:00
|
|
|
tb "gopkg.in/lightningtipbot/telebot.v3"
|
|
|
|
|
|
2021-10-07 00:21:21 +03:00
|
|
|
"github.com/LightningTipBot/LightningTipBot/internal/lnbits/webhook"
|
2021-10-08 09:35:46 +03:00
|
|
|
"github.com/LightningTipBot/LightningTipBot/internal/price"
|
2021-10-07 00:21:21 +03:00
|
|
|
"github.com/LightningTipBot/LightningTipBot/internal/telegram"
|
2021-09-10 08:52:44 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// setLogger will initialize the log format
|
|
|
|
|
func setLogger() {
|
2022-01-04 12:32:10 +00:00
|
|
|
log.SetLevel(log.DebugLevel)
|
2021-09-10 08:52:44 +02:00
|
|
|
customFormatter := new(log.TextFormatter)
|
|
|
|
|
customFormatter.TimestampFormat = "2006-01-02 15:04:05"
|
|
|
|
|
customFormatter.FullTimestamp = true
|
|
|
|
|
log.SetFormatter(customFormatter)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
// set logger
|
|
|
|
|
setLogger()
|
2022-01-05 00:09:11 +01:00
|
|
|
|
|
|
|
|
defer withRecovery()
|
|
|
|
|
price.NewPriceWatcher().Start()
|
|
|
|
|
bot := telegram.NewBot()
|
|
|
|
|
startApiServer(&bot)
|
|
|
|
|
bot.Start()
|
|
|
|
|
}
|
|
|
|
|
func startApiServer(bot *telegram.TipBot) {
|
2022-03-27 23:14:13 +02:00
|
|
|
// log errors from interceptors
|
|
|
|
|
bot.Telegram.OnError = func(err error, ctx tb.Context) {
|
|
|
|
|
// we already log in the interceptors
|
|
|
|
|
}
|
2022-01-05 00:09:11 +01:00
|
|
|
// start internal webhook server
|
|
|
|
|
webhook.NewServer(bot)
|
|
|
|
|
// start external api server
|
2022-01-05 01:40:37 +01:00
|
|
|
s := api.NewServer(internal.Configuration.Bot.LNURLServerUrl.Host)
|
2022-01-05 00:09:11 +01:00
|
|
|
|
2022-03-27 22:19:38 +02:00
|
|
|
// append lnurl ctx functions
|
2022-01-05 00:09:11 +01:00
|
|
|
lnUrl := lnurl.New(bot)
|
|
|
|
|
s.AppendRoute("/.well-known/lnurlp/{username}", lnUrl.Handle, http.MethodGet)
|
2022-04-29 22:05:28 +02:00
|
|
|
|
|
|
|
|
// userpage server
|
|
|
|
|
userpage := userpage.New(bot)
|
|
|
|
|
s.AppendRoute("/@{username}", userpage.UserPageHandler, http.MethodGet)
|
2022-01-05 00:09:11 +01:00
|
|
|
|
2022-03-27 22:19:38 +02:00
|
|
|
// append lndhub ctx functions
|
2022-01-05 00:09:11 +01:00
|
|
|
hub := lndhub.New(bot)
|
|
|
|
|
s.AppendRoute(`/lndhub/ext/{.*}`, hub.Handle)
|
|
|
|
|
s.AppendRoute(`/lndhub/ext`, hub.Handle)
|
|
|
|
|
|
|
|
|
|
// start internal admin server
|
2022-01-05 01:40:37 +01:00
|
|
|
adminService := admin.New(bot)
|
|
|
|
|
internalAdminServer := api.NewServer("0.0.0.0:6060")
|
|
|
|
|
internalAdminServer.AppendRoute("/mutex", mutex.ServeHTTP)
|
|
|
|
|
internalAdminServer.AppendRoute("/mutex/unlock/{id}", mutex.UnlockHTTP)
|
|
|
|
|
internalAdminServer.AppendRoute("/admin/ban/{id}", adminService.BanUser)
|
|
|
|
|
internalAdminServer.AppendRoute("/admin/unban/{id}", adminService.UnbanUser)
|
|
|
|
|
internalAdminServer.PathPrefix("/debug/pprof/", http.DefaultServeMux)
|
|
|
|
|
|
2021-09-10 08:52:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func withRecovery() {
|
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
|
log.Errorln("Recovered panic: ", r)
|
|
|
|
|
debug.PrintStack()
|
|
|
|
|
}
|
|
|
|
|
}
|