Implémenter web_handlers.WebHandler #146

Merged
vlbeaudoin merged 1 commit from refactor/web-handler-dependency-injection into main 2023-08-30 15:26:32 -04:00
3 changed files with 30 additions and 87 deletions
api_handlers
cmd
web_handlers

View file

@ -252,7 +252,7 @@ func (h *V1Handler) HandleV1DocumentRead(c echo.Context) error {
}
// HandleV1DocumentUpdate permet de mettre à jour certains champs d'un object, comme le Content-Type ou le Filename
func HandleV1DocumentUpdate(c echo.Context) error {
func (h *V1Handler) HandleV1DocumentUpdate(c echo.Context) error {
return c.JSON(models.NotImplementedResponse())
}

View file

@ -16,6 +16,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"git.agecem.com/agecem/agecem-org/api"
"git.agecem.com/agecem/agecem-org/api_handlers"
"git.agecem.com/agecem/agecem-org/config"
"git.agecem.com/agecem/agecem-org/media"
@ -207,7 +208,7 @@ func RunServer() {
MediaClient: mediaClient,
}
groupV1.GET("", api_handlers.HandleV1)
groupV1.GET("", v1Handler.HandleV1)
groupV1.POST("/seed", v1Handler.HandleV1Seed)
@ -219,11 +220,19 @@ func RunServer() {
groupV1.GET("/bucket/:bucket/:document", v1Handler.HandleV1DocumentRead)
groupV1.PUT("/bucket/:bucket/:document", api_handlers.HandleV1DocumentUpdate)
groupV1.PUT("/bucket/:bucket/:document", v1Handler.HandleV1DocumentUpdate)
groupV1.DELETE("/bucket/:bucket/:document", v1Handler.HandleV1DocumentDelete)
// HTML Routes
apiClient, err := api.NewApiClientFromViper()
if err != nil {
log.Fatal("Error during NewMediaClientFromViper for API handlers")
}
webHandler := web_handlers.WebHandler{
ApiClient: apiClient,
}
e.GET("/", web_handlers.HandleIndex)
@ -237,21 +246,21 @@ func RunServer() {
e.GET("/vie-etudiante/:organisme", web_handlers.HandleVieEtudianteOrganisme)
e.GET("/documentation", web_handlers.HandleDocumentation)
e.GET("/documentation", webHandler.HandleDocumentation)
e.GET("/formulaires", web_handlers.HandleFormulaires)
// Public Routes
e.GET("/public/documentation/:bucket/:document", web_handlers.HandlePublicDocumentation)
e.GET("/public/documentation/:bucket/:document", webHandler.HandlePublicDocumentation)
// Admin Routes
groupAdmin.GET("", web_handlers.HandleAdmin)
groupAdmin.GET("/documents/upload", web_handlers.HandleAdminDocumentsUpload)
groupAdmin.GET("/documents/upload", webHandler.HandleAdminDocumentsUpload)
groupAdmin.POST("/documents/upload", web_handlers.HandleAdminDocumentsUploadPOST)
groupAdmin.POST("/documents/upload", webHandler.HandleAdminDocumentsUploadPOST)
e.Logger.Fatal(e.Start(
fmt.Sprintf(":%d", cfg.Server.Port)))

View file

@ -11,6 +11,10 @@ import (
"github.com/labstack/echo/v4"
)
type WebHandler struct {
ApiClient *api.API
}
func HandleIndex(c echo.Context) error {
return c.Render(http.StatusOK, "index-html", nil)
}
@ -43,19 +47,10 @@ func HandleVieEtudianteOrganisme(c echo.Context) error {
return c.String(http.StatusOK, fmt.Sprintf("Organisme: %s", organisme))
}
func HandleDocumentation(c echo.Context) error {
func (h *WebHandler) HandleDocumentation(c echo.Context) error {
var response models.HandleDocumentationResponse
client, err := api.NewApiClientFromViper()
if err != nil {
response.StatusCode = http.StatusInternalServerError
response.Message = "Error during api.NewApiClientFromViper()"
response.Error = err.Error()
return c.Render(response.StatusCode, "documentation-html", response)
}
v1BucketListResponse, err := client.ListBuckets()
v1BucketListResponse, err := h.ApiClient.ListBuckets()
if err != nil {
response.StatusCode = v1BucketListResponse.StatusCode
response.Message = v1BucketListResponse.Message
@ -68,7 +63,7 @@ func HandleDocumentation(c echo.Context) error {
for bucket, displayName := range v1BucketListResponse.Data.Buckets {
// TODO move call to dedicated API client method
content, err := client.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s", bucket))
content, err := h.ApiClient.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s", bucket))
if err != nil {
response.StatusCode = http.StatusInternalServerError
response.Message = "Error during /v1/bucket/:bucket"
@ -88,24 +83,6 @@ func HandleDocumentation(c echo.Context) error {
return c.Render(response.StatusCode, "documentation-html", response)
}
// Ce bloc retire tous les caractères spéciaux d'une string
// N'est pas présentement activé, car les fichiers sont processed
// à la création de toute façon.
/*
reg, err := regexp.Compile("[^.a-zA-Z0-9_-]+")
if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
}
var documents_processed []string
for _, document := range documents {
document_processed := reg.ReplaceAllString(document, "")
documents_processed = append(documents_processed, document_processed)
}
documents_processed := documents
*/
response.Data.Buckets = append(response.Data.Buckets, models.Bucket{
Name: bucket,
DisplayName: displayName,
@ -127,16 +104,11 @@ func HandleFormulaires(c echo.Context) error {
return c.Render(http.StatusOK, "formulaires-html", nil)
}
func HandlePublicDocumentation(c echo.Context) error {
client, err := api.NewApiClientFromViper()
if err != nil {
return c.JSON(models.NotFoundResponse())
}
func (h *WebHandler) HandlePublicDocumentation(c echo.Context) error {
bucket := c.Param("bucket")
document := c.Param("document")
result, err := client.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s/%s", bucket, document))
result, err := h.ApiClient.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s/%s", bucket, document))
if err != nil {
return c.JSON(models.NotFoundResponse())
}
@ -156,17 +128,10 @@ func HandleAdmin(c echo.Context) error {
return c.Render(http.StatusOK, "admin-html", nil)
}
func HandleAdminDocumentsUpload(c echo.Context) error {
func (h *WebHandler) HandleAdminDocumentsUpload(c echo.Context) error {
var response models.HandleAdminDocumentsUploadResponse
client, err := api.NewApiClientFromViper()
if err != nil {
response.StatusCode = http.StatusInternalServerError
response.Error = err.Error()
return c.Render(response.StatusCode, "admin-upload-html", nil)
}
v1BucketListResponse, err := client.ListBuckets()
v1BucketListResponse, err := h.ApiClient.ListBuckets()
if err != nil {
response.StatusCode = v1BucketListResponse.StatusCode
response.Error = err.Error()
@ -186,26 +151,10 @@ func HandleAdminDocumentsUpload(c echo.Context) error {
return c.Render(response.StatusCode, "admin-upload-html", response)
}
func HandleAdminDocumentsUploadPOST(c echo.Context) error {
/*
var data struct {
Buckets []models.Bucket
Message string
}
*/
func (h *WebHandler) HandleAdminDocumentsUploadPOST(c echo.Context) error {
var response models.HandleAdminDocumentsUploadResponse
client, err := api.NewApiClientFromViper()
if err != nil {
response.StatusCode = http.StatusInternalServerError
response.Message = "Error during api.newApiClientFromViper()"
response.Error = err.Error()
return c.Render(response.StatusCode, "admin-upload-html", response)
}
v1BucketListResponse, err := client.ListBuckets()
v1BucketListResponse, err := h.ApiClient.ListBuckets()
if err != nil {
response.StatusCode = v1BucketListResponse.StatusCode
response.Message = v1BucketListResponse.Message
@ -214,21 +163,6 @@ func HandleAdminDocumentsUploadPOST(c echo.Context) error {
return c.Render(response.StatusCode, "admin-upload-html", response)
}
/*
result, err := client.Call(http.MethodGet, "/v1/bucket")
if err != nil {
data.Message = "Error during GET /v1/bucket"
return c.Render(http.StatusInternalServerError, "admin-upload-html", data)
}
var buckets map[string]string
err = json.Unmarshal(result, &buckets)
if err != nil {
return c.Render(http.StatusInternalServerError, "admin-upload-html", nil)
}
*/
for bucketName, displayName := range v1BucketListResponse.Data.Buckets {
response.Data.Buckets = append(response.Data.Buckets, models.Bucket{
Name: bucketName,
@ -247,7 +181,7 @@ func HandleAdminDocumentsUploadPOST(c echo.Context) error {
return c.Render(response.StatusCode, "admin-upload-html", response)
}
uploadDocumentResponse, err := client.UploadDocument(bucket, document)
uploadDocumentResponse, err := h.ApiClient.UploadDocument(bucket, document)
if err != nil {
response.StatusCode = uploadDocumentResponse.StatusCode
response.Message = uploadDocumentResponse.Message