Merge pull request '[BREAKING] Changer Config.Server.Buckets à un map[string]string' (#98) from feature/config-bucket-displayname into main

Reviewed-on: #98
This commit is contained in:
Victor Lacasse-Beaudoin 2023-08-15 15:02:09 -05:00
commit 7b4a75c93b
6 changed files with 35 additions and 25 deletions

View file

@ -96,7 +96,12 @@ func init() {
viper.BindPFlag("server.documents.use_ssl", serverCmd.Flags().Lookup("server-documents-use-ssl"))
// server.documents.buckets - --server-documents-buckets
serverCmd.Flags().StringSlice("server-documents-buckets", []string{"proces-verbaux", "politiques-et-reglements"}, "Buckets that are allowed to be accessed by the API (config: server.documents.buckets)")
serverCmd.Flags().StringToString("server-documents-buckets", map[string]string{
"proces-verbaux": "Procès-verbaux",
"politiques": "Politiques",
"reglements": "Règlements",
"formulaires": "Formulaires",
}, "Buckets that are allowed to be accessed by the API (config: server.documents.buckets)")
viper.BindPFlag("server.documents.buckets", serverCmd.Flags().Lookup("server-documents-buckets"))
// server.api.auth - --server-api-auth
@ -293,7 +298,7 @@ func handleDocumentation(c echo.Context) error {
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
}
var buckets []string
var buckets map[string]string
err = json.Unmarshal(result, &buckets)
if err != nil {
@ -302,12 +307,13 @@ func handleDocumentation(c echo.Context) error {
type Bucket struct {
Name string
DisplayName string
Documents []string
}
var data []Bucket
for _, bucket := range buckets {
for bucket, displayName := range buckets {
content, err := client.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s", bucket))
if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
@ -340,6 +346,7 @@ func handleDocumentation(c echo.Context) error {
data = append(data, Bucket{
Name: bucket,
DisplayName: displayName,
Documents: documents,
})
}

View file

@ -29,7 +29,7 @@ type Config struct {
} `mapstructure:"api"`
Documents struct {
AccessKeyId string `mapstructure:"access_key_id"`
Buckets []string `mapstructure:"buckets"`
Buckets map[string]string `mapstructure:"buckets"`
Endpoint string `mapstructure:"endpoint"`
SecretAccessKey string `mapstructure:"secret_access_key"`
UseSSL bool `mapstructure:"use_ssl"`

View file

@ -35,8 +35,10 @@ server:
#
# Also used to specify which buckets are to be created on receiving a POST request on /v1/seed
buckets:
- 'proces-verbaux'
- 'politiques-et-reglements'
'proces-verbaux': 'Procès-verbaux'
'politiques': 'Politiques'
'reglements': 'Règlements'
'formulaires': 'Formulaires'
api:
# Enable or disable key auth on /v1 routes

View file

@ -58,7 +58,7 @@ func (m *MediaClient) Seed() ([]string, error) {
var new_buckets []string
for _, bucket := range cfg.Server.Documents.Buckets {
for bucket := range cfg.Server.Documents.Buckets {
exists, err := m.MinioClient.BucketExists(context.Background(), bucket)
if err != nil {
return new_buckets, err

View file

@ -72,16 +72,16 @@ func HandleV1BucketList(c echo.Context) error {
})
}
var buckets []string
var buckets = make(map[string]string)
for _, bucket_name := range cfg.Server.Documents.Buckets {
for bucket_name, bucket_display_name := range cfg.Server.Documents.Buckets {
exists, err := mediaClient.MinioClient.BucketExists(context.Background(), bucket_name)
if err != nil {
return c.JSON(http.StatusInternalServerError, "Error during minio#BucketExists")
}
if exists {
buckets = append(buckets, bucket_name)
buckets[bucket_name] = bucket_display_name
}
}
@ -99,7 +99,7 @@ func HandleV1BucketRead(c echo.Context) error {
bucket := c.Param("bucket")
allowed := false
for _, bucket_allowed := range cfg.Server.Documents.Buckets {
for bucket_allowed := range cfg.Server.Documents.Buckets {
if bucket == bucket_allowed {
allowed = true
}
@ -171,7 +171,7 @@ func HandleV1DocumentCreate(c echo.Context) error {
}
allowed := false
for _, bucket_allowed := range cfg.Server.Documents.Buckets {
for bucket_allowed := range cfg.Server.Documents.Buckets {
if bucket == bucket_allowed {
allowed = true
}
@ -240,7 +240,7 @@ func HandleV1DocumentRead(c echo.Context) error {
document := c.Param("document")
allowed := false
for _, bucket_allowed := range cfg.Server.Documents.Buckets {
for bucket_allowed := range cfg.Server.Documents.Buckets {
if bucket == bucket_allowed {
allowed = true
}
@ -317,7 +317,7 @@ func HandleV1DocumentDelete(c echo.Context) error {
document := c.Param("document")
allowed := false
for _, bucket_allowed := range cfg.Server.Documents.Buckets {
for bucket_allowed := range cfg.Server.Documents.Buckets {
if bucket == bucket_allowed {
allowed = true
}

View file

@ -12,8 +12,9 @@
<p>
{{ range . }}
{{ $bucket_name := .Name }}
{{ $bucket_display_name := .DisplayName }}
<details>
<summary>{{ $bucket_name }}</summary>
<summary>{{ $bucket_display_name }}</summary>
<ul>
{{ range .Documents }}