arnaucube
arnaucube/goBlockchainDataAnalysisPublic Self-hosted repository
goBlockchainDataAnalysis/main.go
main.goRaw
 1package main
 2
 3import (
 4	"fmt"
 5	"log"
 6	"net/http"
 7	"os"
 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 nodeCollection *mgo.Collection
18var edgeCollection *mgo.Collection
19var dateCountCollection *mgo.Collection
20var hourCountCollection *mgo.Collection
21
22func main() {
23	//read goBlockchainDataAbalysis config
24	readConfig("config.json")
25
26	//connect with mongodb
27	readMongodbConfig("./mongodbConfig.json")
28	session, err := getSession()
29	check(err)
30	blockCollection = getCollection(session, "blocks")
31	nodeCollection = getCollection(session, "nodes")
32	edgeCollection = getCollection(session, "edges")
33	dateCountCollection = getCollection(session, "dateCounts")
34	hourCountCollection = getCollection(session, "hourCounts")
35
36	// create new client instance
37	client, err := btcrpcclient.New(&btcrpcclient.ConnConfig{
38		HTTPPostMode: true,
39		DisableTLS:   true,
40		Host:         config.Host + ":" + config.Port,
41		User:         config.User,
42		Pass:         config.Pass,
43	}, nil)
44	if err != nil {
45		log.Fatalf("error creating new btc client: %v", err)
46	}
47
48	// list accounts
49	accounts, err := client.ListAccounts()
50	if err != nil {
51		log.Fatalf("error listing accounts: %v", err)
52	}
53	// iterate over accounts (map[string]btcutil.Amount) and write to stdout
54	for label, amount := range accounts {
55		log.Printf("%s: %s", label, amount)
56	}
57	if len(os.Args) > 1 {
58		if os.Args[1] == "-explore" {
59			color.Blue("starting to explore blockchain")
60			explore(client, config.GenesisBlock)
61		}
62		/*if os.Args[1] == "-tree" {
63			color.Blue("starting to make tree")
64			addressTree(client, "fY3HZxu7HFKRcYzVSTXRZpAJMP4qba2oR6")
65		}*/
66	}
67
68	// Get the current block count.
69	blockCount, err := client.GetBlockCount()
70	if err != nil {
71		log.Fatal(err)
72	}
73	log.Printf("Block count: %d", blockCount)
74
75	//http server start
76	readServerConfig("./serverConfig.json")
77	color.Green("server running")
78	fmt.Print("port: ")
79	color.Green(serverConfig.ServerPort)
80	router := NewRouter()
81
82	headersOk := handlers.AllowedHeaders([]string{"X-Requested-With", "Access-Control-Allow-Origin"})
83	originsOk := handlers.AllowedOrigins([]string{"*"})
84	methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"})
85	log.Fatal(http.ListenAndServe(":"+serverConfig.ServerPort, handlers.CORS(originsOk, headersOk, methodsOk)(router)))
86	//log.Fatal(http.ListenAndServe(":"+serverConfig.ServerPort, router))
87
88}