wip: make apiCmd run and remove db test

This commit is contained in:
Victor Lacasse-Beaudoin 2024-06-06 17:59:58 -04:00
parent 0123d9d37c
commit cdd526a6f3
4 changed files with 46 additions and 95 deletions

1
client.go Normal file
View file

@ -0,0 +1 @@
package main

1
client_test.go Normal file
View file

@ -0,0 +1 @@
package main_test

84
cmd.go
View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"context"
"crypto/subtle" "crypto/subtle"
"embed" "embed"
"fmt" "fmt"
@ -10,25 +11,23 @@ import (
"os" "os"
"strings" "strings"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware" "github.com/labstack/echo/v4/middleware"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
var (
apiPort int
apiKey string
)
// apiCmd represents the api command // apiCmd represents the api command
var apiCmd = &cobra.Command{ var apiCmd = &cobra.Command{
Use: "api", Use: "api",
Short: "Démarrer le serveur API", Short: "Démarrer le serveur API",
Args: cobra.ExactArgs(0), Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
apiKey = viper.GetString(ViperAPIKey) var cfg Config
apiPort = viper.GetInt(ViperAPIPort) if err := viper.Unmarshal(&cfg); err != nil {
log.Fatal("parse config:", err)
}
e := echo.New() e := echo.New()
@ -36,30 +35,43 @@ var apiCmd = &cobra.Command{
e.Pre(middleware.AddTrailingSlash()) e.Pre(middleware.AddTrailingSlash())
if apiKey != "" { if cfg.API.Key != "" {
e.Use(middleware.KeyAuth(func(key string, c echo.Context) (bool, error) { 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 // DataClient
/* ctx := context.Background()
client, err := data.NewDataClientFromViper()
if err != nil {
log.Fatalf("Could not establish database connection.\n Error: %s\n", err)
}
defer client.DB.Close()
err = client.DB.Ping() //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 { if err != nil {
log.Fatalf("Database was supposed to be ready but Ping() failed.\n Error: %s\n", err) log.Fatal("init pgx pool:", err)
}
defer pool.Close()
db := &PostgresClient{
Ctx: ctx,
Pool: pool,
}
if err := db.Pool.Ping(ctx); err != nil {
log.Fatal("ping db:", err)
} }
_, err = client.Seed() if err := db.CreateOrReplaceSchema(); err != nil {
if err != nil { log.Fatal("create or replace schema:", err)
log.Fatalf("Error during client.Seed(): %s", err)
} }
*/
// Routes // Routes
/* /*
@ -82,7 +94,7 @@ var apiCmd = &cobra.Command{
// Execution // 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/* //go:embed templates/*
var templatesFS embed.FS var templatesFS embed.FS
@ -214,13 +216,10 @@ var webCmd = &cobra.Command{
Short: "Démarrer le client web", Short: "Démarrer le client web",
Args: cobra.ExactArgs(0), Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
webApiHost = viper.GetString(ViperWebAPIHost) var cfg Config
webApiKey = viper.GetString(ViperWebAPIKey) if err := viper.Unmarshal(&cfg); err != nil {
webApiPort = viper.GetInt(ViperWebAPIPort) log.Fatal("init config:", err)
webApiProtocol = viper.GetString(ViperWebAPIProtocol) }
webPassword = viper.GetString(ViperWebPassword)
webPort = viper.GetInt(ViperWebPort)
webUser = viper.GetString(ViperWebUser)
// Ping API server // Ping API server
/* /*
@ -244,8 +243,8 @@ var webCmd = &cobra.Command{
e.Pre(middleware.AddTrailingSlash()) e.Pre(middleware.AddTrailingSlash())
e.Use(middleware.BasicAuth(func(user, password string, c echo.Context) (bool, error) { e.Use(middleware.BasicAuth(func(user, password string, c echo.Context) (bool, error) {
usersMatch := subtle.ConstantTimeCompare([]byte(user), []byte(webUser)) == 1 usersMatch := subtle.ConstantTimeCompare([]byte(user), []byte(cfg.Web.User)) == 1
passwordsMatch := subtle.ConstantTimeCompare([]byte(password), []byte(webPassword)) == 1 passwordsMatch := subtle.ConstantTimeCompare([]byte(password), []byte(cfg.Web.Password)) == 1
return usersMatch && passwordsMatch, nil return usersMatch && passwordsMatch, nil
})) }))
@ -268,13 +267,12 @@ var webCmd = &cobra.Command{
// Execution // Execution
e.Logger.Fatal(e.Start( e.Logger.Fatal(e.Start(
fmt.Sprintf(":%d", webPort))) fmt.Sprintf(":%d", cfg.Web.Port)))
}, },
} }
func init() { func init() {
rootCmd.AddCommand(webCmd) rootCmd.AddCommand(webCmd)
//templatesFS = web.GetTemplates()
// web.api.host // web.api.host
webCmd.Flags().String(FlagWebAPIHost, DefaultWebAPIHost, DescriptionWebAPIHost) webCmd.Flags().String(FlagWebAPIHost, DefaultWebAPIHost, DescriptionWebAPIHost)

View file

@ -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)
}
})
}