github.com/weplanx/server@v0.2.6-0.20240318110640-f7e75155779a/main.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"github.com/spf13/cobra"
     7  	"github.com/weplanx/server/bootstrap"
     8  	"github.com/weplanx/server/cmd"
     9  	"github.com/weplanx/server/common"
    10  	"os"
    11  )
    12  
    13  func main() {
    14  	var config string
    15  	root := &cobra.Command{
    16  		Use:               "weplanx",
    17  		Short:             "API service, based on Hertz's project",
    18  		CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true},
    19  		Args: func(cmd *cobra.Command, args []string) error {
    20  			if len(args) < 1 {
    21  				return cmd.Help()
    22  			}
    23  			return nil
    24  		},
    25  		PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) {
    26  			ctx := context.Background()
    27  			var v *common.Values
    28  			if v, err = bootstrap.LoadStaticValues(config); err != nil {
    29  				return
    30  			}
    31  			cmd.SetContext(context.WithValue(ctx, "values", v))
    32  			return
    33  		},
    34  	}
    35  	root.PersistentFlags().StringVarP(&config,
    36  		"config", "c", "config/default.values.yml",
    37  		"The default configuration file of weplanx server values",
    38  	)
    39  	root.AddCommand(cmd.API)
    40  	root.AddCommand(cmd.XAPI)
    41  	root.AddCommand(cmd.OpenAPI)
    42  	root.AddCommand(cmd.Setup)
    43  	root.AddCommand(cmd.Sync)
    44  	root.AddCommand(cmd.User())
    45  	root.AddCommand(cmd.Values)
    46  	if err := root.Execute(); err != nil {
    47  		fmt.Println(err)
    48  		os.Exit(1)
    49  	}
    50  }