From cdd526a6f3e17cbd1fd366ce9cb95ecfd40202ec Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Thu, 6 Jun 2024 17:59:58 -0400 Subject: [PATCH] wip: make apiCmd run and remove db test --- client.go | 1 + client_test.go | 1 + cmd.go | 90 ++++++++++++++++++++++++-------------------------- db_test.go | 49 --------------------------- 4 files changed, 46 insertions(+), 95 deletions(-) create mode 100644 client.go create mode 100644 client_test.go delete mode 100644 db_test.go diff --git a/client.go b/client.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/client.go @@ -0,0 +1 @@ +package main diff --git a/client_test.go b/client_test.go new file mode 100644 index 0000000..0fee6f5 --- /dev/null +++ b/client_test.go @@ -0,0 +1 @@ +package main_test diff --git a/cmd.go b/cmd.go index 8800c3d..3e37028 100644 --- a/cmd.go +++ b/cmd.go @@ -1,6 +1,7 @@ package main import ( + "context" "crypto/subtle" "embed" "fmt" @@ -10,25 +11,23 @@ import ( "os" "strings" + "github.com/jackc/pgx/v5/pgxpool" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" "github.com/spf13/cobra" "github.com/spf13/viper" ) -var ( - apiPort int - apiKey string -) - // apiCmd represents the api command var apiCmd = &cobra.Command{ Use: "api", Short: "Démarrer le serveur API", Args: cobra.ExactArgs(0), Run: func(cmd *cobra.Command, args []string) { - apiKey = viper.GetString(ViperAPIKey) - apiPort = viper.GetInt(ViperAPIPort) + var cfg Config + if err := viper.Unmarshal(&cfg); err != nil { + log.Fatal("parse config:", err) + } e := echo.New() @@ -36,30 +35,43 @@ var apiCmd = &cobra.Command{ e.Pre(middleware.AddTrailingSlash()) - if apiKey != "" { + if cfg.API.Key != "" { e.Use(middleware.KeyAuth(func(key string, c echo.Context) (bool, error) { - return subtle.ConstantTimeCompare([]byte(key), []byte(apiKey)) == 1, nil + return subtle.ConstantTimeCompare([]byte(key), []byte(cfg.API.Key)) == 1, nil })) } // DataClient - /* - client, err := data.NewDataClientFromViper() - if err != nil { - log.Fatalf("Could not establish database connection.\n Error: %s\n", err) - } - defer client.DB.Close() + ctx := context.Background() - err = client.DB.Ping() - if err != nil { - log.Fatalf("Database was supposed to be ready but Ping() failed.\n Error: %s\n", err) - } + //prep + pool, err := pgxpool.New( + ctx, + fmt.Sprintf( + "user=%s password=%s database=%s host=%s port=%d sslmode=%s ", + cfg.DB.User, + cfg.DB.Password, + cfg.DB.Database, + cfg.DB.Host, + cfg.DB.Port, + cfg.DB.SSLMode, + )) + if err != nil { + log.Fatal("init pgx pool:", err) + } + defer pool.Close() - _, err = client.Seed() - if err != nil { - log.Fatalf("Error during client.Seed(): %s", err) - } - */ + db := &PostgresClient{ + Ctx: ctx, + Pool: pool, + } + if err := db.Pool.Ping(ctx); err != nil { + log.Fatal("ping db:", err) + } + + if err := db.CreateOrReplaceSchema(); err != nil { + log.Fatal("create or replace schema:", err) + } // Routes /* @@ -82,7 +94,7 @@ var apiCmd = &cobra.Command{ // Execution - e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", apiPort))) + e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", cfg.API.Port))) }, } @@ -187,16 +199,6 @@ func initConfig() { } } -var ( - webUser string - webPassword string - webPort int - webApiHost string - webApiKey string - webApiPort int - webApiProtocol string -) - //go:embed templates/* var templatesFS embed.FS @@ -214,13 +216,10 @@ var webCmd = &cobra.Command{ Short: "Démarrer le client web", Args: cobra.ExactArgs(0), Run: func(cmd *cobra.Command, args []string) { - webApiHost = viper.GetString(ViperWebAPIHost) - webApiKey = viper.GetString(ViperWebAPIKey) - webApiPort = viper.GetInt(ViperWebAPIPort) - webApiProtocol = viper.GetString(ViperWebAPIProtocol) - webPassword = viper.GetString(ViperWebPassword) - webPort = viper.GetInt(ViperWebPort) - webUser = viper.GetString(ViperWebUser) + var cfg Config + if err := viper.Unmarshal(&cfg); err != nil { + log.Fatal("init config:", err) + } // Ping API server /* @@ -244,8 +243,8 @@ var webCmd = &cobra.Command{ e.Pre(middleware.AddTrailingSlash()) e.Use(middleware.BasicAuth(func(user, password string, c echo.Context) (bool, error) { - usersMatch := subtle.ConstantTimeCompare([]byte(user), []byte(webUser)) == 1 - passwordsMatch := subtle.ConstantTimeCompare([]byte(password), []byte(webPassword)) == 1 + usersMatch := subtle.ConstantTimeCompare([]byte(user), []byte(cfg.Web.User)) == 1 + passwordsMatch := subtle.ConstantTimeCompare([]byte(password), []byte(cfg.Web.Password)) == 1 return usersMatch && passwordsMatch, nil })) @@ -268,13 +267,12 @@ var webCmd = &cobra.Command{ // Execution e.Logger.Fatal(e.Start( - fmt.Sprintf(":%d", webPort))) + fmt.Sprintf(":%d", cfg.Web.Port))) }, } func init() { rootCmd.AddCommand(webCmd) - //templatesFS = web.GetTemplates() // web.api.host webCmd.Flags().String(FlagWebAPIHost, DefaultWebAPIHost, DescriptionWebAPIHost) diff --git a/db_test.go b/db_test.go deleted file mode 100644 index 4c3881a..0000000 --- a/db_test.go +++ /dev/null @@ -1,49 +0,0 @@ -package main - -import ( - "context" - "fmt" - "testing" - - "github.com/jackc/pgx/v5/pgxpool" - "github.com/spf13/viper" -) - -func TestDB(t *testing.T) { - cfg := DefaultConfig() - cfg.DB.Password = viper.GetString(ViperDBPassword) - - ctx := context.Background() - - //prep - pool, err := pgxpool.New( - ctx, - fmt.Sprintf( - "user=%s password=%s database=%s host=%s port=%d sslmode=%s ", - cfg.DB.User, - cfg.DB.Password, - cfg.DB.Database, - cfg.DB.Host, - cfg.DB.Port, - cfg.DB.SSLMode, - )) - if err != nil { - t.Error(err) - return - } - defer pool.Close() - - db := &PostgresClient{ - Ctx: ctx, - Pool: pool, - } - - //exec - - t.Run("create or replace schema", - func(t *testing.T) { - if err := db.CreateOrReplaceSchema(); err != nil { - t.Error(err) - } - }) -}