herald.go/server/server.go

185 lines
4.2 KiB
Go
Raw Normal View History

2021-04-19 21:25:34 +02:00
package server
2021-03-18 22:14:56 +01:00
import (
2021-09-18 19:21:32 +02:00
"context"
2021-10-01 19:54:03 +02:00
"crypto/sha256"
2021-09-18 19:21:32 +02:00
"fmt"
2021-10-01 19:54:03 +02:00
"hash"
2021-06-18 06:01:47 +02:00
"log"
2021-10-01 19:54:03 +02:00
"net/http"
"os"
2021-06-18 06:01:47 +02:00
"regexp"
2021-09-21 20:02:09 +02:00
"time"
2021-10-01 19:54:03 +02:00
"github.com/ReneKroon/ttlcache/v2"
2021-09-24 22:24:22 +02:00
"github.com/lbryio/hub/meta"
2021-03-18 22:14:56 +01:00
pb "github.com/lbryio/hub/protobuf/go"
2021-06-04 07:56:50 +02:00
"github.com/olivere/elastic/v7"
2021-07-06 02:20:38 +02:00
"github.com/prometheus/client_golang/prometheus/promhttp"
"google.golang.org/grpc"
2021-03-18 22:14:56 +01:00
)
2021-04-19 21:25:34 +02:00
type Server struct {
2021-10-03 04:49:49 +02:00
GrpcServer *grpc.Server
Args *Args
MultiSpaceRe *regexp.Regexp
WeirdCharsRe *regexp.Regexp
EsClient *elastic.Client
Servers []*FederatedServer
QueryCache *ttlcache.Cache
S256 *hash.Hash
LastRefreshCheck time.Time
RefreshDelta time.Duration
NumESRefreshes int64
2021-03-18 22:14:56 +01:00
pb.UnimplementedHubServer
}
2021-07-06 02:20:38 +02:00
type FederatedServer struct {
Address string
2021-09-21 20:02:09 +02:00
Port string
Ts time.Time
Ping int //?
2021-07-06 02:20:38 +02:00
}
const (
2021-09-21 20:02:09 +02:00
ServeCmd = iota
2021-07-06 02:20:38 +02:00
SearchCmd = iota
)
type Args struct {
2021-07-06 02:20:38 +02:00
// TODO Make command types an enum
CmdType int
2021-08-11 05:39:37 +02:00
Host string
Port string
EsHost string
EsPort string
EsIndex string
Debug bool
}
2021-09-24 22:24:22 +02:00
func getVersion() string {
return meta.Version
2021-07-06 02:20:38 +02:00
}
2021-03-18 22:14:56 +01:00
/*
'blockchain.block.get_chunk'
'blockchain.block.get_header'
'blockchain.estimatefee'
'blockchain.relayfee'
'blockchain.scripthash.get_balance'
'blockchain.scripthash.get_history'
'blockchain.scripthash.get_mempool'
'blockchain.scripthash.listunspent'
'blockchain.scripthash.subscribe'
'blockchain.transaction.broadcast'
'blockchain.transaction.get'
'blockchain.transaction.get_batch'
'blockchain.transaction.info'
'blockchain.transaction.get_merkle'
'server.add_peer'
'server.banner'
'server.payment_address'
'server.donation_address'
'server.features'
'server.peers.subscribe'
'server.version'
'blockchain.transaction.get_height'
'blockchain.claimtrie.search'
'blockchain.claimtrie.resolve'
'blockchain.claimtrie.getclaimsbyids'
'blockchain.block.get_server_height'
'mempool.get_fee_histogram'
'blockchain.block.headers'
'server.ping'
'blockchain.headers.subscribe'
'blockchain.address.get_balance'
'blockchain.address.get_history'
'blockchain.address.get_mempool'
'blockchain.address.listunspent'
'blockchain.address.subscribe'
'blockchain.address.unsubscribe'
*/
2021-06-01 04:19:10 +02:00
func MakeHubServer(args *Args) *Server {
2021-07-06 02:20:38 +02:00
grpcServer := grpc.NewServer(grpc.NumStreamWorkers(10))
2021-06-01 04:19:10 +02:00
2021-08-24 10:45:30 +02:00
multiSpaceRe, err := regexp.Compile(`\s{2,}`)
2021-06-01 04:19:10 +02:00
if err != nil {
log.Fatal(err)
}
weirdCharsRe, err := regexp.Compile("[#!~]")
if err != nil {
log.Fatal(err)
}
2021-07-06 02:20:38 +02:00
self := &FederatedServer{
Address: "127.0.0.1",
2021-09-21 20:02:09 +02:00
Port: args.Port,
Ts: time.Now(),
Ping: 0,
2021-07-06 02:20:38 +02:00
}
servers := make([]*FederatedServer, 10)
servers = append(servers, self)
2021-06-01 04:19:10 +02:00
esUrl := args.EsHost + ":" + args.EsPort
opts := []elastic.ClientOptionFunc{elastic.SetSniff(false), elastic.SetURL(esUrl)}
if args.Debug {
opts = append(opts, elastic.SetTraceLog(log.New(os.Stderr, "[[ELASTIC]]", 0)))
}
client, err := elastic.NewClient(opts...)
if err != nil {
log.Fatal(err)
}
2021-10-01 19:54:03 +02:00
cache := ttlcache.NewCache()
err = cache.SetTTL(5 * time.Minute)
if err != nil {
log.Fatal(err)
}
s256 := sha256.New()
2021-10-03 04:49:49 +02:00
var refreshDelta = time.Second * 2
if args.Debug {
refreshDelta = time.Second * 0
}
2021-08-11 05:39:37 +02:00
s := &Server{
2021-10-03 04:49:49 +02:00
GrpcServer: grpcServer,
Args: args,
MultiSpaceRe: multiSpaceRe,
WeirdCharsRe: weirdCharsRe,
EsClient: client,
QueryCache: cache,
S256: &s256,
LastRefreshCheck: time.Now(),
RefreshDelta: refreshDelta,
NumESRefreshes: 0,
2021-06-01 04:19:10 +02:00
}
return s
2021-07-06 02:20:38 +02:00
}
func (s *Server) PromethusEndpoint(port string, endpoint string) error {
2021-09-21 20:02:09 +02:00
http.Handle("/"+endpoint, promhttp.Handler())
2021-07-06 02:20:38 +02:00
log.Println(fmt.Sprintf("listening on :%s /%s", port, endpoint))
2021-09-21 20:02:09 +02:00
err := http.ListenAndServe(":"+port, nil)
2021-07-06 02:20:38 +02:00
if err != nil {
return err
}
log.Fatalln("Shouldn't happen??!?!")
return nil
}
func (s *Server) Hello(context context.Context, args *FederatedServer) (*FederatedServer, error) {
s.Servers = append(s.Servers, args)
return s.Servers[0], nil
}
2021-09-18 19:21:32 +02:00
func (s *Server) Ping(context context.Context, args *pb.EmptyMessage) (*pb.StringValue, error) {
return &pb.StringValue{Value: "Hello, world!"}, nil
2021-07-06 02:20:38 +02:00
}
2021-09-18 19:21:32 +02:00
func (s *Server) Version(context context.Context, args *pb.EmptyMessage) (*pb.StringValue, error) {
2021-09-24 22:24:22 +02:00
return &pb.StringValue{Value: getVersion()}, nil
2021-09-18 19:21:32 +02:00
}