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,