summaryrefslogtreecommitdiff
path: root/cmd/api/api.go
blob: 7c315933248ab9c35e182e4902aceff88f10b599 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package api

import (
	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
	"github.com/urfave/cli/v2"
	"gitrepo.ru/neonxp/idecnode/pkg/apiv1"
	"gitrepo.ru/neonxp/idecnode/pkg/apiv2"
	"gitrepo.ru/neonxp/idecnode/pkg/config"
	"gitrepo.ru/neonxp/idecnode/pkg/idec"
)

var APICommand *cli.Command = &cli.Command{
	Name:        "api",
	Description: "Start api server",
	Action: func(c *cli.Context) error {
		configPath := c.String("config")
		cfg, err := config.New(configPath)
		if err != nil {
			return err
		}

		idecApi, err := idec.New(cfg)
		if err != nil {
			return err
		}
		defer idecApi.Close()
		e := echo.New()

		e.Use(
			middleware.Recover(),
			middleware.Logger(),
		)
		apiv1.New(idecApi, cfg).Register(e)

		apiv2.New(idecApi, cfg).Register(e)

		e.Static("/", "./web/dist")

		return e.Start(cfg.Listen)
	},
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:        "config",
			DefaultText: "config path",
			Value:       "./etc/node.yaml",
		},
	},
}