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.3 KiB

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