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/ diff --git a/apihandler/apihandler.go b/apihandler/apihandler.go index ae97e0f..1339e8e 100644 --- a/apihandler/apihandler.go +++ b/apihandler/apihandler.go @@ -5,8 +5,8 @@ import ( "fmt" "net/http" - "git.agecem.com/agecem/bottin-ag/apierror" "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" ) @@ -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, + } } /* @@ -27,6 +34,7 @@ routes */ type APIHandler struct { BottinAPIClient *bottindata.ApiClient + DBClient *dbclient.DBClient } // HealthGET is the handler for `GET /v:version/health/ http/1.1` @@ -42,18 +50,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 } } diff --git a/cmd/api.go b/cmd/api.go index 26f18ce..d0e5c5b 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" @@ -31,12 +32,18 @@ var apiCmd = &cobra.Command{ v0 := e.Group("/v0") - handler := apihandler.New() - if &handler == nil { - log.Fatal("Newly created APIHandler is nil") + bottinApiClient := bottindata.NewApiClient("bottin", "localhost", "http", 1312) + + dbClient, err := dbclient.New("db", "bottinag", "bottinag", "bottinag", 5432, false) + if err != nil { + log.Fatal(err) } - handler.BottinAPIClient = bottindata.NewApiClient("bottin", "localhost", "http", 1312) + handler := apihandler.New(bottinApiClient, dbClient) + + if err := handler.DBClient.CreateTablesIfNotExist(); err != nil { + log.Fatal(err) + } apihandler.DeclareRoutes(v0, &handler) diff --git a/dbclient/dbclient.go b/dbclient/dbclient.go index 61c4d69..5e763ea 100644 --- a/dbclient/dbclient.go +++ b/dbclient/dbclient.go @@ -1,2 +1,46 @@ // Package dbclient provides the database client used by the API server package dbclient + +import ( + "fmt" + + "git.agecem.com/agecem/bottin-ag/dbschema" + "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 +} + +func (d *DBClient) CreateTablesIfNotExist() error { + _, err := d.DB.Exec(dbschema.Schema) + return err +} 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 ); `