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