Skip to content

Commit

Permalink
Move endpoint to root path and improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
w3irdrobot committed Jul 12, 2022
1 parent 3b3eb13 commit bc47589
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
16 changes: 9 additions & 7 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ type lndUnlock struct {
StatelessInit bool `json:"stateless_init"`
}

type unlockHandler struct {
type handler struct {
ctx context.Context
secret string
nodeAPI string
walletPassword string
}

func newUnlockHandler(ctx context.Context, secret, nodeAPI, walletPassword string) *unlockHandler {
return &unlockHandler{ctx, secret, nodeAPI, walletPassword}
func newHandler(ctx context.Context, secret, nodeAPI, walletPassword string) *handler {
return &handler{ctx, secret, nodeAPI, walletPassword}
}

func (h *unlockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)

Expand All @@ -69,7 +69,9 @@ func (h *unlockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.handleEvent(w, event)
}

func (h *unlockHandler) handleEvent(w http.ResponseWriter, event unlockEvent) {
func (h *handler) handleEvent(w http.ResponseWriter, event unlockEvent) {
log.Printf("received request for %s", event.Details.Status)

if event.API != h.nodeAPI {
log.Printf("api '%s' does not match expected api\n", event.API)
w.WriteHeader(http.StatusBadRequest)
Expand All @@ -96,7 +98,7 @@ func (h *unlockHandler) handleEvent(w http.ResponseWriter, event unlockEvent) {
StatelessInit: true,
})
if err != nil {
log.Printf("error marshalling the request to lnd")
log.Printf("error marshalling the request to lnd: %s", err)
w.WriteHeader(http.StatusInternalServerError)

return
Expand All @@ -110,7 +112,7 @@ func (h *unlockHandler) handleEvent(w http.ResponseWriter, event unlockEvent) {

res, err := http.Post(endpoint.String(), "application/json", bytes.NewBuffer(body))
if err != nil {
log.Printf("error sending the request to lnd")
log.Printf("error sending the request to lnd: %s", err)
w.WriteHeader(http.StatusInternalServerError)

return
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ func main() {
server := &http.Server{Addr: cfg.address, Handler: router}
appCtx, appCancel := context.WithCancel(context.Background())

router.Handle("/unlock", newUnlockHandler(appCtx, cfg.webhookSecret, cfg.nodeAPI, cfg.walletPassword))
router.Handle("/", newHandler(appCtx, cfg.webhookSecret, cfg.nodeAPI, cfg.walletPassword))

signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)

shutdownComplete := make(chan struct{}, 1)

go func() {
log.Println("shutting down the server")
<-signals
log.Println("shutting down the server")

ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer cancel()
Expand Down

0 comments on commit bc47589

Please sign in to comment.