2023-08-20 16:19:05 -04:00
|
|
|
package web_handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-10-24 17:00:49 -04:00
|
|
|
"io"
|
2023-08-20 16:19:05 -04:00
|
|
|
"net/http"
|
2023-10-24 17:20:57 -04:00
|
|
|
"net/url"
|
2023-08-20 16:19:05 -04:00
|
|
|
"sort"
|
|
|
|
|
|
|
|
"git.agecem.com/agecem/agecem-org/api"
|
2023-10-24 17:39:15 -04:00
|
|
|
"git.agecem.com/agecem/agecem-org/apiresponse"
|
2023-08-20 16:19:05 -04:00
|
|
|
"git.agecem.com/agecem/agecem-org/models"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
2023-08-30 15:24:37 -04:00
|
|
|
type WebHandler struct {
|
|
|
|
ApiClient *api.API
|
|
|
|
}
|
|
|
|
|
2023-08-20 16:19:05 -04:00
|
|
|
func HandleIndex(c echo.Context) error {
|
|
|
|
return c.Render(http.StatusOK, "index-html", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
func HandleAPropos(c echo.Context) error {
|
|
|
|
return c.Render(http.StatusOK, "a-propos-html", nil)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
func HandleActualite(c echo.Context) error {
|
|
|
|
return c.Render(http.StatusOK, "actualite-html", nil)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
func HandleActualiteArticle(c echo.Context) error {
|
|
|
|
article := c.Param("article")
|
|
|
|
return c.String(http.StatusOK, fmt.Sprintf("Article: %s", article))
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
func HandleVieEtudiante(c echo.Context) error {
|
|
|
|
return c.Render(http.StatusOK, "vie-etudiante-html", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func HandleVieEtudianteOrganisme(c echo.Context) error {
|
|
|
|
organisme := c.Param("organisme")
|
|
|
|
return c.String(http.StatusOK, fmt.Sprintf("Organisme: %s", organisme))
|
|
|
|
}
|
|
|
|
|
2023-08-30 15:24:37 -04:00
|
|
|
func (h *WebHandler) HandleDocumentation(c echo.Context) error {
|
2023-10-24 17:39:15 -04:00
|
|
|
var response apiresponse.HandleDocumentationResponse
|
2023-08-20 18:03:45 -04:00
|
|
|
|
2023-08-30 15:24:37 -04:00
|
|
|
v1BucketListResponse, err := h.ApiClient.ListBuckets()
|
2023-08-20 16:19:05 -04:00
|
|
|
if err != nil {
|
2023-08-20 18:03:45 -04:00
|
|
|
response.Error = err.Error()
|
2023-10-24 17:00:49 -04:00
|
|
|
response.Message = v1BucketListResponse.Message
|
|
|
|
response.StatusCode = v1BucketListResponse.StatusCode
|
2023-08-20 16:19:05 -04:00
|
|
|
|
2023-08-20 18:03:45 -04:00
|
|
|
return c.Render(response.StatusCode, "documentation-html", response)
|
2023-08-20 16:19:05 -04:00
|
|
|
}
|
|
|
|
|
2023-08-20 18:03:45 -04:00
|
|
|
//TODO check v1BucketListRespone StatusCode and Error
|
2023-08-20 16:19:05 -04:00
|
|
|
|
2023-08-20 18:03:45 -04:00
|
|
|
for bucket, displayName := range v1BucketListResponse.Data.Buckets {
|
|
|
|
// TODO move call to dedicated API client method
|
2023-10-24 17:39:15 -04:00
|
|
|
var v1BucketReadResponse apiresponse.V1BucketReadResponse
|
2023-08-20 16:19:05 -04:00
|
|
|
|
2023-10-24 17:00:49 -04:00
|
|
|
if err = h.ApiClient.Voki.Unmarshal(http.MethodGet, fmt.Sprintf("/v1/bucket/%s", bucket), nil, true, &v1BucketReadResponse); err != nil {
|
2023-08-20 18:35:07 -04:00
|
|
|
response.Error = err.Error()
|
2023-10-24 17:00:49 -04:00
|
|
|
response.Message = "Error during json.Unmarshal /v1/bucket/:bucket"
|
|
|
|
response.StatusCode = http.StatusInternalServerError
|
2023-08-20 18:35:07 -04:00
|
|
|
|
2023-10-24 17:00:49 -04:00
|
|
|
return c.Render(http.StatusOK, "documentation-html", response)
|
2023-08-20 16:19:05 -04:00
|
|
|
}
|
|
|
|
|
2023-08-20 18:03:45 -04:00
|
|
|
response.Data.Buckets = append(response.Data.Buckets, models.Bucket{
|
2023-08-20 16:19:05 -04:00
|
|
|
Name: bucket,
|
|
|
|
DisplayName: displayName,
|
2023-08-22 15:08:10 -04:00
|
|
|
Documents: v1BucketReadResponse.Data.Keys,
|
2023-08-20 16:19:05 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-20 18:03:45 -04:00
|
|
|
sort.SliceStable(response.Data.Buckets, func(i, j int) bool { return response.Data.Buckets[i].Name < response.Data.Buckets[j].Name })
|
2023-08-20 16:19:05 -04:00
|
|
|
|
2023-08-20 18:03:45 -04:00
|
|
|
response.StatusCode = http.StatusOK
|
2023-08-22 15:08:10 -04:00
|
|
|
//response.Message = "HandleDocumentation ok"
|
2023-08-20 18:03:45 -04:00
|
|
|
|
|
|
|
// TODO render .Message
|
2023-10-24 17:00:49 -04:00
|
|
|
return c.Render(http.StatusOK, "documentation-html", response)
|
2023-08-20 18:35:07 -04:00
|
|
|
//return c.Render(response.StatusCode, "documentation-html", response.Data.Buckets)
|
2023-08-20 16:19:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func HandleFormulaires(c echo.Context) error {
|
|
|
|
return c.Render(http.StatusOK, "formulaires-html", nil)
|
|
|
|
}
|
|
|
|
|
2023-08-30 15:24:37 -04:00
|
|
|
func (h *WebHandler) HandlePublicDocumentation(c echo.Context) error {
|
2023-08-20 16:19:05 -04:00
|
|
|
bucket := c.Param("bucket")
|
|
|
|
document := c.Param("document")
|
|
|
|
|
2023-10-24 17:20:57 -04:00
|
|
|
unescaped, err := url.QueryUnescape(fmt.Sprintf("/v1/bucket/%s/%s", bucket, document))
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusBadRequest, map[string]string{"message": "Bad Request"})
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := h.ApiClient.Voki.Call(http.MethodGet, unescaped, nil, true)
|
2023-08-20 16:19:05 -04:00
|
|
|
if err != nil {
|
2023-10-24 17:39:15 -04:00
|
|
|
return c.JSON(apiresponse.NotFoundResponse())
|
2023-08-20 16:19:05 -04:00
|
|
|
}
|
2023-10-24 17:00:49 -04:00
|
|
|
defer response.Body.Close()
|
2023-08-20 16:19:05 -04:00
|
|
|
|
2023-10-24 17:00:49 -04:00
|
|
|
switch response.StatusCode {
|
|
|
|
case http.StatusNotFound:
|
2023-10-24 17:39:15 -04:00
|
|
|
return c.JSON(apiresponse.NotFoundResponse())
|
2023-10-24 17:00:49 -04:00
|
|
|
case http.StatusInternalServerError:
|
|
|
|
return c.JSON(http.StatusInternalServerError, map[string]string{"message": "Internal Server Error"})
|
|
|
|
}
|
2023-08-20 16:19:05 -04:00
|
|
|
|
2023-10-24 17:00:49 -04:00
|
|
|
body, err := io.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, map[string]string{"message": "Internal Server Error"})
|
2023-08-20 16:19:05 -04:00
|
|
|
}
|
|
|
|
|
2023-10-24 17:00:49 -04:00
|
|
|
return c.Blob(http.StatusOK, "application/octet-stream", body)
|
2023-08-20 16:19:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func HandleAdmin(c echo.Context) error {
|
|
|
|
return c.Render(http.StatusOK, "admin-html", nil)
|
|
|
|
}
|
|
|
|
|
2023-08-30 15:24:37 -04:00
|
|
|
func (h *WebHandler) HandleAdminDocumentsUpload(c echo.Context) error {
|
2023-10-24 17:39:15 -04:00
|
|
|
var response apiresponse.HandleAdminDocumentsUploadResponse
|
2023-08-20 16:19:05 -04:00
|
|
|
|
2023-08-30 15:24:37 -04:00
|
|
|
v1BucketListResponse, err := h.ApiClient.ListBuckets()
|
2023-08-20 16:19:05 -04:00
|
|
|
if err != nil {
|
2023-08-20 18:03:45 -04:00
|
|
|
response.StatusCode = v1BucketListResponse.StatusCode
|
|
|
|
response.Error = err.Error()
|
|
|
|
response.Message = v1BucketListResponse.Message
|
2023-08-20 16:19:05 -04:00
|
|
|
|
2023-08-20 18:03:45 -04:00
|
|
|
return c.Render(response.StatusCode, "admin-upload-html", nil)
|
2023-08-20 16:19:05 -04:00
|
|
|
}
|
|
|
|
|
2023-08-20 18:03:45 -04:00
|
|
|
for bucketName, displayName := range v1BucketListResponse.Data.Buckets {
|
|
|
|
response.Data.Buckets = append(response.Data.Buckets, models.Bucket{
|
2023-08-20 16:19:05 -04:00
|
|
|
Name: bucketName,
|
|
|
|
DisplayName: displayName,
|
|
|
|
})
|
|
|
|
}
|
2023-08-20 18:03:45 -04:00
|
|
|
response.StatusCode = http.StatusOK
|
2023-08-20 16:19:05 -04:00
|
|
|
|
2023-08-20 18:03:45 -04:00
|
|
|
return c.Render(response.StatusCode, "admin-upload-html", response)
|
2023-08-20 16:19:05 -04:00
|
|
|
}
|
|
|
|
|
2023-08-30 15:24:37 -04:00
|
|
|
func (h *WebHandler) HandleAdminDocumentsUploadPOST(c echo.Context) error {
|
2023-10-24 17:39:15 -04:00
|
|
|
var response apiresponse.HandleAdminDocumentsUploadResponse
|
2023-08-20 16:19:05 -04:00
|
|
|
|
2023-08-30 15:24:37 -04:00
|
|
|
v1BucketListResponse, err := h.ApiClient.ListBuckets()
|
2023-08-20 16:19:05 -04:00
|
|
|
if err != nil {
|
2023-08-20 18:03:45 -04:00
|
|
|
response.StatusCode = v1BucketListResponse.StatusCode
|
|
|
|
response.Message = v1BucketListResponse.Message
|
|
|
|
response.Error = err.Error()
|
|
|
|
|
|
|
|
return c.Render(response.StatusCode, "admin-upload-html", response)
|
2023-08-20 16:19:05 -04:00
|
|
|
}
|
|
|
|
|
2023-08-20 18:03:45 -04:00
|
|
|
for bucketName, displayName := range v1BucketListResponse.Data.Buckets {
|
|
|
|
response.Data.Buckets = append(response.Data.Buckets, models.Bucket{
|
2023-08-20 16:19:05 -04:00
|
|
|
Name: bucketName,
|
|
|
|
DisplayName: displayName,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket := c.FormValue("bucket")
|
|
|
|
|
|
|
|
document, err := c.FormFile("document")
|
|
|
|
if err != nil {
|
2023-08-20 18:03:45 -04:00
|
|
|
response.StatusCode = http.StatusBadRequest
|
|
|
|
response.Message = "Formulaire invalide"
|
|
|
|
response.Error = err.Error()
|
|
|
|
|
|
|
|
return c.Render(response.StatusCode, "admin-upload-html", response)
|
2023-08-20 16:19:05 -04:00
|
|
|
}
|
|
|
|
|
2023-08-30 15:24:37 -04:00
|
|
|
uploadDocumentResponse, err := h.ApiClient.UploadDocument(bucket, document)
|
2023-08-20 16:19:05 -04:00
|
|
|
if err != nil {
|
2023-08-20 18:03:45 -04:00
|
|
|
response.StatusCode = uploadDocumentResponse.StatusCode
|
|
|
|
response.Message = uploadDocumentResponse.Message
|
|
|
|
response.Error = err.Error()
|
|
|
|
|
|
|
|
return c.Render(response.StatusCode, "admin-upload-html", response)
|
2023-08-20 16:19:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Format response
|
|
|
|
var info, status string
|
|
|
|
|
2023-08-23 16:01:57 -04:00
|
|
|
info = fmt.Sprintf("[%d] /public/documentation/%s/%s", uploadDocumentResponse.Data.Size, uploadDocumentResponse.Data.Bucket, uploadDocumentResponse.Data.Key)
|
2023-08-20 16:19:05 -04:00
|
|
|
|
2023-08-20 18:03:45 -04:00
|
|
|
status = uploadDocumentResponse.Message
|
2023-08-20 16:19:05 -04:00
|
|
|
|
2023-08-20 18:03:45 -04:00
|
|
|
response.StatusCode = http.StatusOK
|
|
|
|
response.Message = fmt.Sprintf("%s - %s", status, info)
|
2023-08-20 16:19:05 -04:00
|
|
|
|
2023-08-20 18:03:45 -04:00
|
|
|
return c.Render(response.StatusCode, "admin-upload-html", response)
|
2023-08-20 16:19:05 -04:00
|
|
|
}
|