From 76a3b9b10550d333c0050b9410933146251218f6 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sat, 16 Sep 2023 16:28:18 -0400 Subject: [PATCH 1/8] Ajouter type dbclient.DBClient et func dbclient.New --- dbclient/dbclient.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/dbclient/dbclient.go b/dbclient/dbclient.go index 61c4d69..85049ea 100644 --- a/dbclient/dbclient.go +++ b/dbclient/dbclient.go @@ -1,2 +1,40 @@ // Package dbclient provides the database client used by the API server package dbclient + +import ( + "fmt" + + "github.com/jmoiron/sqlx" +) + +// DBClient holds a *sqlx.DB and implements methods to manipulate the database +type DBClient struct { + DB *sqlx.DB +} + +func New(host, database, user, password string, port int, useSSL bool) (*DBClient, error) { + var SSLMode string + + switch useSSL { + case true: + SSLMode = "enable" + case false: + SSLMode = "disable" + } + + connectionString := fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s", + user, + password, + host, + port, + database, + SSLMode, + ) + + db, err := sqlx.Connect("pgx", connectionString) + if err != nil { + return nil, err + } + + return &DBClient{DB: db}, nil +} From 2af75e1ecf9b8a8fa98f082b760ff31f50ac9904 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sat, 16 Sep 2023 17:08:56 -0400 Subject: [PATCH 2/8] =?UTF-8?q?Ajouter=20dbschema/=20et=20dbstruct/=20?= =?UTF-8?q?=C3=A0=20build=20step?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Dockerfile b/Dockerfile index 3e11ae5..499b2a3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,6 +13,8 @@ ADD apiresponse/ apiresponse/ ADD cmd/ cmd/ ADD config/ config/ ADD dbclient/ dbclient/ +ADD dbschema/ dbschema/ +ADD dbstruct/ dbstruct/ ADD webcontent/ webcontent/ ADD webhandler/ webhandler/ From 20fa0e10d1fbfb29750ee525f781caf1b9155d8d Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sat, 16 Sep 2023 17:10:52 -0400 Subject: [PATCH 3/8] Fix schema MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PRIMARY KEY est toujours UNIQUE et NOT NULL, alors NOT NULL est redondant Assigner type VARCHAR(6) à presences.programme_id --- dbschema/dbschema.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dbschema/dbschema.go b/dbschema/dbschema.go index e18b72d..b1c6030 100644 --- a/dbschema/dbschema.go +++ b/dbschema/dbschema.go @@ -3,9 +3,9 @@ package dbschema const Schema string = ` CREATE TABLE IF NOT EXISTS presences ( - id VARCHAR(7) PRIMARY KEY NOT NULL, + id VARCHAR(7) PRIMARY KEY, at TIMESTAMP NOT NULL, name VARCHAR(50) NOT NULL, - programme_id NOT NULL + programme_id VARCHAR(6) NOT NULL ); ` From c583220fd66de0adc031a81c01ff9df527d5868b Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sat, 16 Sep 2023 17:13:51 -0400 Subject: [PATCH 4/8] Ajouter dbschema.DBClient#CreateTablesIfNotExist() --- dbclient/dbclient.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/dbclient/dbclient.go b/dbclient/dbclient.go index 85049ea..5e763ea 100644 --- a/dbclient/dbclient.go +++ b/dbclient/dbclient.go @@ -4,6 +4,7 @@ package dbclient import ( "fmt" + "git.agecem.com/agecem/bottin-ag/dbschema" "github.com/jmoiron/sqlx" ) @@ -38,3 +39,8 @@ func New(host, database, user, password string, port int, useSSL bool) (*DBClien return &DBClient{DB: db}, nil } + +func (d *DBClient) CreateTablesIfNotExist() error { + _, err := d.DB.Exec(dbschema.Schema) + return err +} From 622efd8d637a8dd3301adb8b265c5223fbdc5e81 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sat, 16 Sep 2023 17:19:32 -0400 Subject: [PATCH 5/8] Retirer field Data.DBStatus de apiresponse.HealthGET --- apihandler/apihandler.go | 13 ------------- apiresponse/apiresponse.go | 1 - 2 files changed, 14 deletions(-) diff --git a/apihandler/apihandler.go b/apihandler/apihandler.go index ae97e0f..e051ce1 100644 --- a/apihandler/apihandler.go +++ b/apihandler/apihandler.go @@ -5,7 +5,6 @@ import ( "fmt" "net/http" - "git.agecem.com/agecem/bottin-ag/apierror" "git.agecem.com/agecem/bottin-ag/apiresponse" bottindata "git.agecem.com/agecem/bottin/v5/data" "github.com/labstack/echo/v4" @@ -42,18 +41,6 @@ func (a *APIHandler) HealthGET(c echo.Context) error { r.Data.BottinStatus = bottinStatus } - // TODO - r.Data.DBStatus = "not implemented" - - if r.Data.BottinStatus == "not implemented" || r.Data.DBStatus == "not implemented" { - var err apierror.ErrBottinOrDBNotImplemented - r.Error = err.Error() - r.StatusCode = http.StatusInternalServerError - r.Message = "not ok" - - return c.JSON(r.StatusCode, r) - } - return c.JSON(r.StatusCode, r) } diff --git a/apiresponse/apiresponse.go b/apiresponse/apiresponse.go index cdeb9c0..d034916 100644 --- a/apiresponse/apiresponse.go +++ b/apiresponse/apiresponse.go @@ -13,7 +13,6 @@ type HealthGET struct { Response Data struct { BottinStatus string - DBStatus string } } From a88809f91e83f3fd331bb4036a9f1d520109043c Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sat, 16 Sep 2023 17:21:18 -0400 Subject: [PATCH 6/8] =?UTF-8?q?Retirer=20validation=20probl=C3=A9matique?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Si le handler était nil, la vérification serait probablement assez pour faire panic l'application, ce qui serait un problème que la validation soit présente ou non. --- cmd/api.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/cmd/api.go b/cmd/api.go index 26f18ce..71e1304 100644 --- a/cmd/api.go +++ b/cmd/api.go @@ -32,9 +32,6 @@ var apiCmd = &cobra.Command{ v0 := e.Group("/v0") handler := apihandler.New() - if &handler == nil { - log.Fatal("Newly created APIHandler is nil") - } handler.BottinAPIClient = bottindata.NewApiClient("bottin", "localhost", "http", 1312) From 682d8fc6aae5e6667b1943b1b284b70ddfc0b4a5 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sat, 16 Sep 2023 17:22:12 -0400 Subject: [PATCH 7/8] =?UTF-8?q?Cr=C3=A9er=20tables=20sur=20d=C3=A9marrage?= =?UTF-8?q?=20de=20apiCmd?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apihandler/apihandler.go | 2 ++ cmd/api.go | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/apihandler/apihandler.go b/apihandler/apihandler.go index e051ce1..8c4bd8e 100644 --- a/apihandler/apihandler.go +++ b/apihandler/apihandler.go @@ -6,6 +6,7 @@ import ( "net/http" "git.agecem.com/agecem/bottin-ag/apiresponse" + "git.agecem.com/agecem/bottin-ag/dbclient" bottindata "git.agecem.com/agecem/bottin/v5/data" "github.com/labstack/echo/v4" ) @@ -26,6 +27,7 @@ routes */ type APIHandler struct { BottinAPIClient *bottindata.ApiClient + DBClient *dbclient.DBClient } // HealthGET is the handler for `GET /v:version/health/ http/1.1` diff --git a/cmd/api.go b/cmd/api.go index 71e1304..c55e4b2 100644 --- a/cmd/api.go +++ b/cmd/api.go @@ -9,6 +9,7 @@ import ( "git.agecem.com/agecem/bottin-ag/apihandler" "git.agecem.com/agecem/bottin-ag/config" + "git.agecem.com/agecem/bottin-ag/dbclient" bottindata "git.agecem.com/agecem/bottin/v5/data" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" @@ -35,6 +36,17 @@ var apiCmd = &cobra.Command{ handler.BottinAPIClient = bottindata.NewApiClient("bottin", "localhost", "http", 1312) + dbClient, err := dbclient.New("db", "bottinag", "bottinag", "bottinag", 5432, false) + if err != nil { + log.Fatal(err) + } + + handler.DBClient = dbClient + + if err := handler.DBClient.CreateTablesIfNotExist(); err != nil { + log.Fatal(err) + } + apihandler.DeclareRoutes(v0, &handler) e.Start(fmt.Sprintf(":%d", cfg.API.Port)) From 61237996509487ac7f20eb77b3bac43ef4d6bbb3 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sat, 16 Sep 2023 17:44:38 -0400 Subject: [PATCH 8/8] =?UTF-8?q?Ajouter=20pointers=20=C3=A0=20clients=20en?= =?UTF-8?q?=20param=C3=A8tres=20=C3=A0=20apihandler.New?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apihandler/apihandler.go | 11 +++++++++-- cmd/api.go | 6 ++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/apihandler/apihandler.go b/apihandler/apihandler.go index 8c4bd8e..1339e8e 100644 --- a/apihandler/apihandler.go +++ b/apihandler/apihandler.go @@ -17,8 +17,15 @@ func DeclareRoutes(e *echo.Group, h *APIHandler) { e.GET("/scan/:membre_id/", h.ScanGET) } -func New() (handler APIHandler) { - return +/* +New retourne un nouveau APIHandler contenant des pointers vers les clients +nécessaires à l'exécution du API server +*/ +func New(bottinAPIClient *bottindata.ApiClient, dbClient *dbclient.DBClient) APIHandler { + return APIHandler{ + BottinAPIClient: bottinAPIClient, + DBClient: dbClient, + } } /* diff --git a/cmd/api.go b/cmd/api.go index c55e4b2..d0e5c5b 100644 --- a/cmd/api.go +++ b/cmd/api.go @@ -32,16 +32,14 @@ var apiCmd = &cobra.Command{ v0 := e.Group("/v0") - handler := apihandler.New() - - handler.BottinAPIClient = bottindata.NewApiClient("bottin", "localhost", "http", 1312) + bottinApiClient := bottindata.NewApiClient("bottin", "localhost", "http", 1312) dbClient, err := dbclient.New("db", "bottinag", "bottinag", "bottinag", 5432, false) if err != nil { log.Fatal(err) } - handler.DBClient = dbClient + handler := apihandler.New(bottinApiClient, dbClient) if err := handler.DBClient.CreateTablesIfNotExist(); err != nil { log.Fatal(err)