global_store.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2019 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "io"
  19. "net"
  20. "net/http"
  21. "os"
  22. "github.com/ethereum/go-ethereum/log"
  23. "github.com/ethereum/go-ethereum/rpc"
  24. "github.com/ethereum/go-ethereum/swarm/storage/mock"
  25. "github.com/ethereum/go-ethereum/swarm/storage/mock/db"
  26. "github.com/ethereum/go-ethereum/swarm/storage/mock/mem"
  27. cli "gopkg.in/urfave/cli.v1"
  28. )
  29. // startHTTP starts a global store with HTTP RPC server.
  30. // It is used for "http" cli command.
  31. func startHTTP(ctx *cli.Context) (err error) {
  32. server, cleanup, err := newServer(ctx)
  33. if err != nil {
  34. return err
  35. }
  36. defer cleanup()
  37. listener, err := net.Listen("tcp", ctx.String("addr"))
  38. if err != nil {
  39. return err
  40. }
  41. log.Info("http", "address", listener.Addr().String())
  42. return http.Serve(listener, server)
  43. }
  44. // startWS starts a global store with WebSocket RPC server.
  45. // It is used for "websocket" cli command.
  46. func startWS(ctx *cli.Context) (err error) {
  47. server, cleanup, err := newServer(ctx)
  48. if err != nil {
  49. return err
  50. }
  51. defer cleanup()
  52. listener, err := net.Listen("tcp", ctx.String("addr"))
  53. if err != nil {
  54. return err
  55. }
  56. origins := ctx.StringSlice("origins")
  57. log.Info("websocket", "address", listener.Addr().String(), "origins", origins)
  58. return http.Serve(listener, server.WebsocketHandler(origins))
  59. }
  60. // newServer creates a global store and starts a chunk explorer server if configured.
  61. // Returned cleanup function should be called only if err is nil.
  62. func newServer(ctx *cli.Context) (server *rpc.Server, cleanup func(), err error) {
  63. log.PrintOrigins(true)
  64. log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(ctx.Int("verbosity")), log.StreamHandler(os.Stdout, log.TerminalFormat(false))))
  65. cleanup = func() {}
  66. var globalStore mock.GlobalStorer
  67. dir := ctx.String("dir")
  68. if dir != "" {
  69. dbStore, err := db.NewGlobalStore(dir)
  70. if err != nil {
  71. return nil, nil, err
  72. }
  73. cleanup = func() {
  74. if err := dbStore.Close(); err != nil {
  75. log.Error("global store: close", "err", err)
  76. }
  77. }
  78. globalStore = dbStore
  79. log.Info("database global store", "dir", dir)
  80. } else {
  81. globalStore = mem.NewGlobalStore()
  82. log.Info("in-memory global store")
  83. }
  84. server = rpc.NewServer()
  85. if err := server.RegisterName("mockStore", globalStore); err != nil {
  86. cleanup()
  87. return nil, nil, err
  88. }
  89. shutdown, err := serveChunkExplorer(ctx, globalStore)
  90. if err != nil {
  91. cleanup()
  92. return nil, nil, err
  93. }
  94. if shutdown != nil {
  95. cleanup = func() {
  96. shutdown()
  97. if c, ok := globalStore.(io.Closer); ok {
  98. if err := c.Close(); err != nil {
  99. log.Error("global store: close", "err", err)
  100. }
  101. }
  102. }
  103. }
  104. return server, cleanup, nil
  105. }