wall

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 2, 2018 License: MPL-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package wall contains all paywall-related code.

This is the package you need to use for creating a middleware for one of the supported handlers / routers / frameworks. For creating a middleware you only need to call one of the provided factory functions, but all functions require a storage client (an implementation of the wall.StorageClient interface) as parameter. You can either pick one from the storage package (https://www.godoc.org/github.com/philippgille/ln-paywall/storage), or implement your own.

Usage

Here's one example of a web service implemented with Gin. For more examples check out the "examples" directory in the GitHub repository of this package (https://github.com/philippgille/ln-paywall/tree/master/examples).

package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/philippgille/ln-paywall/ln"
	"github.com/philippgille/ln-paywall/storage"
	"github.com/philippgille/ln-paywall/wall"
)

func main() {
	r := gin.Default()

	// 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)
	}
	// Use middleware
	r.Use(wall.NewGinMiddleware(invoiceOptions, lnClient, storageClient))

	r.GET("/ping", func(c *gin.Context) {
		c.String(http.StatusOK, "pong")
	})

	r.Run() // Listen and serve on 0.0.0.0:8080
}

Index

Constants

This section is empty.

Variables

View Source
var DefaultInvoiceOptions = InvoiceOptions{
	Price: 1,
	Memo:  "API call",
}

DefaultInvoiceOptions provides default values for InvoiceOptions.

Functions

func NewEchoMiddleware

func NewEchoMiddleware(invoiceOptions InvoiceOptions, lnClient LNclient, storageClient StorageClient, skipper middleware.Skipper) echo.MiddlewareFunc

NewEchoMiddleware returns an Echo middleware in the form of an echo.MiddlewareFunc.

func NewGinMiddleware

func NewGinMiddleware(invoiceOptions InvoiceOptions, lnClient LNclient, storageClient StorageClient) gin.HandlerFunc

NewGinMiddleware returns a Gin middleware in the form of a gin.HandlerFunc.

func NewHandlerFuncMiddleware

func NewHandlerFuncMiddleware(invoiceOptions InvoiceOptions, lnClient LNclient, storageClient StorageClient) func(http.HandlerFunc) http.HandlerFunc

NewHandlerFuncMiddleware returns a function which you can use within an http.HandlerFunc chain.

func NewHandlerMiddleware

func NewHandlerMiddleware(invoiceOptions InvoiceOptions, lnClient LNclient, storageClient StorageClient) func(http.Handler) http.Handler

NewHandlerMiddleware returns a function which you can use within an http.Handler chain.

Types

type InvoiceOptions

type InvoiceOptions struct {
	// Amount of Satoshis you want to have paid for one API call.
	// Values below 1 are automatically changed to the default value.
	// Optional (1 by default).
	Price int64
	// Note to be shown on the invoice,
	// for example: "API call to api.example.com".
	// Optional ("" by default).
	Memo string
}

InvoiceOptions are the options for an invoice.

type LNclient

type LNclient interface {
	GenerateInvoice(int64, string) (string, error)
	CheckInvoice(string) (bool, error)
}

LNclient is an abstraction of a client that connects to a Lightning Network node implementation (like lnd, c-lightning and eclair) and provides the methods required by the paywall.

type StorageClient

type StorageClient interface {
	WasUsed(string) (bool, error)
	SetUsed(string) error
}

StorageClient is an abstraction for different storage client implementations. A storage client must only be able to check if a preimage was already used for a payment bofore and to store a preimage that was used before.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL