arnaucube
arnaucube/goBlockchainDataAnalysisPublic Self-hosted repository
goBlockchainDataAnalysis/main.go
main.goRaw
  1package main
  2
  3import (
  4	"log"
  5	"net/http"
  6	"os"
  7	"time"
  8
  9	mgo "gopkg.in/mgo.v2"
 10
 11	"github.com/btcsuite/btcrpcclient"
 12	"github.com/fatih/color"
 13	"github.com/gorilla/handlers"
 14)
 15
 16var blockCollection *mgo.Collection
 17var txCollection *mgo.Collection
 18var addressCollection *mgo.Collection
 19var nodeCollection *mgo.Collection
 20var edgeCollection *mgo.Collection
 21var dateCountCollection *mgo.Collection
 22var hourCountCollection *mgo.Collection
 23
 24func main() {
 25	savelog()
 26	//read goBlockchainDataAbalysis config
 27	readConfig("config.json")
 28
 29	//connect with mongodb
 30	//readMongodbConfig("./mongodbConfig.json")
 31	session, err := getSession()
 32	check(err)
 33	blockCollection = getCollection(session, "blocks")
 34	txCollection = getCollection(session, "txs")
 35	addressCollection = getCollection(session, "address")
 36	nodeCollection = getCollection(session, "nodes")
 37	edgeCollection = getCollection(session, "edges")
 38	dateCountCollection = getCollection(session, "dateCounts")
 39	hourCountCollection = getCollection(session, "hourCounts")
 40
 41	if len(os.Args) > 1 {
 42		if os.Args[1] == "-explore" {
 43			// create new client instance
 44			client, err := btcrpcclient.New(&btcrpcclient.ConnConfig{
 45				HTTPPostMode: true,
 46				DisableTLS:   true,
 47				Host:         config.Host + ":" + config.Port,
 48				User:         config.User,
 49				Pass:         config.Pass,
 50			}, nil)
 51			if err != nil {
 52				log.Fatalf("error creating new btc client: %v", err)
 53			}
 54
 55			// list accounts
 56			accounts, err := client.ListAccounts()
 57			if err != nil {
 58				log.Fatalf("error listing accounts: %v", err)
 59			}
 60			// iterate over accounts (map[string]btcutil.Amount) and write to stdout
 61			for label, amount := range accounts {
 62				log.Printf("%s: %s", label, amount)
 63			}
 64			color.Blue("starting to explore blockchain")
 65			start := time.Now()
 66			explore(client, config.GenesisBlock)
 67			log.Println("blockchain exploration finished, time:")
 68			log.Println(time.Since(start))
 69
 70			// Get the current block count.
 71			blockCount, err := client.GetBlockCount()
 72			if err != nil {
 73				log.Fatal(err)
 74			}
 75			log.Printf("Block count: %d", blockCount)
 76		}
 77	}
 78	//run thw webserver
 79	go webserver()
 80
 81	//http server start
 82	//readServerConfig("./serverConfig.json")
 83	log.Println("server running")
 84	log.Print("port: ")
 85	log.Println(config.Server.ServerPort)
 86	router := NewRouter()
 87
 88	headersOk := handlers.AllowedHeaders([]string{"X-Requested-With", "Access-Control-Allow-Origin"})
 89	originsOk := handlers.AllowedOrigins([]string{"*"})
 90	methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"})
 91	log.Fatal(http.ListenAndServe(":"+config.Server.ServerPort, handlers.CORS(originsOk, headersOk, methodsOk)(router)))
 92	//log.Fatal(http.ListenAndServe(":"+serverConfig.ServerPort, router))
 93
 94}
 95
 96func webserver() {
 97	log.Println("webserver in port " + config.Server.WebServerPort)
 98	http.Handle("/", http.FileServer(http.Dir("./web")))
 99	http.ListenAndServe(":"+config.Server.WebServerPort, nil)
100}