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.

105 lines
3.0 KiB

  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "os"
  6. "time"
  7. mgo "gopkg.in/mgo.v2"
  8. "github.com/btcsuite/btcd/rpcclient"
  9. "github.com/gorilla/handlers"
  10. )
  11. var statsCollection *mgo.Collection
  12. var blockCollection *mgo.Collection
  13. var txCollection *mgo.Collection
  14. var addressCollection *mgo.Collection
  15. var nodeCollection *mgo.Collection
  16. var edgeCollection *mgo.Collection
  17. var dateCountCollection *mgo.Collection
  18. var hourCountCollection *mgo.Collection
  19. func main() {
  20. savelog()
  21. //read goBlockchainDataAbalysis config
  22. readConfig("config.json")
  23. //connect with mongodb
  24. //readMongodbConfig("./mongodbConfig.json")
  25. session, err := getSession()
  26. check(err)
  27. statsCollection = getCollection(session, "stats")
  28. blockCollection = getCollection(session, "blocks")
  29. txCollection = getCollection(session, "txs")
  30. addressCollection = getCollection(session, "address")
  31. nodeCollection = getCollection(session, "nodes")
  32. edgeCollection = getCollection(session, "edges")
  33. dateCountCollection = getCollection(session, "dateCounts")
  34. hourCountCollection = getCollection(session, "hourCounts")
  35. if len(os.Args) > 1 {
  36. if os.Args[1] == "-explore" {
  37. // create new client instance
  38. client, err := rpcclient.New(&rpcclient.ConnConfig{
  39. HTTPPostMode: true,
  40. DisableTLS: true,
  41. Host: config.Host + ":" + config.Port,
  42. User: config.User,
  43. Pass: config.Pass,
  44. }, nil)
  45. if err != nil {
  46. log.Fatalf("error creating new btc client: %v", err)
  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. log.Println("starting to explore blockchain")
  58. start := time.Now()
  59. explore(client, config.GenesisBlock)
  60. log.Println("blockchain exploration finished, time:")
  61. log.Println(time.Since(start))
  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. if os.Args[1] == "-continue" {
  70. explorationContinue()
  71. }
  72. }
  73. go continuousExploration()
  74. //run thw webserver
  75. go webserver()
  76. //http server start
  77. //readServerConfig("./serverConfig.json")
  78. log.Println("server running")
  79. log.Print("port: ")
  80. log.Println(config.Server.ServerPort)
  81. router := NewRouter()
  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(":"+config.Server.ServerPort, handlers.CORS(originsOk, headersOk, methodsOk)(router)))
  86. //log.Fatal(http.ListenAndServe(":"+serverConfig.ServerPort, router))
  87. }
  88. func webserver() {
  89. log.Println("webserver in port " + config.Server.WebServerPort)
  90. http.Handle("/", http.FileServer(http.Dir("./web")))
  91. http.ListenAndServe(":"+config.Server.WebServerPort, nil)
  92. }