lightningtipbot/main.go

96 lines
4 KiB
Go
Raw Permalink Normal View History

2021-09-10 08:52:44 +02:00
package main
import (
2022-03-27 23:14:13 +02:00
"net/http"
"runtime/debug"
"github.com/LightningTipBot/LightningTipBot/internal"
"github.com/LightningTipBot/LightningTipBot/internal/api"
"github.com/LightningTipBot/LightningTipBot/internal/api/admin"
"github.com/LightningTipBot/LightningTipBot/internal/api/userpage"
"github.com/LightningTipBot/LightningTipBot/internal/lndhub"
"github.com/LightningTipBot/LightningTipBot/internal/lnurl"
"github.com/LightningTipBot/LightningTipBot/internal/runtime/mutex"
_ "net/http/pprof"
2022-03-27 23:14:13 +02:00
tb "gopkg.in/lightningtipbot/telebot.v3"
"github.com/LightningTipBot/LightningTipBot/internal/lnbits/webhook"
"github.com/LightningTipBot/LightningTipBot/internal/price"
"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() {
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()
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
}
// start internal webhook server
webhook.NewServer(bot)
// start external api server
s := api.NewServer(internal.Configuration.Bot.LNURLServerUrl.Host)
// append lnurl ctx functions
lnUrl := lnurl.New(bot)
s.AppendRoute("/.well-known/lnurlp/{username}", lnUrl.Handle, http.MethodGet)
// userpage server
userpage := userpage.New(bot)
s.AppendRoute("/@{username}", userpage.UserPageHandler, http.MethodGet)
2022-08-28 14:59:30 +02:00
s.AppendRoute("/app/@{username}", userpage.UserWebAppHandler, http.MethodGet)
// append lndhub ctx functions
hub := lndhub.New(bot)
2022-08-22 09:32:30 +02:00
s.AppendAuthorizedRoute(`/lndhub/ext/auth`, api.AuthTypeNone, api.AccessKeyTypeNone, bot.DB.Users, hub.Handle)
2022-08-22 09:09:53 +02:00
s.AppendAuthorizedRoute(`/lndhub/ext/{.*}`, api.AuthTypeBearerBase64, api.AccessKeyTypeAdmin, bot.DB.Users, hub.Handle)
s.AppendAuthorizedRoute(`/lndhub/ext`, api.AuthTypeBearerBase64, api.AccessKeyTypeAdmin, bot.DB.Users, hub.Handle)
2022-08-19 22:55:27 +02:00
// starting api service
2022-08-20 00:17:58 +02:00
apiService := api.Service{Bot: bot}
2022-08-22 09:09:53 +02:00
s.AppendAuthorizedRoute(`/api/v1/paymentstatus/{payment_hash}`, api.AuthTypeBasic, api.AccessKeyTypeInvoice, bot.DB.Users, apiService.PaymentStatus, http.MethodPost)
s.AppendAuthorizedRoute(`/api/v1/invoicestatus/{payment_hash}`, api.AuthTypeBasic, api.AccessKeyTypeInvoice, bot.DB.Users, apiService.InvoiceStatus, http.MethodPost)
s.AppendAuthorizedRoute(`/api/v1/payinvoice`, api.AuthTypeBasic, api.AccessKeyTypeAdmin, bot.DB.Users, apiService.PayInvoice, http.MethodPost)
s.AppendAuthorizedRoute(`/api/v1/invoicestream`, api.AuthTypeBasic, api.AccessKeyTypeInvoice, bot.DB.Users, apiService.InvoiceStream, http.MethodGet)
s.AppendAuthorizedRoute(`/api/v1/createinvoice`, api.AuthTypeBasic, api.AccessKeyTypeInvoice, bot.DB.Users, apiService.CreateInvoice, http.MethodPost)
s.AppendAuthorizedRoute(`/api/v1/balance`, api.AuthTypeBasic, api.AccessKeyTypeInvoice, bot.DB.Users, apiService.Balance, http.MethodGet)
2022-08-19 22:32:08 +02:00
// start internal admin server
adminService := admin.New(bot)
2022-08-22 09:09:53 +02:00
internalAdminServer := api.NewServer(internal.Configuration.Bot.AdminAPIHost)
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.AppendRoute("/admin/dalle/enable", adminService.EnableDalle)
internalAdminServer.AppendRoute("/admin/dalle/disable", adminService.DisableDalle)
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()
}
}