Merge branch 'feature/dbclient' into main
This commit is contained in:
commit
54b79d7db3
6 changed files with 70 additions and 22 deletions
|
@ -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/
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -13,7 +13,6 @@ type HealthGET struct {
|
|||
Response
|
||||
Data struct {
|
||||
BottinStatus string
|
||||
DBStatus string
|
||||
}
|
||||
}
|
||||
|
||||
|
|
15
cmd/api.go
15
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)
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
);
|
||||
`
|
||||
|
|
Loading…
Reference in a new issue