270 lines
7.6 KiB
Go
270 lines
7.6 KiB
Go
package web_handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"sort"
|
|
|
|
"git.agecem.com/agecem/agecem-org/api"
|
|
"git.agecem.com/agecem/agecem-org/models"
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
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))
|
|
}
|
|
|
|
func 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()
|
|
if err != nil {
|
|
response.StatusCode = v1BucketListResponse.StatusCode
|
|
response.Message = v1BucketListResponse.Message
|
|
response.Error = err.Error()
|
|
|
|
return c.Render(response.StatusCode, "documentation-html", response)
|
|
}
|
|
|
|
//TODO check v1BucketListRespone StatusCode and 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))
|
|
if err != nil {
|
|
response.StatusCode = http.StatusInternalServerError
|
|
response.Message = "Error during /v1/bucket/:bucket"
|
|
response.Error = err.Error()
|
|
|
|
return c.Render(response.StatusCode, "documentation-html", response)
|
|
}
|
|
|
|
var v1BucketReadResponse models.V1BucketReadResponse
|
|
|
|
err = json.Unmarshal(content, &v1BucketReadResponse)
|
|
if err != nil {
|
|
response.StatusCode = http.StatusInternalServerError
|
|
response.Message = "Error during json.Unmarshal /v1/bucket/:bucket"
|
|
response.Error = err.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,
|
|
Documents: v1BucketReadResponse.Data.Keys,
|
|
})
|
|
}
|
|
|
|
sort.SliceStable(response.Data.Buckets, func(i, j int) bool { return response.Data.Buckets[i].Name < response.Data.Buckets[j].Name })
|
|
|
|
response.StatusCode = http.StatusOK
|
|
//response.Message = "HandleDocumentation ok"
|
|
|
|
// TODO render .Message
|
|
return c.Render(response.StatusCode, "documentation-html", response)
|
|
//return c.Render(response.StatusCode, "documentation-html", response.Data.Buckets)
|
|
}
|
|
|
|
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())
|
|
}
|
|
|
|
bucket := c.Param("bucket")
|
|
document := c.Param("document")
|
|
|
|
result, err := client.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s/%s", bucket, document))
|
|
if err != nil {
|
|
return c.JSON(models.NotFoundResponse())
|
|
}
|
|
|
|
// Check if result can fit inside a map containing a message
|
|
var result_map map[string]string
|
|
|
|
err = json.Unmarshal(result, &result_map)
|
|
if err == nil {
|
|
return c.JSON(http.StatusBadRequest, result_map)
|
|
}
|
|
|
|
return c.Blob(http.StatusOK, "application/octet-stream", result)
|
|
}
|
|
|
|
func HandleAdmin(c echo.Context) error {
|
|
return c.Render(http.StatusOK, "admin-html", nil)
|
|
}
|
|
|
|
func 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()
|
|
if err != nil {
|
|
response.StatusCode = v1BucketListResponse.StatusCode
|
|
response.Error = err.Error()
|
|
response.Message = v1BucketListResponse.Message
|
|
|
|
return c.Render(response.StatusCode, "admin-upload-html", nil)
|
|
}
|
|
|
|
for bucketName, displayName := range v1BucketListResponse.Data.Buckets {
|
|
response.Data.Buckets = append(response.Data.Buckets, models.Bucket{
|
|
Name: bucketName,
|
|
DisplayName: displayName,
|
|
})
|
|
}
|
|
response.StatusCode = http.StatusOK
|
|
|
|
return c.Render(response.StatusCode, "admin-upload-html", response)
|
|
}
|
|
|
|
func HandleAdminDocumentsUploadPOST(c echo.Context) error {
|
|
/*
|
|
var data struct {
|
|
Buckets []models.Bucket
|
|
Message string
|
|
}
|
|
*/
|
|
|
|
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()
|
|
if err != nil {
|
|
response.StatusCode = v1BucketListResponse.StatusCode
|
|
response.Message = v1BucketListResponse.Message
|
|
response.Error = err.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,
|
|
DisplayName: displayName,
|
|
})
|
|
}
|
|
|
|
bucket := c.FormValue("bucket")
|
|
|
|
document, err := c.FormFile("document")
|
|
if err != nil {
|
|
response.StatusCode = http.StatusBadRequest
|
|
response.Message = "Formulaire invalide"
|
|
response.Error = err.Error()
|
|
|
|
return c.Render(response.StatusCode, "admin-upload-html", response)
|
|
}
|
|
|
|
uploadDocumentResponse, err := client.UploadDocument(bucket, document)
|
|
if err != nil {
|
|
response.StatusCode = uploadDocumentResponse.StatusCode
|
|
response.Message = uploadDocumentResponse.Message
|
|
response.Error = err.Error()
|
|
|
|
return c.Render(response.StatusCode, "admin-upload-html", response)
|
|
}
|
|
|
|
// Format response
|
|
var info, status string
|
|
|
|
info = fmt.Sprintf("[%d] /public/documentation/%s/%s", uploadDocumentResponse.Data.Size, uploadDocumentResponse.Data.Bucket, uploadDocumentResponse.Data.Key)
|
|
|
|
status = uploadDocumentResponse.Message
|
|
|
|
response.StatusCode = http.StatusOK
|
|
response.Message = fmt.Sprintf("%s - %s", status, info)
|
|
|
|
return c.Render(response.StatusCode, "admin-upload-html", response)
|
|
}
|