-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
40 lines (33 loc) · 1.16 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
package main
import (
"fmt"
"log"
"net/http"
"github.com/philippgille/ln-paywall/ln"
"github.com/philippgille/ln-paywall/storage"
"github.com/philippgille/ln-paywall/wall"
)
func pingHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "pong")
}
func withLogging(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Printf("Logged connection from %s", r.RemoteAddr)
next.ServeHTTP(w, r)
}
}
func main() {
// Configure middleware
invoiceOptions := wall.DefaultInvoiceOptions // Price: 1 Satoshi; Memo: "API call"
lndOptions := ln.DefaultLNDoptions // Address: "localhost:10009", CertFile: "tls.cert", MacaroonFile: "invoice.macaroon"
storageClient := storage.NewGoMap() // Local in-memory cache
lnClient, err := ln.NewLNDclient(lndOptions)
if err != nil {
panic(err)
}
// Create function that we can use in the middleware chain
withPayment := wall.NewHandlerFuncMiddleware(invoiceOptions, lnClient, storageClient)
// Use a chain of middlewares for the "/ping" endpoint
http.HandleFunc("/ping", withLogging(withPayment(pingHandler)))
log.Fatal(http.ListenAndServe(":8080", nil))
}