From 04de5cdad2a1d73fbee3b9b64b913c42f48e559c Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Fri, 14 Jul 2023 19:56:12 -0400 Subject: [PATCH 1/2] Ajouter flags pour api port, host et protocol Ajouter `server.api.port`, `server.api.host`, `server.api.protocol` --- cmd/server.go | 12 ++++++++++++ config/config.go | 7 +++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/cmd/server.go b/cmd/server.go index 770d970..68b2b3b 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -110,6 +110,18 @@ func init() { serverCmd.Flags().String("server-api-key", "agecem-org", "Key to use for authenticating to /v1 routes") viper.BindPFlag("server.api.key", serverCmd.Flags().Lookup("server-api-key")) + // server.api.port + serverCmd.Flags().Int("server-api-port", 8080, "API server port (config: server.api.port)") + viper.BindPFlag("server.api.port", serverCmd.Flags().Lookup("server-api-port")) + + // server.api.protocol + serverCmd.Flags().String("server-api-protocol", "http", "API server protocol (http/https) (config: server.api.protocol)") + viper.BindPFlag("server.api.protocol", serverCmd.Flags().Lookup("server-api-protocol")) + + // server.api.host + serverCmd.Flags().String("server-api-host", "localhost", "API server host (config: server.api.host)") + viper.BindPFlag("server.api.host", serverCmd.Flags().Lookup("server-api-host")) + // server.admin.auth - --server-admin-auth serverCmd.Flags().Bool("server-admin-auth", true, "Enable to allow basic authentication for /admin routes (config: server.admin.auth)") viper.BindPFlag("server.admin.auth", serverCmd.Flags().Lookup("server-admin-auth")) diff --git a/config/config.go b/config/config.go index decfeca..d2d637a 100644 --- a/config/config.go +++ b/config/config.go @@ -21,8 +21,11 @@ type Config struct { Username string `mapstructure:"username"` } `mapstructure:"admin"` Api struct { - Auth bool `mapstructure:"auth"` - Key string `mapstructure:"key"` + Auth bool `mapstructure:"auth"` + Host string `mapstructure:"host"` + Key string `mapstructure:"key"` + Port int `mapstructure:"port"` + Protocol string `mapstructure:"protocol"` } `mapstructure:"api"` Documents struct { AccessKeyId string `mapstructure:"access_key_id"` From d6a16a72229530d9e61190c6267fa6c0fa601a4e Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Fri, 14 Jul 2023 20:14:15 -0400 Subject: [PATCH 2/2] =?UTF-8?q?Impl=C3=A9menter=20NewApiClientFromViper()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remplacer api.New() par api.NewApiClientFromViper() à certains endroits de serverCmd Remplacer hard-coded api protocol et host par viper où api.New() est encore utilisé --- api/api.go | 21 +++++++++++++++++++++ cmd/server.go | 12 +++--------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/api/api.go b/api/api.go index c7d270d..552d7b6 100644 --- a/api/api.go +++ b/api/api.go @@ -9,6 +9,9 @@ import ( "io/ioutil" "mime/multipart" "net/http" + + "git.agecem.com/agecem/agecem-org/config" + "github.com/spf13/viper" ) type API struct { @@ -37,6 +40,24 @@ type UploadDocumentResponseInfo struct { Size float64 `json:"size"` } +func NewApiClientFromViper() (*API, error) { + var config config.Config + + if err := viper.Unmarshal(&config); err != nil { + return nil, err + } + + api, err := New(config.Server.Api.Protocol, config.Server.Api.Host, config.Server.Api.Port, APIOptions{ + KeyAuth: config.Server.Api.Auth, + Key: config.Server.Api.Key, + }) + if err != nil { + return api, err + } + + return api, nil +} + func New(protocol, host string, port int, opts APIOptions) (*API, error) { api := API{ Protocol: protocol, diff --git a/cmd/server.go b/cmd/server.go index 68b2b3b..8c0f41a 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -617,10 +617,7 @@ func handleVieEtudianteOrganisme(c echo.Context) error { } func handleDocumentation(c echo.Context) error { - client, err := api.New("http", "localhost", cfg.Server.Port, api.APIOptions{ - KeyAuth: cfg.Server.Api.Auth, - Key: cfg.Server.Api.Key, - }) + client, err := api.NewApiClientFromViper() if err != nil { return c.Render(http.StatusInternalServerError, "documentation-html", nil) } @@ -689,10 +686,7 @@ func handleFormulaires(c echo.Context) error { } func handlePublicDocumentation(c echo.Context) error { - client, err := api.New("http", "localhost", cfg.Server.Port, api.APIOptions{ - KeyAuth: cfg.Server.Api.Auth, - Key: cfg.Server.Api.Key, - }) + client, err := api.NewApiClientFromViper() if err != nil { return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"}) } @@ -725,7 +719,7 @@ func handleAdminDocumentsUpload(c echo.Context) error { } func handleAdminDocumentsUploadPOST(c echo.Context) error { - client, err := api.New("http", "localhost", cfg.Server.Port, api.APIOptions{ + client, err := api.New(cfg.Server.Api.Protocol, cfg.Server.Api.Host, cfg.Server.Port, api.APIOptions{ KeyAuth: cfg.Server.Api.Auth, Key: cfg.Server.Api.Key, BasicAuth: cfg.Server.Admin.Auth,