api.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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/node"
  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. To listen to a socket, prefix with unix://, e.g. unix:///socket.path",
  35. },
  36. ),
  37. Action: func(c *cli.Context) error {
  38. o, _, ll := cliToOpts(c)
  39. e, err := node.New(o...)
  40. if err != nil {
  41. return err
  42. }
  43. displayStart(ll)
  44. ctx := context.Background()
  45. // Start the node to the network, using our ledger
  46. if err := e.Start(ctx); err != nil {
  47. return err
  48. }
  49. return api.API(ctx, c.String("listen"), 5*time.Second, 20*time.Second, e)
  50. },
  51. }
  52. }