Retirer package data/

Utiliser client minio-go directement dans les handlers.

Un package api/ pourra être ajouté pour avoir un client API de
agecem-org, mais dédoubler un minio-go#Client dans un object data#Client
est simplement redondant.
This commit is contained in:
Victor Lacasse-Beaudoin 2023-04-21 18:51:20 -04:00
parent 8f5d3794f4
commit d5b9b5202c
3 changed files with 13 additions and 55 deletions

View file

@ -10,8 +10,6 @@ ADD public/ public/
ADD cmd/ cmd/
ADD data/ data/
RUN CGO_ENABLED=0 go build -a -installsuffix cgo -o agecem-org .
# Alpine

View file

@ -4,6 +4,7 @@ Copyright © 2023 AGECEM
package cmd
import (
"context"
"fmt"
"embed"
@ -12,10 +13,11 @@ import (
"net/http"
"sort"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"git.agecem.com/agecem/agecem-org/data"
"git.agecem.com/agecem/agecem-org/public"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
@ -136,17 +138,23 @@ func handleV1Bucket(c echo.Context) error {
documents_secret_access_key := viper.GetString("server.documents.secret_access_key")
documents_use_ssl := viper.GetBool("server.documents.use_ssl")
client, err := data.NewClient(documents_buckets, documents_endpoint, documents_access_key_id, documents_secret_access_key, documents_use_ssl)
// Initialize minio client object
client, err := minio.New(documents_endpoint, &minio.Options{
Creds: credentials.NewStaticV4(documents_access_key_id, documents_secret_access_key, ""),
Secure: documents_use_ssl,
})
if err != nil {
return c.JSON(http.StatusInternalServerError, err)
return c.JSON(http.StatusInternalServerError, map[string]string{
"message": "Error during minio#New",
})
}
var buckets []string
for _, bucket_name := range documents_buckets {
exists, err := client.BucketExists(bucket_name)
exists, err := client.BucketExists(context.Background(), bucket_name)
if err != nil {
return c.JSON(http.StatusInternalServerError, err)
return c.JSON(http.StatusInternalServerError, "Error during minio#BucketExists")
}
if exists {

View file

@ -1,48 +0,0 @@
// Package data provides database interactions to the app
package data
import (
"context"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
type Client struct {
Endpoint, AccessKeyID, SecretAccessKey string
UseSSL bool
MinioClient *minio.Client
Context context.Context
}
func NewClient(buckets []string, endpoint, accessKeyID, secretAccessKey string, useSSL bool) (*Client, error) {
c := Client{
Endpoint: endpoint,
AccessKeyID: accessKeyID,
SecretAccessKey: secretAccessKey,
UseSSL: useSSL,
Context: context.Background(),
}
// Initialize minio client object
minio_client, err := minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
Secure: useSSL,
})
if err != nil {
return &c, err
}
c.MinioClient = minio_client
return &c, nil
}
func (c *Client) BucketExists(bucketName string) (bool, error) {
result, err := c.MinioClient.BucketExists(c.Context, bucketName)
if err != nil {
return false, err
}
return result, nil
}