Merge branch 'main' into feature/snackbar

This commit is contained in:
Victor Lacasse-Beaudoin 2023-08-22 12:59:14 -05:00
commit ae9c45d4db
9 changed files with 437 additions and 324 deletions

View file

@ -12,13 +12,17 @@ ADD cmd/ cmd/
ADD api/ api/
ADD api_handlers/ api_handlers/
ADD config/ config/
ADD media/ media/
ADD models/ models/
ADD templates/ templates/
ADD serverhandlers/ serverhandlers/
ADD web_handlers/ web_handlers/
RUN CGO_ENABLED=0 go build -a -installsuffix cgo -o agecem-org .

View file

@ -6,12 +6,12 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"git.agecem.com/agecem/agecem-org/config"
"git.agecem.com/agecem/agecem-org/models"
"github.com/spf13/viper"
)
@ -30,17 +30,9 @@ type APIOptions struct {
Password string
}
type UploadDocumentResponse struct {
Info UploadDocumentResponseInfo `json:"info"`
Message string `json:"message"`
}
type UploadDocumentResponseInfo struct {
Bucket string `json:"bucket"`
Object string `json:"key"`
Size float64 `json:"size"`
}
// NewApiClientFromViper returns a pointer to a new API object,
// provided the configuration options are managed by
// https://git.agecem.com/agecem/agecem-org/config
func NewApiClientFromViper() (*API, error) {
var config config.Config
@ -107,7 +99,7 @@ func (a *API) Call(method, route string) ([]byte, error) {
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
body, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
@ -135,7 +127,7 @@ func (a *API) Call(method, route string) ([]byte, error) {
defer resp.Body.Close()
// Read Response Body
respBody, err := ioutil.ReadAll(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
@ -146,7 +138,8 @@ func (a *API) Call(method, route string) ([]byte, error) {
return nil, errors.New(fmt.Sprintf("method must be 'GET' or 'DELETE', got '%s'", method))
}
func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (UploadDocumentResponse, error) {
func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (models.UploadDocumentResponse, error) {
var response models.UploadDocumentResponse
endpoint := fmt.Sprintf("%s://%s:%d",
a.Protocol,
a.Host,
@ -162,34 +155,34 @@ func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (
// Add the file to the request
file, err := file_header.Open()
if err != nil {
return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#file_header.Open: %s", err)
return response, fmt.Errorf("UploadDocument#file_header.Open: %s", err)
}
defer file.Close()
filename_processed, err := url.QueryUnescape(file_header.Filename)
if err != nil {
return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#url.QueryUnescape: %s", err)
return response, fmt.Errorf("UploadDocument#url.QueryUnescape: %s", err)
}
part, err := writer.CreateFormFile("document", filename_processed)
if err != nil {
return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#writer.CreateFormFile: %s", err)
return response, fmt.Errorf("UploadDocument#writer.CreateFormFile: %s", err)
}
_, err = io.Copy(part, file)
if err != nil {
return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#io.Copy: %s", err)
return response, fmt.Errorf("UploadDocument#io.Copy: %s", err)
}
err = writer.Close()
if err != nil {
return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#writer.Close: %s", err)
return response, fmt.Errorf("UploadDocument#writer.Close: %s", err)
}
// Create a new HTTP request with the multipart body
req, err := http.NewRequest(http.MethodPost, current_url, body)
if err != nil {
return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#http.NewRequest: %s", err)
return response, fmt.Errorf("UploadDocument#http.NewRequest: %s", err)
}
req.Header.Set("Content-Type", writer.FormDataContentType())
@ -202,15 +195,12 @@ func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#client.Do: %s", err)
return response, fmt.Errorf("UploadDocument#client.Do: %s", err)
}
defer resp.Body.Close()
// Handle the response
var res UploadDocumentResponse
json.NewDecoder(resp.Body).Decode(&res)
return res, nil
err = json.NewDecoder(resp.Body).Decode(&response)
return response, err
}
// CallWithData takes data and returns a string representing a response body.
@ -281,3 +271,17 @@ func (a *API) CallWithData(method, route string, data []byte) (string, error) {
//return "", errors.New(fmt.Sprintf("method must be 'POST' or 'PUT', got '%s'", method))
return "", errors.New(fmt.Sprintf("method must be 'POST', got '%s'", method))
}
func (a *API) ListBuckets() (models.V1BucketListResponse, error) {
var response models.V1BucketListResponse
result, err := a.Call(http.MethodGet, "/v1/bucket")
if err != nil {
return response, err
}
if err = json.Unmarshal(result, &response); err != nil {
return response, err
}
return response, nil
}

View file

@ -1,4 +1,4 @@
package serverhandlers
package api_handlers
import (
"context"
@ -7,6 +7,7 @@ import (
"git.agecem.com/agecem/agecem-org/config"
"git.agecem.com/agecem/agecem-org/media"
"git.agecem.com/agecem/agecem-org/models"
"github.com/labstack/echo/v4"
"github.com/minio/minio-go/v7"
"github.com/spf13/viper"
@ -25,51 +26,59 @@ func HandleV1(c echo.Context) error {
// HandleV1Seed créé des buckets dans minio selon la liste de buckets dans server.documents.buckets
// Les buckets sont créés avec paramètres par défaut, et sont ensuite visible dans /v1/bucket.
func HandleV1Seed(c echo.Context) error {
var response models.V1SeedResponse
mediaClient, err := media.NewMediaClientFromViper()
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{
"message": "Error during media.NewMediaClientFromViper()",
"error": err.Error(),
})
response.StatusCode = http.StatusInternalServerError
response.Message = "Error during media.NewMediaClientFromViper()"
response.Error = err.Error()
return c.JSON(response.StatusCode, response)
}
new_buckets, err := mediaClient.Seed()
response.Data.Buckets = new_buckets
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{
"message": "Error during mediaClient.Seed()",
"error": err.Error(),
})
response.StatusCode = http.StatusInternalServerError
response.Message = "Error during mediaClient.Seed()"
response.Error = err.Error()
return c.JSON(response.StatusCode, response)
}
var message string
if len(new_buckets) == 0 {
message = "All buckets already exist"
response.Message = "All buckets already exist"
} else {
message = "Buckets successfully created"
response.Message = "Buckets successfully created"
}
return c.JSON(http.StatusOK, map[string]interface{}{
"message": message,
"buckets": new_buckets,
})
response.StatusCode = http.StatusOK
return c.JSON(response.StatusCode, response)
}
// HandleV1BucketList affiche les buckets permis par server.documents.buckets, qui existent.
func HandleV1BucketList(c echo.Context) error {
var response models.V1BucketListResponse
var cfg config.Config
if err := viper.Unmarshal(&cfg); err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{
"error": err.Error(),
})
response.StatusCode = http.StatusInternalServerError
response.Message = "Error during viper.Unmarshal"
// response.Error = err.Error()
return c.JSON(response.StatusCode, response)
}
mediaClient, err := media.NewMediaClientFromViper()
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{
"message": "Error during media.NewMediaClientFromViper()",
"error": err.Error(),
})
response.StatusCode = http.StatusInternalServerError
response.Message = "Error during media.NewMediaClientFromViper()"
// response.Error = err.Error()
return c.JSON(response.StatusCode, response)
}
var buckets = make(map[string]string)
@ -77,7 +86,11 @@ func HandleV1BucketList(c echo.Context) error {
for bucket_name, bucket_display_name := range cfg.Server.Documents.Buckets {
exists, err := mediaClient.MinioClient.BucketExists(context.Background(), bucket_name)
if err != nil {
return c.JSON(http.StatusInternalServerError, "Error during minio#BucketExists")
response.StatusCode = http.StatusInternalServerError
response.Message = "Error during minio#BucketExists"
// response.Error = err.Error()
return c.JSON(response.StatusCode, response)
}
if exists {
@ -85,7 +98,12 @@ func HandleV1BucketList(c echo.Context) error {
}
}
return c.JSON(http.StatusOK, buckets)
response.StatusCode = http.StatusOK
response.Message = "Buckets list successful"
response.Data.Buckets = buckets
return c.JSON(response.StatusCode, response)
//return c.JSON(response.StatusCode, response.Data.Buckets)
}
func HandleV1BucketRead(c echo.Context) error {

View file

@ -5,10 +5,8 @@ package cmd
import (
"crypto/subtle"
"encoding/json"
"fmt"
"log"
"sort"
"embed"
"html/template"
@ -18,12 +16,12 @@ 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"
"git.agecem.com/agecem/agecem-org/public"
"git.agecem.com/agecem/agecem-org/serverhandlers"
"git.agecem.com/agecem/agecem-org/templates"
"git.agecem.com/agecem/agecem-org/web_handlers"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
@ -200,51 +198,51 @@ func RunServer() {
// API Routes
groupV1.GET("", serverhandlers.HandleV1)
groupV1.GET("", api_handlers.HandleV1)
groupV1.POST("/seed", serverhandlers.HandleV1Seed)
groupV1.POST("/seed", api_handlers.HandleV1Seed)
groupV1.GET("/bucket", serverhandlers.HandleV1BucketList)
groupV1.GET("/bucket", api_handlers.HandleV1BucketList)
groupV1.GET("/bucket/:bucket", serverhandlers.HandleV1BucketRead)
groupV1.GET("/bucket/:bucket", api_handlers.HandleV1BucketRead)
groupV1.POST("/bucket/:bucket", serverhandlers.HandleV1DocumentCreate)
groupV1.POST("/bucket/:bucket", api_handlers.HandleV1DocumentCreate)
groupV1.GET("/bucket/:bucket/:document", serverhandlers.HandleV1DocumentRead)
groupV1.GET("/bucket/:bucket/:document", api_handlers.HandleV1DocumentRead)
groupV1.PUT("/bucket/:bucket/:document", serverhandlers.HandleV1DocumentUpdate)
groupV1.PUT("/bucket/:bucket/:document", api_handlers.HandleV1DocumentUpdate)
groupV1.DELETE("/bucket/:bucket/:document", serverhandlers.HandleV1DocumentDelete)
groupV1.DELETE("/bucket/:bucket/:document", api_handlers.HandleV1DocumentDelete)
// HTML Routes
e.GET("/", handleIndex)
e.GET("/", web_handlers.HandleIndex)
//e.GET("/a-propos", handleAPropos)
//e.GET("/a-propos", web_handlers.HandleAPropos)
//e.GET("/actualite", handleActualite)
//e.GET("/actualite", web_handlers.HandleActualite)
//e.GET("/actualite/:article", handleActualiteArticle)
//e.GET("/actualite/:article", web_handlers.HandleActualiteArticle)
e.GET("/vie-etudiante", handleVieEtudiante)
e.GET("/vie-etudiante", web_handlers.HandleVieEtudiante)
e.GET("/vie-etudiante/:organisme", handleVieEtudianteOrganisme)
e.GET("/vie-etudiante/:organisme", web_handlers.HandleVieEtudianteOrganisme)
e.GET("/documentation", handleDocumentation)
e.GET("/documentation", web_handlers.HandleDocumentation)
e.GET("/formulaires", handleFormulaires)
e.GET("/formulaires", web_handlers.HandleFormulaires)
// Public Routes
e.GET("/public/documentation/:bucket/:document", handlePublicDocumentation)
e.GET("/public/documentation/:bucket/:document", web_handlers.HandlePublicDocumentation)
// Admin Routes
groupAdmin.GET("", handleAdmin)
groupAdmin.GET("", web_handlers.HandleAdmin)
groupAdmin.GET("/documents/upload", handleAdminDocumentsUpload)
groupAdmin.GET("/documents/upload", web_handlers.HandleAdminDocumentsUpload)
groupAdmin.POST("/documents/upload", handleAdminDocumentsUploadPOST)
groupAdmin.POST("/documents/upload", web_handlers.HandleAdminDocumentsUploadPOST)
e.Logger.Fatal(e.Start(
fmt.Sprintf(":%d", cfg.Server.Port)))
@ -253,248 +251,3 @@ func RunServer() {
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}
// HTML Handlers
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 {
client, err := api.NewApiClientFromViper()
if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
}
result, err := client.Call(http.MethodGet, "/v1/bucket")
if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
}
var buckets map[string]string
err = json.Unmarshal(result, &buckets)
if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
}
type Bucket struct {
Name string
DisplayName string
Documents []string
}
var data []Bucket
for bucket, displayName := range buckets {
content, err := client.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s", bucket))
if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
}
var documents []string
err = json.Unmarshal(content, &documents)
if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
}
// 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
*/
data = append(data, Bucket{
Name: bucket,
DisplayName: displayName,
Documents: documents,
})
}
sort.SliceStable(data, func(i, j int) bool { return data[i].Name < data[j].Name })
return c.Render(http.StatusOK, "documentation-html", data)
}
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(http.StatusNotFound, map[string]string{"message": "Not Found"})
}
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(http.StatusNotFound, map[string]string{"message": "Not Found"})
}
// 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 {
client, err := api.NewApiClientFromViper()
if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
}
result, err := client.Call(http.MethodGet, "/v1/bucket")
if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
}
var buckets map[string]string
err = json.Unmarshal(result, &buckets)
if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
}
type Bucket struct {
Name string
DisplayName string
Documents []string
}
var data struct {
Buckets []Bucket
Message string
}
for bucketName, displayName := range buckets {
data.Buckets = append(data.Buckets, Bucket{
Name: bucketName,
DisplayName: displayName,
})
}
return c.Render(http.StatusOK, "admin-upload-html", data)
}
func handleAdminDocumentsUploadPOST(c echo.Context) error {
type Bucket struct {
Name string
DisplayName string
Documents []string
}
var data struct {
Buckets []Bucket
Message string
}
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,
Username: cfg.Server.Admin.Username,
Password: cfg.Server.Admin.Password,
})
if err != nil {
data.Message = fmt.Sprintf("handleAdminDocumentsUploadPOST#api.New: %s", err)
return c.Render(http.StatusInternalServerError, "admin-upload-html", data)
}
result, err := client.Call(http.MethodGet, "/v1/bucket")
if err != nil {
data.Message = "Error during GET /v1/bucket"
return c.Render(http.StatusInternalServerError, "documentation-html", data)
}
var buckets map[string]string
err = json.Unmarshal(result, &buckets)
if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
}
for bucketName, displayName := range buckets {
data.Buckets = append(data.Buckets, Bucket{
Name: bucketName,
DisplayName: displayName,
})
}
bucket := c.FormValue("bucket")
document, err := c.FormFile("document")
if err != nil {
data.Message = fmt.Sprintf("handleAdminDocumentsUploadPOST#c.FormFile: %s", err)
return c.Render(http.StatusBadRequest, "admin-upload-html", data)
}
response, err := client.UploadDocument(bucket, document)
if err != nil {
data.Message = fmt.Sprintf("handleAdminDocumentsUploadPOST#client.UploadDocument: %s", err)
return c.Render(http.StatusInternalServerError, "admin-upload-html", data)
}
// Format response
var info, status string
info = fmt.Sprintf("[%.0f] /public/documentation/%s/%s", response.Info.Size, response.Info.Bucket, response.Info.Object)
status = response.Message
data.Message = fmt.Sprintf("%s - %s", status, info)
return c.Render(http.StatusOK, "admin-upload-html", data)
}

7
models/models.go Normal file
View file

@ -0,0 +1,7 @@
package models
type Bucket struct {
Name string
DisplayName string
Documents []string
}

54
models/responses.go Normal file
View file

@ -0,0 +1,54 @@
package models
type Responder interface {
Respond() Responder
}
type Response struct {
StatusCode int `json:"status_code"`
Message string
Error string
}
func (r Response) Respond() Responder {
return r
}
type HandleAdminDocumentsUploadResponse struct {
Response
Data struct {
Buckets []Bucket
}
}
type HandleDocumentationResponse struct {
Response
Data struct {
Buckets []Bucket
}
}
type UploadDocumentResponse struct {
Response
Data UploadDocumentResponseData
}
type UploadDocumentResponseData struct {
Bucket string
Object string
Size float64
}
type V1SeedResponse struct {
Response
Data struct {
Buckets []string
}
}
type V1BucketListResponse struct {
Response
Data struct {
Buckets map[string]string
}
}

View file

@ -12,7 +12,7 @@
<form class="form adminUploadForm" action="/admin/documents/upload" method="post" enctype="multipart/form-data">
<label class="formLabel" for="bucket">Type de document:</label>
<select class="formSelect" name="bucket" id="bucket">
{{ range .Buckets }}
{{ range .Data.Buckets }}
<option class="formOption" value="{{ .Name }}">{{ .DisplayName }}</option>
{{ end }}
</select>

View file

@ -12,7 +12,7 @@
<div class="wrapper documentationWrapper">
<h1 class="heading1">Documentation</h1>
<p>
{{ range . }}
{{ range .Data.Buckets }}
{{ $bucket_name := .Name }}
{{ $bucket_display_name := .DisplayName }}
<details class="documentationCategorie">
@ -25,6 +25,9 @@
</details>
{{ end }}
</p>
<p>
{{ .Message }}
</p>
</div>
{{ template "snackbar-html" }}
</body>

View file

@ -0,0 +1,270 @@
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
// TODO add Response type
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 documents []string
err = json.Unmarshal(content, &documents)
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: documents,
})
}
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
// 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(http.StatusNotFound, map[string]string{"message": "Not Found"})
}
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(http.StatusNotFound, map[string]string{"message": "Not Found"})
}
// 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("[%.0f] /public/documentation/%s/%s", uploadDocumentResponse.Data.Size, uploadDocumentResponse.Data.Bucket, uploadDocumentResponse.Data.Object)
status = uploadDocumentResponse.Message
response.StatusCode = http.StatusOK
response.Message = fmt.Sprintf("%s - %s", status, info)
return c.Render(response.StatusCode, "admin-upload-html", response)
}