api.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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/mudler/edgevpn/api"
  20. "github.com/mudler/edgevpn/pkg/edgevpn"
  21. "github.com/urfave/cli"
  22. )
  23. func API() cli.Command {
  24. return cli.Command{
  25. Name: "api",
  26. Usage: "Starts an http server to display network informations",
  27. Description: `Start listening locally, providing an API for the network.
  28. A simple UI interface is available to display network data.`,
  29. UsageText: "edgevpn api",
  30. Flags: append(CommonFlags,
  31. &cli.StringFlag{
  32. Name: "listen",
  33. Value: ":8080",
  34. Usage: "Listening address",
  35. },
  36. ),
  37. Action: func(c *cli.Context) error {
  38. e := edgevpn.New(cliToOpts(c)...)
  39. displayStart(e)
  40. // Join the node to the network, using our ledger
  41. if err := e.Join(context.Background()); err != nil {
  42. return err
  43. }
  44. ledger, _ := e.Ledger()
  45. return api.API(c.String("listen"), 5*time.Second, 20*time.Second, ledger)
  46. },
  47. }
  48. }