goBlockchainDataAnalysis/main.go
main.goRaw
1package main
2
3import (
4 "log"
5 "net/http"
6 "os"
7
8 mgo "gopkg.in/mgo.v2"
9
10 "github.com/btcsuite/btcrpcclient"
11 "github.com/fatih/color"
12 "github.com/gorilla/handlers"
13)
14
15var blockCollection *mgo.Collection
16var nodeCollection *mgo.Collection
17var edgeCollection *mgo.Collection
18var dateCountCollection *mgo.Collection
19var hourCountCollection *mgo.Collection
20
21func main() {
22 savelog()
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 if len(os.Args) > 1 {
37 if os.Args[1] == "-explore" {
38 // create new client instance
39 client, err := btcrpcclient.New(&btcrpcclient.ConnConfig{
40 HTTPPostMode: true,
41 DisableTLS: true,
42 Host: config.Host + ":" + config.Port,
43 User: config.User,
44 Pass: config.Pass,
45 }, nil)
46 if err != nil {
47 log.Fatalf("error creating new btc client: %v", err)
48 }
49
50 // list accounts
51 accounts, err := client.ListAccounts()
52 if err != nil {
53 log.Fatalf("error listing accounts: %v", err)
54 }
55 // iterate over accounts (map[string]btcutil.Amount) and write to stdout
56 for label, amount := range accounts {
57 log.Printf("%s: %s", label, amount)
58 }
59 color.Blue("starting to explore blockchain")
60 explore(client, config.GenesisBlock)
61
62 // Get the current block count.
63 blockCount, err := client.GetBlockCount()
64 if err != nil {
65 log.Fatal(err)
66 }
67 log.Printf("Block count: %d", blockCount)
68 }
69 }
70
71 //http server start
72 readServerConfig("./serverConfig.json")
73 log.Println("server running")
74 log.Print("port: ")
75 log.Println(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}