cmd.go 1.38 KB
Newer Older
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 50 51 52 53
package api_server

import (
	"os"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/rs/zerolog/log"
	"github.com/spf13/cobra"
	"gitlab.com/tensorsecurity-rd/waf-console/api"
	"gitlab.com/tensorsecurity-rd/waf-console/cmd/api-server/config"

	// "gitlab.com/tensorsecurity-rd/waf-console/cmd/config"
	"gitlab.com/tensorsecurity-rd/waf-console/internal/service"
	es "gitlab.com/tensorsecurity-rd/waf-console/internal/store"
)

func NewApiServer() *gin.Engine {
	router := gin.Default()
	return router
}

func NewRootCommand() *cobra.Command {
	return &cobra.Command{
		Use:   "api-server",
		Short: "Start api-server service.",
		Args:  cobra.ExactArgs(0),
		RunE: func(cmd *cobra.Command, args []string) error {
			config := config.LoadConfig()
			debugMode := os.Getenv("DEBUG_MODE")
			log.Info().Msgf("DEBUG_MODE: %s", debugMode)
			if debugMode == "true" {
				config.Debug = true
				// config.Conf.Debug = true
			}
			esClient, err := es.CreateEsClientFromConfig(config.ElasticsearchConfig)
			if err != nil {
				panic(err)
			}

			e := api.SetApiRouters(config, config.SSOUrl, esClient)
			esStore := es.NewESStore(es.Config{
				ESBatchSize:   100,
				ESConcurrency: 10,
				ESTimeout:     10 * time.Second,
			}, esClient)
			esStore.Init()
			logConsumerService := service.NewLogConsumerService(nil, esStore, config.KafkaConfig)
			go logConsumerService.Consume()
			return e.Run(":8080")
		},
	}
}