zaply/server/server.go

65 lines
1.3 KiB
Go
Raw Normal View History

2024-12-26 22:46:18 +00:00
package server
import (
2024-12-27 18:34:36 +00:00
"fmt"
2024-12-27 00:52:47 +00:00
"log"
"net/url"
2024-12-26 22:46:18 +00:00
"github.com/ekzyis/zaply/env"
"github.com/ekzyis/zaply/lightning/phoenixd"
"github.com/ekzyis/zaply/lnurl"
2024-12-27 02:54:09 +00:00
"github.com/ekzyis/zaply/pages"
2024-12-26 22:46:18 +00:00
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
type Server struct {
*echo.Echo
}
func NewServer() *Server {
s := &Server{
Echo: echo.New(),
}
s.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: "${time_custom} ${method} ${uri} ${status}\n",
CustomTimeFormat: "2006-01-02 15:04:05.00000-0700",
}))
2025-01-03 19:30:52 +00:00
u, err := url.Parse(env.PublicUrl)
if err != nil {
log.Fatal(err)
}
2024-12-27 00:52:47 +00:00
webhookPath := "/overlay/webhook"
webhookUrl, err := url.JoinPath(env.PublicUrl, webhookPath)
if err != nil {
log.Fatal(err)
}
2024-12-26 22:46:18 +00:00
p := phoenixd.NewPhoenixd(
phoenixd.WithPhoenixdURL(env.PhoenixdURL),
phoenixd.WithPhoenixdLimitedAccessToken(env.PhoenixdLimitedAccessToken),
2024-12-27 00:52:47 +00:00
phoenixd.WithPhoenixdWebhookUrl(webhookUrl),
2024-12-26 22:46:18 +00:00
)
2024-12-27 00:52:47 +00:00
s.POST(webhookPath, p.WebhookHandler)
2024-12-26 22:46:18 +00:00
lnurl.Router(s.Echo, p)
2024-12-27 02:54:09 +00:00
s.Static("/", "public/")
2024-12-27 18:34:36 +00:00
s.GET("/overlay", pages.OverlayHandler(
2025-01-03 19:30:52 +00:00
lnurl.Encode(fmt.Sprintf("%s/.well-known/lnurlp/%s", env.PublicUrl, "snl")),
fmt.Sprintf("%s@%s", "snl", u.Host),
2024-12-27 18:34:36 +00:00
))
2024-12-27 02:54:09 +00:00
s.GET("/overlay/sse", sseHandler(p.IncomingPayments()))
2024-12-26 22:46:18 +00:00
return s
}
func (s *Server) Start(address string) {
s.Logger.Fatal(s.Echo.Start(address))
}