api.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright © 2021 Ettore Di Giacinto <[email protected]>
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program; if not, see <http://www.gnu.org/licenses/>.
  15. package cmd
  16. import (
  17. "context"
  18. "time"
  19. "github.com/libp2p/go-libp2p"
  20. "github.com/libp2p/go-libp2p-core/metrics"
  21. "github.com/mudler/edgevpn/api"
  22. "github.com/mudler/edgevpn/pkg/node"
  23. "github.com/urfave/cli"
  24. )
  25. func API() cli.Command {
  26. return cli.Command{
  27. Name: "api",
  28. Usage: "Starts an http server to display network informations",
  29. Description: `Start listening locally, providing an API for the network.
  30. A simple UI interface is available to display network data.`,
  31. UsageText: "edgevpn api",
  32. Flags: append(CommonFlags,
  33. &cli.BoolFlag{
  34. Name: "debug",
  35. },
  36. &cli.StringFlag{
  37. Name: "listen",
  38. Value: ":8080",
  39. Usage: "Listening address. To listen to a socket, prefix with unix://, e.g. unix:///socket.path",
  40. },
  41. ),
  42. Action: func(c *cli.Context) error {
  43. o, _, ll := cliToOpts(c)
  44. bwc := metrics.NewBandwidthCounter()
  45. o = append(o, node.WithLibp2pAdditionalOptions(libp2p.BandwidthReporter(bwc)))
  46. e, err := node.New(o...)
  47. if err != nil {
  48. return err
  49. }
  50. displayStart(ll)
  51. ctx := context.Background()
  52. // Start the node to the network, using our ledger
  53. if err := e.Start(ctx); err != nil {
  54. return err
  55. }
  56. return api.API(ctx, c.String("listen"), 5*time.Second, 20*time.Second, e, bwc, c.Bool("debug"))
  57. },
  58. }
  59. }