refactor: enhancement of logging, config and metrics

This commit is contained in:
Jonas Kaninda
2024-11-11 08:50:34 +01:00
parent e25bc218b5
commit 11c72e5e17
12 changed files with 97 additions and 65 deletions

View File

@@ -18,9 +18,10 @@
package config
import (
"fmt"
pkg "github.com/jkaninda/goma-gateway/internal"
"github.com/spf13/cobra"
"log"
"os"
)
var CheckConfigCmd = &cobra.Command{
@@ -29,13 +30,15 @@ var CheckConfigCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
configFile, _ := cmd.Flags().GetString("config")
if configFile == "" {
log.Fatalln("no config file specified")
fmt.Println("no config file specified")
os.Exit(1)
}
err := pkg.CheckConfig(configFile)
if err != nil {
log.Fatalf(" Error checking config file: %s\n", err)
fmt.Printf(" Error checking config file: %s\n", err)
os.Exit(1)
}
log.Println("Goma Gateway configuration file checked successfully")
fmt.Println("Goma Gateway configuration file checked successfully")
},
}

View File

@@ -17,8 +17,9 @@ limitations under the License.
package config
import (
"fmt"
"github.com/spf13/cobra"
"log"
"os"
)
var Cmd = &cobra.Command{
@@ -28,8 +29,8 @@ var Cmd = &cobra.Command{
if len(args) == 0 {
return
} else {
log.Fatalf("Config accepts no argument %q", args)
fmt.Printf("config accepts no argument %q\n", args)
os.Exit(1)
}
},

View File

@@ -16,9 +16,10 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"fmt"
"github.com/jkaninda/goma-gateway/internal"
"github.com/jkaninda/goma-gateway/pkg/logger"
"github.com/spf13/cobra"
"os"
)
var InitConfigCmd = &cobra.Command{
@@ -26,14 +27,34 @@ var InitConfigCmd = &cobra.Command{
Short: "Initialize Goma Gateway configuration file",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
pkg.InitConfig(cmd)
force, _ := cmd.Flags().GetBool("force")
configFile, _ := cmd.Flags().GetString("output")
if configFile == "" {
fmt.Println("Error: no config file specified")
os.Exit(1)
}
// Check if the config file exists
if _, err := os.Stat(configFile); !os.IsNotExist(err) {
if !force {
fmt.Printf("%s config file already exists, use -f to overwrite\n", configFile)
os.Exit(1)
}
}
err := pkg.InitConfig(configFile)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("configuration file has been initialized successfully")
} else {
logger.Fatal(`"config" accepts no argument %q`, args)
fmt.Printf("config accepts no argument %q\n", args)
os.Exit(1)
}
},
}
func init() {
InitConfigCmd.Flags().StringP("output", "o", "", "config file output")
InitConfigCmd.Flags().StringP("output", "o", "", "configuration file output")
InitConfigCmd.Flags().BoolP("force", "f", false, "Force overwrite configuration file")
}

View File

@@ -17,10 +17,11 @@ limitations under the License.
package cmd
import (
"fmt"
"github.com/jkaninda/goma-gateway/cmd/config"
"github.com/jkaninda/goma-gateway/pkg/logger"
"github.com/jkaninda/goma-gateway/util"
"github.com/spf13/cobra"
"os"
)
// rootCmd represents
@@ -29,7 +30,7 @@ var rootCmd = &cobra.Command{
Short: "Goma Gateway is a lightweight API Gateway Management",
Long: `.`,
Example: util.MainExample,
Version: util.FullVersion(),
Version: util.Version,
}
// Execute adds all child commands to the root command and sets flags appropriately.
@@ -37,7 +38,8 @@ var rootCmd = &cobra.Command{
func Execute() {
err := rootCmd.Execute()
if err != nil {
logger.Fatal("Error executing root command %v", err)
fmt.Printf("Error executing root command %v\n", err)
os.Exit(1)
}
}
func init() {

View File

@@ -21,9 +21,9 @@ import (
"fmt"
"github.com/common-nighthawk/go-figure"
"github.com/jkaninda/goma-gateway/internal"
"github.com/jkaninda/goma-gateway/pkg/logger"
"github.com/jkaninda/goma-gateway/util"
"github.com/spf13/cobra"
"os"
)
var ServerCmd = &cobra.Command{
@@ -39,11 +39,13 @@ var ServerCmd = &cobra.Command{
g := pkg.GatewayServer{}
gs, err := g.Config(configFile)
if err != nil {
logger.Fatal("Could not load configuration: %v", err)
fmt.Printf("Could not load configuration: %v\n", err)
os.Exit(1)
}
gs.SetEnv()
if err := gs.Start(ctx); err != nil {
logger.Fatal("Could not start server: %v", err)
fmt.Printf("Could not start server: %v\n", err)
os.Exit(1)
}
@@ -56,7 +58,7 @@ func init() {
func intro() {
nameFigure := figure.NewFigure("Goma", "", true)
nameFigure.Print()
fmt.Printf("Version: %s\n", util.FullVersion())
fmt.Printf("Version: %s\n", util.Version)
fmt.Println("Copyright (c) 2024 Jonas Kaninda")
fmt.Println("Starting Goma Gateway server...")
}

View File

@@ -18,7 +18,6 @@
package cmd
import (
"fmt"
"github.com/jkaninda/goma-gateway/util"
"github.com/spf13/cobra"
)
@@ -32,5 +31,5 @@ var VersionCmd = &cobra.Command{
}
func version() {
fmt.Println("Version:\t", util.FullVersion())
util.FullVersion()
}