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/btcd/rpcclient"
 12	"github.com/gorilla/handlers"
 13)
 14
 15var statsCollection *mgo.Collection
 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	statsCollection = getCollection(session, "stats")
 34	blockCollection = getCollection(session, "blocks")
 35	txCollection = getCollection(session, "txs")
 36	addressCollection = getCollection(session, "address")
 37	nodeCollection = getCollection(session, "nodes")
 38	edgeCollection = getCollection(session, "edges")
 39	dateCountCollection = getCollection(session, "dateCounts")
 40	hourCountCollection = getCollection(session, "hourCounts")
 41
 42	if len(os.Args) > 1 {
 43		if os.Args[1] == "-explore" {
 44			// create new client instance
 45			client, err := rpcclient.New(&rpcclient.ConnConfig{
 46				HTTPPostMode: true,
 47				DisableTLS:   true,
 48				Host:         config.Host + ":" + config.Port,
 49				User:         config.User,
 50				Pass:         config.Pass,
 51			}, nil)
 52			if err != nil {
 53				log.Fatalf("error creating new btc client: %v", err)
 54			}
 55
 56			// list accounts
 57			accounts, err := client.ListAccounts()
 58			if err != nil {
 59				log.Fatalf("error listing accounts: %v", err)
 60			}
 61			// iterate over accounts (map[string]btcutil.Amount) and write to stdout
 62			for label, amount := range accounts {
 63				log.Printf("%s: %s", label, amount)
 64			}
 65			log.Println("starting to explore blockchain")
 66			start := time.Now()
 67			explore(client, config.GenesisBlock)
 68			log.Println("blockchain exploration finished, time:")
 69			log.Println(time.Since(start))
 70
 71			// Get the current block count.
 72			blockCount, err := client.GetBlockCount()
 73			if err != nil {
 74				log.Fatal(err)
 75			}
 76			log.Printf("Block count: %d", blockCount)
 77		}
 78		if os.Args[1] == "-continue" {
 79			explorationContinue()
 80		}
 81	}
 82	go continuousExploration()
 83	//run thw webserver
 84	go webserver()
 85
 86	//http server start
 87	//readServerConfig("./serverConfig.json")
 88	log.Println("server running")
 89	log.Print("port: ")
 90	log.Println(config.Server.ServerPort)
 91	router := NewRouter()
 92
 93	headersOk := handlers.AllowedHeaders([]string{"X-Requested-With", "Access-Control-Allow-Origin"})
 94	originsOk := handlers.AllowedOrigins([]string{"*"})
 95	methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"})
 96	log.Fatal(http.ListenAndServe(":"+config.Server.ServerPort, handlers.CORS(originsOk, headersOk, methodsOk)(router)))
 97	//log.Fatal(http.ListenAndServe(":"+serverConfig.ServerPort, router))
 98
 99}
100
101func webserver() {
102	log.Println("webserver in port " + config.Server.WebServerPort)
103	http.Handle("/", http.FileServer(http.Dir("./web")))
104	http.ListenAndServe(":"+config.Server.WebServerPort, nil)
105}