Merge pull request 'Ajouter api.NewApiClientFromViper() et flags manquants associés' (#70) from feature/new-from-viper into main

Reviewed-on: #70
This commit is contained in:
Victor Lacasse-Beaudoin 2023-07-14 19:17:24 -05:00
commit 346df72955
3 changed files with 41 additions and 11 deletions

View file

@ -9,6 +9,9 @@ import (
"io/ioutil" "io/ioutil"
"mime/multipart" "mime/multipart"
"net/http" "net/http"
"git.agecem.com/agecem/agecem-org/config"
"github.com/spf13/viper"
) )
type API struct { type API struct {
@ -37,6 +40,24 @@ type UploadDocumentResponseInfo struct {
Size float64 `json:"size"` 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) { func New(protocol, host string, port int, opts APIOptions) (*API, error) {
api := API{ api := API{
Protocol: protocol, Protocol: protocol,

View file

@ -110,6 +110,18 @@ func init() {
serverCmd.Flags().String("server-api-key", "agecem-org", "Key to use for authenticating to /v1 routes") 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")) 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 // 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)") 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")) viper.BindPFlag("server.admin.auth", serverCmd.Flags().Lookup("server-admin-auth"))
@ -605,10 +617,7 @@ func handleVieEtudianteOrganisme(c echo.Context) error {
} }
func handleDocumentation(c echo.Context) error { func handleDocumentation(c echo.Context) error {
client, err := api.New("http", "localhost", cfg.Server.Port, api.APIOptions{ client, err := api.NewApiClientFromViper()
KeyAuth: cfg.Server.Api.Auth,
Key: cfg.Server.Api.Key,
})
if err != nil { if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil) return c.Render(http.StatusInternalServerError, "documentation-html", nil)
} }
@ -677,10 +686,7 @@ func handleFormulaires(c echo.Context) error {
} }
func handlePublicDocumentation(c echo.Context) error { func handlePublicDocumentation(c echo.Context) error {
client, err := api.New("http", "localhost", cfg.Server.Port, api.APIOptions{ client, err := api.NewApiClientFromViper()
KeyAuth: cfg.Server.Api.Auth,
Key: cfg.Server.Api.Key,
})
if err != nil { if err != nil {
return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"}) return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"})
} }
@ -713,7 +719,7 @@ func handleAdminDocumentsUpload(c echo.Context) error {
} }
func handleAdminDocumentsUploadPOST(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, KeyAuth: cfg.Server.Api.Auth,
Key: cfg.Server.Api.Key, Key: cfg.Server.Api.Key,
BasicAuth: cfg.Server.Admin.Auth, BasicAuth: cfg.Server.Admin.Auth,

View file

@ -21,8 +21,11 @@ type Config struct {
Username string `mapstructure:"username"` Username string `mapstructure:"username"`
} `mapstructure:"admin"` } `mapstructure:"admin"`
Api struct { Api struct {
Auth bool `mapstructure:"auth"` Auth bool `mapstructure:"auth"`
Key string `mapstructure:"key"` Host string `mapstructure:"host"`
Key string `mapstructure:"key"`
Port int `mapstructure:"port"`
Protocol string `mapstructure:"protocol"`
} `mapstructure:"api"` } `mapstructure:"api"`
Documents struct { Documents struct {
AccessKeyId string `mapstructure:"access_key_id"` AccessKeyId string `mapstructure:"access_key_id"`