You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.2 KiB

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