/
gin.go
50 lines (40 loc) · 1.12 KB
/
gin.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
41
42
43
44
45
46
47
48
49
50
package wall
import (
"net/http"
"github.com/gin-gonic/gin"
)
// NewGinMiddleware returns a Gin middleware in the form of a gin.HandlerFunc.
func NewGinMiddleware(invoiceOptions InvoiceOptions, lnClient LNclient, storageClient StorageClient) gin.HandlerFunc {
invoiceOptions = assignDefaultValues(invoiceOptions)
return func(ctx *gin.Context) {
fa := ginAbstraction{
ctx: ctx,
}
commonHandler(fa, invoiceOptions, lnClient, storageClient)
}
}
type ginAbstraction struct {
ctx *gin.Context
}
func (fa ginAbstraction) getPreimageFromHeader() string {
return fa.ctx.GetHeader("x-preimage")
}
func (fa ginAbstraction) respondWithError(err error, errorMsg string, statusCode int) {
http.Error(fa.ctx.Writer, errorMsg, statusCode)
fa.ctx.Abort()
}
func (fa ginAbstraction) getHTTPrequest() *http.Request {
return fa.ctx.Request
}
func (fa ginAbstraction) respondWithInvoice(headers map[string]string, statusCode int, body []byte) {
for k, v := range headers {
fa.ctx.Header(k, v)
}
fa.ctx.Status(statusCode)
fa.ctx.Writer.Write(body)
fa.ctx.Abort()
}
func (fa ginAbstraction) next() error {
fa.ctx.Next()
return nil
}