| iomodo | c6abf5b | 2021-02-19 14:48:03 +0400 | [diff] [blame] | 1 | package commands |
| 2 | |
| 3 | import ( |
| 4 | "github.com/giolekva/pcloud/core/kg/log" |
| 5 | "github.com/giolekva/pcloud/core/kg/server" |
| 6 | "github.com/spf13/cobra" |
| 7 | ) |
| 8 | |
| 9 | // Command is an abstraction of the cobra Command |
| 10 | type Command = cobra.Command |
| 11 | |
| 12 | // Run function starts the application |
| 13 | func Run(args []string) error { |
| 14 | rootCmd.SetArgs(args) |
| 15 | return rootCmd.Execute() |
| 16 | } |
| 17 | |
| 18 | // rootCmd is a command to run the server. |
| iomodo | d32f9ee | 2021-02-21 21:28:50 +0400 | [diff] [blame] | 19 | var rootCmd = &Command{ |
| iomodo | c6abf5b | 2021-02-19 14:48:03 +0400 | [diff] [blame] | 20 | Use: "server", |
| 21 | Short: "An example of the basic server", |
| 22 | RunE: serverCmdF, |
| 23 | } |
| 24 | |
| 25 | func serverCmdF(command *cobra.Command, args []string) error { |
| 26 | config := &log.LoggerConfiguration{ |
| 27 | EnableConsole: true, |
| 28 | ConsoleJSON: true, |
| 29 | ConsoleLevel: "debug", |
| 30 | EnableFile: true, |
| 31 | FileJSON: true, |
| 32 | FileLevel: "debug", |
| 33 | FileLocation: "server.log", |
| 34 | } |
| 35 | logger := log.NewLogger(config) |
| 36 | srv, err := server.NewServer(logger) |
| 37 | if err != nil { |
| 38 | logger.Error(err.Error()) |
| 39 | return err |
| 40 | } |
| 41 | defer srv.Shutdown() |
| 42 | |
| 43 | serverErr := srv.Start() |
| 44 | if serverErr != nil { |
| 45 | logger.Error(err.Error()) |
| 46 | return serverErr |
| 47 | } |
| 48 | return nil |
| 49 | } |