Skip to content
This repository has been archived by the owner on Feb 25, 2023. It is now read-only.

Commit

Permalink
Define rough architecture for middleware
Browse files Browse the repository at this point in the history
The functionality of the middleware is now split into three packages. The
middleware package takes care of communicating with bitcoind and
lightningd and emits an event if new data is received.

The handlers package takes care of the communication with
the bitbox-wallet-app. It upgrades connections to websocket, if
requested, and starts the main middleware event loop. Events from the
middleware are caught and passed into a websocket handler.

The main package is what is compiled into a binary. It parses command
line arguments and creates new middleware and handler instances.
This commit also implements logging, where each package creates its own
logrus logging instance.
  • Loading branch information
TheCharlatan committed Jun 3, 2019
1 parent 6111778 commit 71cbcf4
Show file tree
Hide file tree
Showing 32 changed files with 3,002 additions and 151 deletions.
9 changes: 9 additions & 0 deletions middleware/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions middleware/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ envinit:
./scripts/go-get.sh v1.16.0 github.com/golangci/golangci-lint/cmd/golangci-lint
go get -u github.com/golang/dep/cmd/dep
native: check-env
go build -o $(REPO_ROOT)/build/base-middleware ./src/
go build -o $(REPO_ROOT)/build/base-middleware ./cmd/middleware/
aarch64: check-env
GOARCH=arm64 go build -o $(REPO_ROOT)/build/base-middleware ./src/
GOARCH=arm64 go build -o $(REPO_ROOT)/build/base-middleware ./cmd/middleware/
38 changes: 38 additions & 0 deletions middleware/cmd/middleware/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Package main provides the entry point into the middleware and accepts command line arguments
package main

import (
"flag"
"log"
"net/http"

middleware "github.com/digitalbitbox/bitbox-base/middleware/src"
"github.com/digitalbitbox/bitbox-base/middleware/src/handlers"
)

func main() {
bitcoinRPCUser := flag.String("rpcuser", "rpcuser", "Bitcoin rpc user name")
bitcoinRPCPassword := flag.String("rpcpassword", "rpcpassword", "Bitcoin rpc password")
bitcoinRPCPort := flag.String("rpcport", "8332", "Bitcoin rpc port, localhost is assumed as an address")
lightningRPCPath := flag.String("lightning-rpc-path", "/home/bitcoin/.lightning/lightning-rpc", "Path to the lightning rpc unix socket")
flag.Parse()

logBeforeExit := func() {
// Recover from all panics and log error before panicking again.
if r := recover(); r != nil {
// r is of type interface{} and thus we cannot use log.WithError(r).
log.Printf("%v, error detected, shutting down", r)
panic(r)
}
}
defer logBeforeExit()
middleware := middleware.NewMiddleware(*bitcoinRPCUser, *bitcoinRPCPassword, *bitcoinRPCPort, *lightningRPCPath)
log.Println("--------------- Started middleware --------------")

handlers := handlers.NewHandlers(middleware)
log.Println("Binding middleware api to port 8845")

if err := http.ListenAndServe(":8845", handlers.Router); err != nil {
log.Println(err.Error() + " Failed to listen for HTTP")
}
}
91 changes: 91 additions & 0 deletions middleware/src/handlers/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Package handlers implements an api for the bitbox-wallet-app to talk to.
package handlers

import (
"fmt"
"log"
"net/http"

middleware "github.com/digitalbitbox/bitbox-base/middleware/src"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
//"github.com/sirupsen/logrus"
)

// Middleware provides an interface to the middleware package.
type Middleware interface {
Start() <-chan *middleware.SampleInfo
}

// Handlers provides a web api
type Handlers struct {
Router *mux.Router
clients map[*websocket.Conn]bool
upgrader websocket.Upgrader //upgrader takes an http request and upgrades the connection with its origin to websocket
//log log.Logger
middleware Middleware
//TODO(TheCharlatan): In future this event should have a generic interface (thus only containing raw json)
middlewareEvents <-chan *middleware.SampleInfo
}

// NewHandlers returns a handler instance.
func NewHandlers(middlewareInstance Middleware) *Handlers {
router := mux.NewRouter()

handlers := &Handlers{
middleware: middlewareInstance,
Router: router,
clients: make(map[*websocket.Conn]bool),
upgrader: websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
},
//log: log.NewLogger(),
}
//log.WithField("group", "Handlers")
handlers.Router.HandleFunc("/", handlers.rootHandler).Methods("GET")
handlers.Router.HandleFunc("/ws", handlers.wsHandler)

handlers.middlewareEvents = handlers.middleware.Start()
go handlers.echo()
return handlers
}

// rootHandler provides an endpoint to indicate that the middleware is online and able to handle requests. TODO(TheCharlatan): Define a better error-response system. In future, this should be the first step in an authentication procedure.
func (handlers *Handlers) rootHandler(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("OK!!\n"))
if err != nil {
log.Println(err.Error() + " Failed to write response bytes in root handler")
}
}

func (handlers *Handlers) wsHandler(w http.ResponseWriter, r *http.Request) {
ws, err := handlers.upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err.Error() + " Failed to upgrade connection")
}

// register client
handlers.clients[ws] = true
}

func (handlers *Handlers) echo() {
var i = 0
for {
i++
val := <-handlers.middlewareEvents
blockinfo := fmt.Sprintf("%d %f %d %s", val.Blocks, val.Difficulty, i, val.LightningAlias)
// send to every client that is currently connected
//fmt.Println(blockinfo)
for client := range handlers.clients {
log.Println(blockinfo)
err := client.WriteMessage(websocket.TextMessage, []byte(blockinfo))
if err != nil {
log.Println(err.Error() + " Unexpected websocket error")
client.Close()
delete(handlers.clients, client)
}
}
}
}
149 changes: 0 additions & 149 deletions middleware/src/main.go

This file was deleted.

Loading

0 comments on commit 71cbcf4

Please sign in to comment.