wip: make apiCmd run and remove db test
This commit is contained in:
parent
0123d9d37c
commit
cdd526a6f3
4 changed files with 46 additions and 95 deletions
1
client.go
Normal file
1
client.go
Normal file
|
@ -0,0 +1 @@
|
|||
package main
|
1
client_test.go
Normal file
1
client_test.go
Normal file
|
@ -0,0 +1 @@
|
|||
package main_test
|
90
cmd.go
90
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)
|
||||
|
|
49
db_test.go
49
db_test.go
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
Loading…
Reference in a new issue