Merge pull request '[BREAKING] Déplacer route /v4 vers /v4/health' (#12) from breaking/v4-to-health into main

Reviewed-on: #12
This commit is contained in:
Victor Lacasse-Beaudoin 2023-06-02 02:20:12 -05:00
commit b6d3949a11
6 changed files with 61 additions and 36 deletions

View file

@ -49,7 +49,7 @@ var apiCmd = &cobra.Command{
// Routes
e.GET("/v4/", handlers.GetV4)
e.GET("/v4/health/", handlers.GetHealth)
e.POST("/v4/membres/", handlers.PostMembres)

View file

@ -9,6 +9,7 @@ import (
"net/http"
"git.agecem.com/agecem/bottin/v4/models"
"github.com/spf13/viper"
)
type ApiClient struct {
@ -18,6 +19,15 @@ type ApiClient struct {
Protocol string
}
func NewApiClientFromViper() *ApiClient {
apiClientKey := viper.GetString("web.api.key")
apiClientHost := viper.GetString("web.api.host")
apiClientProtocol := viper.GetString("web.api.protocol")
apiClientPort := viper.GetInt("web.api.port")
return NewApiClient(apiClientKey, apiClientHost, apiClientProtocol, apiClientPort)
}
func NewApiClient(key, host, protocol string, port int) *ApiClient {
return &ApiClient{
Key: key,

View file

@ -7,6 +7,7 @@ import (
"git.agecem.com/agecem/bottin/v4/models"
_ "github.com/jackc/pgx/stdlib"
"github.com/jmoiron/sqlx"
"github.com/spf13/viper"
)
// DataClient is a postgres client based on sqlx
@ -24,6 +25,19 @@ type PostgresConnection struct {
SSL bool
}
func NewDataClientFromViper() (*DataClient, error) {
client, err := NewDataClient(
PostgresConnection{
User: viper.GetString("db.user"),
Password: viper.GetString("db.password"),
Host: viper.GetString("db.host"),
Database: viper.GetString("db.database"),
Port: viper.GetInt("db.port"),
})
return client, err
}
func NewDataClient(connection PostgresConnection) (*DataClient, error) {
client := &DataClient{PostgresConnection: connection}

35
handlers/health.go Normal file
View file

@ -0,0 +1,35 @@
package handlers
import (
"fmt"
"net/http"
"git.agecem.com/agecem/bottin/v4/data"
"github.com/labstack/echo/v4"
)
type GetHealthResponse struct {
Message string `json:"message"`
}
func GetHealth(c echo.Context) error {
response := GetHealthResponse{
Message: "Bottin API v4 is ready",
}
dataClient, err := data.NewDataClientFromViper()
if err != nil {
response.Message = fmt.Sprintf("Error during data.NewDataClientFromViper(): %s", err)
return c.JSON(http.StatusInternalServerError, response)
}
defer dataClient.DB.Close()
if err = dataClient.DB.Ping(); err != nil {
response.Message = fmt.Sprintf("Error during dataClient.DB.Ping(): %s", err)
return c.JSON(http.StatusInternalServerError, response)
}
return c.JSON(http.StatusOK, response)
}

View file

@ -1,13 +0,0 @@
package handlers
import (
"net/http"
"github.com/labstack/echo/v4"
)
func GetV4(c echo.Context) error {
return c.JSON(http.StatusOK, map[string]string{
"message": "Bottin API v4 is ready",
})
}

View file

@ -6,7 +6,6 @@ import (
"git.agecem.com/agecem/bottin/v4/data"
"github.com/labstack/echo/v4"
"github.com/spf13/viper"
)
func GetIndex(c echo.Context) error {
@ -14,30 +13,10 @@ func GetIndex(c echo.Context) error {
}
func GetMembre(c echo.Context) error {
apiClientKey := viper.GetString("web.api.key")
apiClientHost := viper.GetString("web.api.host")
apiClientProtocol := viper.GetString("web.api.protocol")
apiClientPort := viper.GetInt("web.api.port")
/*
log.Printf(`
apiClientKey: %s
apiClientHost: %s
apiClientProtocol: %s
apiClientPort: %d`,
apiClientKey, apiClientHost, apiClientProtocol, apiClientPort,
)
*/
apiClient := data.NewApiClient(apiClientKey, apiClientHost, apiClientProtocol, apiClientPort)
apiClient := data.NewApiClientFromViper()
membreID := c.QueryParam("membre_id")
/*
// TODO
log.Printf("Requesting membreID: [%s]", membreID)
*/
membre, err := apiClient.GetMembre(membreID)
if err != nil {
return c.Render(http.StatusBadRequest, "index-html", struct {