api.go 693 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package ipfs_api
  2. import (
  3. "log"
  4. "github.com/ipfs/go-ipfs-api"
  5. "github.com/streamrail/concurrent-map"
  6. "net/http"
  7. )
  8. var sh *MyShell
  9. var Addr = "127.0.0.1:5001"
  10. type MyShell struct {
  11. shell.Shell
  12. Url string
  13. Client *http.Client
  14. cache cmap.ConcurrentMap
  15. }
  16. func reset() {
  17. if sh == nil || !sh.IsUp(){
  18. sh = &MyShell{
  19. Url: Addr,
  20. Client: http.DefaultClient,
  21. cache: cmap.New(),
  22. }
  23. sh.Shell = *shell.NewShellWithClient(sh.Url, sh.Client)
  24. if id, err := sh.ID(); err == nil {
  25. v0, _, _ := sh.Version()
  26. log.Println("ipfs version", v0, "node", id.ID, "online")
  27. } else {
  28. sh = nil
  29. }
  30. }
  31. }
  32. func Shell() *MyShell {
  33. reset()
  34. return sh
  35. }
  36. func init() {
  37. reset()
  38. }