2023-03-28 02:35:42 -04:00
|
|
|
/*
|
|
|
|
Copyright © 2023 AGECEM
|
|
|
|
*/
|
|
|
|
package cmd
|
2023-02-17 17:28:47 -05:00
|
|
|
|
|
|
|
import (
|
2023-04-21 18:51:20 -04:00
|
|
|
"context"
|
2023-04-21 21:34:18 -04:00
|
|
|
"crypto/subtle"
|
2023-04-26 16:51:13 -04:00
|
|
|
"encoding/json"
|
2023-03-21 20:29:06 -04:00
|
|
|
"fmt"
|
2023-04-21 21:34:18 -04:00
|
|
|
"log"
|
2023-03-28 02:35:42 -04:00
|
|
|
|
|
|
|
"embed"
|
2023-03-21 18:37:51 -04:00
|
|
|
"html/template"
|
|
|
|
"io"
|
2023-02-17 17:28:47 -05:00
|
|
|
"net/http"
|
2023-03-21 18:48:23 -04:00
|
|
|
"sort"
|
2023-02-17 17:28:47 -05:00
|
|
|
|
2023-04-21 18:51:20 -04:00
|
|
|
"github.com/minio/minio-go/v7"
|
|
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
2023-03-28 02:35:42 -04:00
|
|
|
"github.com/spf13/cobra"
|
2023-04-21 16:41:50 -04:00
|
|
|
"github.com/spf13/viper"
|
2023-03-28 02:35:42 -04:00
|
|
|
|
2023-04-26 16:51:13 -04:00
|
|
|
"git.agecem.com/agecem/agecem-org/api"
|
2023-03-21 18:37:51 -04:00
|
|
|
"git.agecem.com/agecem/agecem-org/public"
|
2023-02-17 17:28:47 -05:00
|
|
|
"github.com/labstack/echo/v4"
|
2023-03-21 18:37:51 -04:00
|
|
|
"github.com/labstack/echo/v4/middleware"
|
2023-02-17 17:28:47 -05:00
|
|
|
)
|
|
|
|
|
2023-03-21 18:37:51 -04:00
|
|
|
type Template struct {
|
|
|
|
templates *template.Template
|
|
|
|
}
|
|
|
|
|
|
|
|
var embedFS embed.FS
|
|
|
|
|
2023-03-28 02:35:42 -04:00
|
|
|
// serverCmd represents the server command
|
|
|
|
var serverCmd = &cobra.Command{
|
|
|
|
Use: "server",
|
|
|
|
Short: "Démarrer le serveur web",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
RunServer()
|
|
|
|
},
|
|
|
|
}
|
2023-02-23 04:24:04 -05:00
|
|
|
|
|
|
|
func init() {
|
2023-03-28 02:35:42 -04:00
|
|
|
rootCmd.AddCommand(serverCmd)
|
2023-03-21 18:37:51 -04:00
|
|
|
embedFS = public.GetEmbedFS()
|
2023-04-21 16:41:50 -04:00
|
|
|
|
|
|
|
// server.port - --server-port
|
|
|
|
serverCmd.Flags().Int("server-port", 8080, "Port to run the webserver on (config: server.port)")
|
|
|
|
viper.BindPFlag("server.port", serverCmd.Flags().Lookup("server-port"))
|
|
|
|
|
|
|
|
// server.documents.location - --server-documents-location
|
|
|
|
serverCmd.Flags().String("server-documents-location", "us-east", "Storage bucket location (config: server.documents.location)")
|
|
|
|
viper.BindPFlag("server.documents.location", serverCmd.Flags().Lookup("server-documents-location"))
|
|
|
|
|
|
|
|
// server.documents.endpoint - --server-documents-endpoint
|
|
|
|
serverCmd.Flags().String("server-documents-endpoint", "minio:9000", "Storage server endpoint (config: server.documents.endpoint)")
|
|
|
|
viper.BindPFlag("server.documents.endpoint", serverCmd.Flags().Lookup("server-documents-endpoint"))
|
|
|
|
|
|
|
|
// server.documents.access_key_id - --server-documents-access-key-id
|
|
|
|
serverCmd.Flags().String("server-documents-access-key-id", "", "Storage server access key id (config: server.documents.access_key_id)")
|
|
|
|
viper.BindPFlag("documents.accessKeyID", serverCmd.Flags().Lookup("documents-access-key-id"))
|
|
|
|
|
|
|
|
// server.documents.secret_access_key - --server-documents-secret-access-key
|
|
|
|
serverCmd.Flags().String("server-documents-secret-access-key", "", "Storage server secret access key (config: server.documents.secret_access_key)")
|
|
|
|
viper.BindPFlag("server.documents.secret_access_key", serverCmd.Flags().Lookup("server-documents-secret-access-key"))
|
|
|
|
|
|
|
|
// server.documents.use_ssl - --server-documents-use-ssl
|
|
|
|
serverCmd.Flags().Bool("server-documents-use-ssl", true, "Storage server SSL status (config: server.documents.use_ssl)")
|
|
|
|
viper.BindPFlag("server.documents.use_ssl", serverCmd.Flags().Lookup("server-documents-use-ssl"))
|
|
|
|
|
|
|
|
// server.documents.buckets - --server-documents-buckets
|
|
|
|
serverCmd.Flags().StringSlice("server-documents-buckets", nil, "Buckets that are allowed to be accessed by the API (config: server.documents.buckets)")
|
|
|
|
viper.BindPFlag("server.documents.buckets", serverCmd.Flags().Lookup("server-documents-buckets"))
|
2023-04-21 21:34:18 -04:00
|
|
|
|
|
|
|
// server.api.auth - --server-api-auth
|
|
|
|
serverCmd.Flags().Bool("server-api-auth", false, "Enable to allow key authentication for /v1 routes (config: server.api.auth)")
|
|
|
|
viper.BindPFlag("server.api.auth", serverCmd.Flags().Lookup("server-api-auth"))
|
|
|
|
|
|
|
|
// server.api.key - --server-api-key
|
|
|
|
serverCmd.Flags().String("server-api-key", "", "Key to use for authenticating to /v1 routes")
|
|
|
|
viper.BindPFlag("server.api.key", serverCmd.Flags().Lookup("server-api-key"))
|
2023-04-26 19:15:22 -04:00
|
|
|
|
|
|
|
// server.admin.auth - --server-admin-auth
|
|
|
|
serverCmd.Flags().Bool("server-admin-auth", false, "Enable to allow basic authentication for /admin routes (config: server.admin.auth)")
|
|
|
|
viper.BindPFlag("server.admin.auth", serverCmd.Flags().Lookup("server-admin-auth"))
|
|
|
|
|
|
|
|
// server.admin.username - --server-frontend-username
|
|
|
|
serverCmd.Flags().String("server-admin-username", "", "Username for basic authentication for /admin routes (config: server.admin.username)")
|
|
|
|
viper.BindPFlag("server.admin.username", serverCmd.Flags().Lookup("server-admin-username"))
|
|
|
|
|
|
|
|
// server.admin.password - --server-frontend-password
|
|
|
|
serverCmd.Flags().String("server-admin-password", "", "Password for basic authentication for /admin routes (config: server.admin.password)")
|
|
|
|
viper.BindPFlag("server.admin.password", serverCmd.Flags().Lookup("server-admin-password"))
|
2023-02-23 04:24:04 -05:00
|
|
|
}
|
|
|
|
|
2023-03-28 02:35:42 -04:00
|
|
|
func RunServer() {
|
2023-02-17 17:28:47 -05:00
|
|
|
e := echo.New()
|
2023-02-23 04:24:04 -05:00
|
|
|
|
2023-03-21 18:37:51 -04:00
|
|
|
t := &Template{
|
|
|
|
templates: template.Must(template.ParseFS(embedFS, "html/*.gohtml")),
|
|
|
|
}
|
|
|
|
|
|
|
|
e.Renderer = t
|
|
|
|
|
|
|
|
e.Pre(middleware.RemoveTrailingSlash())
|
|
|
|
|
2023-04-21 21:34:18 -04:00
|
|
|
groupV1 := e.Group("/v1")
|
|
|
|
|
|
|
|
groupV1.Use(middleware.AddTrailingSlash())
|
|
|
|
|
|
|
|
if viper.GetBool("server.api.auth") {
|
|
|
|
if len(viper.GetString("server.api.key")) < 10 {
|
|
|
|
log.Fatal("server.api.auth is enabled, but server.api.key is too small (needs at least 10 characters)")
|
|
|
|
}
|
|
|
|
|
|
|
|
groupV1.Use(middleware.KeyAuth(func(key string, c echo.Context) (bool, error) {
|
|
|
|
return subtle.ConstantTimeCompare([]byte(key), []byte(viper.GetString("server.api.key"))) == 1, nil
|
|
|
|
}))
|
2023-04-26 19:15:22 -04:00
|
|
|
|
|
|
|
log.Println("Key auth for /v1 activated")
|
|
|
|
}
|
|
|
|
|
|
|
|
groupAdmin := e.Group("/admin")
|
|
|
|
|
|
|
|
groupAdmin.Use(middleware.AddTrailingSlash())
|
|
|
|
|
|
|
|
if viper.GetBool("server.admin.auth") {
|
|
|
|
username := viper.GetString("server.admin.username")
|
|
|
|
password := viper.GetString("server.admin.password")
|
|
|
|
if len(username) < 5 {
|
|
|
|
log.Fatal("server.admin.auth is enabled, but server.admin.username is too small (needs at least 5 characters)")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(password) < 10 {
|
|
|
|
log.Fatal("server.admin.auth is enabled, but server.admin.password is too small (needs at least 10 characters)")
|
|
|
|
}
|
|
|
|
|
|
|
|
groupAdmin.Use(middleware.BasicAuth(func(username_entered, password_entered string, c echo.Context) (bool, error) {
|
|
|
|
// Be careful to use constant time comparison to prevent timing attacks
|
|
|
|
if subtle.ConstantTimeCompare([]byte(username_entered), []byte(username)) == 1 &&
|
|
|
|
subtle.ConstantTimeCompare([]byte(password_entered), []byte(password)) == 1 {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}))
|
|
|
|
|
|
|
|
log.Println("Basic auth for /admin activated")
|
2023-04-21 21:34:18 -04:00
|
|
|
}
|
|
|
|
|
2023-03-21 18:48:23 -04:00
|
|
|
// API Routes
|
|
|
|
|
2023-04-21 21:34:18 -04:00
|
|
|
groupV1.GET("", handleV1)
|
|
|
|
|
|
|
|
groupV1.POST("/seed", handleV1Seed)
|
2023-03-21 18:48:23 -04:00
|
|
|
|
2023-04-21 21:34:18 -04:00
|
|
|
groupV1.GET("/bucket", handleV1BucketList)
|
|
|
|
|
|
|
|
groupV1.GET("/bucket/:bucket", handleV1BucketRead)
|
2023-04-21 16:41:50 -04:00
|
|
|
|
2023-04-24 18:29:05 -04:00
|
|
|
groupV1.POST("/bucket/:bucket", handleV1DocumentCreate)
|
2023-04-24 17:19:52 -04:00
|
|
|
|
|
|
|
groupV1.GET("/bucket/:bucket/:document", handleV1DocumentRead)
|
|
|
|
|
|
|
|
groupV1.PUT("/bucket/:bucket/:document", handleV1DocumentUpdate)
|
|
|
|
|
|
|
|
groupV1.DELETE("/bucket/:bucket/:document", handleV1DocumentDelete)
|
|
|
|
|
2023-03-21 20:26:39 -04:00
|
|
|
// Static Routes
|
|
|
|
|
2023-03-24 20:02:46 -04:00
|
|
|
e.GET("/static/general.css", handleStaticCSSGeneral)
|
|
|
|
|
2023-03-21 20:26:39 -04:00
|
|
|
e.GET("/static/index.css", handleStaticCSSIndex)
|
|
|
|
|
2023-03-21 18:48:23 -04:00
|
|
|
// HTML Routes
|
|
|
|
|
2023-03-21 18:37:51 -04:00
|
|
|
e.GET("/", handleIndex)
|
|
|
|
|
2023-03-21 20:29:06 -04:00
|
|
|
e.GET("/a-propos", handleAPropos)
|
|
|
|
|
|
|
|
e.GET("/actualite", handleActualite)
|
|
|
|
|
|
|
|
e.GET("/actualite/:article", handleActualiteArticle)
|
|
|
|
|
|
|
|
e.GET("/vie-etudiante", handleVieEtudiante)
|
|
|
|
|
|
|
|
e.GET("/vie-etudiante/:organisme", handleVieEtudianteOrganisme)
|
|
|
|
|
|
|
|
e.GET("/documentation", handleDocumentation)
|
|
|
|
|
2023-03-24 18:21:22 -04:00
|
|
|
e.GET("/formulaires", handleFormulaires)
|
2023-03-21 20:29:06 -04:00
|
|
|
|
2023-04-26 18:27:58 -04:00
|
|
|
// Public Routes
|
|
|
|
|
|
|
|
e.GET("/public/documentation/:bucket/:document", handlePublicDocumentation)
|
|
|
|
|
2023-04-26 19:15:22 -04:00
|
|
|
// Admin Routes
|
|
|
|
|
|
|
|
groupAdmin.GET("", handleAdmin)
|
|
|
|
|
2023-04-26 19:28:20 -04:00
|
|
|
groupAdmin.GET("/documents/upload", handleAdminDocumentsUpload)
|
|
|
|
|
2023-04-26 19:43:43 -04:00
|
|
|
groupAdmin.POST("/documents/upload", handleAdminDocumentsUploadPOST)
|
|
|
|
|
2023-04-21 16:41:50 -04:00
|
|
|
e.Logger.Fatal(e.Start(
|
|
|
|
fmt.Sprintf(":%d", viper.GetInt("server.port"))))
|
2023-02-17 17:28:47 -05:00
|
|
|
}
|
2023-03-21 18:37:51 -04:00
|
|
|
|
|
|
|
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
|
|
|
|
return t.templates.ExecuteTemplate(w, name, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// API Handlers
|
|
|
|
|
2023-03-21 18:48:23 -04:00
|
|
|
// handleV1 affiche les routes accessibles.
|
|
|
|
// Les routes sont triées selon .Path, pour les rendre plus facilement navigables.
|
|
|
|
func handleV1(c echo.Context) error {
|
|
|
|
routes := c.Echo().Routes()
|
|
|
|
sort.Slice(routes, func(i, j int) bool { return routes[i].Path < routes[j].Path })
|
|
|
|
return c.JSON(http.StatusOK, routes)
|
|
|
|
}
|
|
|
|
|
2023-04-21 21:34:18 -04:00
|
|
|
// 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 {
|
|
|
|
documents_buckets := viper.GetStringSlice("server.documents.buckets")
|
|
|
|
documents_endpoint := viper.GetString("server.documents.endpoint")
|
|
|
|
documents_access_key_id := viper.GetString("server.documents.access_key_id")
|
|
|
|
documents_secret_access_key := viper.GetString("server.documents.secret_access_key")
|
|
|
|
documents_use_ssl := viper.GetBool("server.documents.use_ssl")
|
|
|
|
|
|
|
|
// Initialize minio client object
|
|
|
|
client, err := minio.New(documents_endpoint, &minio.Options{
|
|
|
|
Creds: credentials.NewStaticV4(documents_access_key_id, documents_secret_access_key, ""),
|
|
|
|
Secure: documents_use_ssl,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, map[string]string{
|
|
|
|
"message": "Error during minio#New",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var new_buckets []string
|
|
|
|
|
|
|
|
for _, bucket := range documents_buckets {
|
2023-04-24 16:29:24 -04:00
|
|
|
exists, err := client.BucketExists(context.Background(), bucket)
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, map[string]string{
|
|
|
|
"message": "Error during minio#BucketExists",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-04-21 21:34:18 -04:00
|
|
|
if err = client.MakeBucket(context.Background(), bucket, minio.MakeBucketOptions{}); err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, map[string]string{
|
|
|
|
"message": "Error during minio#MakeBucket",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
new_buckets = append(new_buckets, bucket)
|
|
|
|
}
|
2023-04-24 16:30:06 -04:00
|
|
|
var message string
|
|
|
|
if len(new_buckets) == 0 {
|
|
|
|
message = "All buckets already exist"
|
|
|
|
|
|
|
|
} else {
|
|
|
|
message = "Buckets successfully created"
|
|
|
|
}
|
2023-04-21 21:34:18 -04:00
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, map[string]interface{}{
|
2023-04-24 16:30:06 -04:00
|
|
|
"message": message,
|
2023-04-21 21:34:18 -04:00
|
|
|
"buckets": new_buckets,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleV1BucketList affiche les buckets permis par server.documents.buckets, qui existent.
|
|
|
|
func handleV1BucketList(c echo.Context) error {
|
2023-04-21 16:41:50 -04:00
|
|
|
documents_buckets := viper.GetStringSlice("server.documents.buckets")
|
|
|
|
documents_endpoint := viper.GetString("server.documents.endpoint")
|
|
|
|
documents_access_key_id := viper.GetString("server.documents.access_key_id")
|
|
|
|
documents_secret_access_key := viper.GetString("server.documents.secret_access_key")
|
|
|
|
documents_use_ssl := viper.GetBool("server.documents.use_ssl")
|
|
|
|
|
2023-04-21 18:51:20 -04:00
|
|
|
// Initialize minio client object
|
|
|
|
client, err := minio.New(documents_endpoint, &minio.Options{
|
|
|
|
Creds: credentials.NewStaticV4(documents_access_key_id, documents_secret_access_key, ""),
|
|
|
|
Secure: documents_use_ssl,
|
|
|
|
})
|
2023-04-21 16:41:50 -04:00
|
|
|
if err != nil {
|
2023-04-21 18:51:20 -04:00
|
|
|
return c.JSON(http.StatusInternalServerError, map[string]string{
|
|
|
|
"message": "Error during minio#New",
|
|
|
|
})
|
2023-04-21 16:41:50 -04:00
|
|
|
}
|
|
|
|
|
2023-04-21 18:36:20 -04:00
|
|
|
var buckets []string
|
2023-04-21 16:41:50 -04:00
|
|
|
|
2023-04-21 17:11:06 -04:00
|
|
|
for _, bucket_name := range documents_buckets {
|
2023-04-21 18:51:20 -04:00
|
|
|
exists, err := client.BucketExists(context.Background(), bucket_name)
|
2023-04-21 16:41:50 -04:00
|
|
|
if err != nil {
|
2023-04-21 18:51:20 -04:00
|
|
|
return c.JSON(http.StatusInternalServerError, "Error during minio#BucketExists")
|
2023-04-21 16:41:50 -04:00
|
|
|
}
|
2023-04-21 18:36:20 -04:00
|
|
|
|
|
|
|
if exists {
|
|
|
|
buckets = append(buckets, bucket_name)
|
|
|
|
}
|
2023-04-21 16:41:50 -04:00
|
|
|
}
|
|
|
|
|
2023-04-21 18:36:20 -04:00
|
|
|
return c.JSON(http.StatusOK, buckets)
|
2023-04-21 16:41:50 -04:00
|
|
|
}
|
|
|
|
|
2023-04-21 21:34:18 -04:00
|
|
|
func handleV1BucketRead(c echo.Context) error {
|
2023-04-26 15:00:17 -04:00
|
|
|
documents_buckets := viper.GetStringSlice("server.documents.buckets")
|
2023-04-21 21:34:18 -04:00
|
|
|
documents_endpoint := viper.GetString("server.documents.endpoint")
|
|
|
|
documents_access_key_id := viper.GetString("server.documents.access_key_id")
|
|
|
|
documents_secret_access_key := viper.GetString("server.documents.secret_access_key")
|
|
|
|
documents_use_ssl := viper.GetBool("server.documents.use_ssl")
|
|
|
|
|
|
|
|
bucket := c.Param("bucket")
|
|
|
|
|
2023-04-26 15:00:17 -04:00
|
|
|
allowed := false
|
|
|
|
for _, bucket_allowed := range documents_buckets {
|
|
|
|
if bucket == bucket_allowed {
|
|
|
|
allowed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !allowed {
|
|
|
|
/*
|
|
|
|
return c.JSON(http.StatusBadRequest, map[string]string{
|
|
|
|
"message": "Bucket is not allowed in server.documents.buckets",
|
|
|
|
})
|
|
|
|
*/
|
|
|
|
return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"})
|
|
|
|
}
|
|
|
|
|
2023-04-21 21:34:18 -04:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// Initialize minio client object
|
|
|
|
client, err := minio.New(documents_endpoint, &minio.Options{
|
|
|
|
Creds: credentials.NewStaticV4(documents_access_key_id, documents_secret_access_key, ""),
|
|
|
|
Secure: documents_use_ssl,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, map[string]string{
|
|
|
|
"message": "Error during minio#New",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
exists, err := client.BucketExists(ctx, bucket)
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, "Error during minio#BucketExists")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
2023-04-24 17:04:02 -04:00
|
|
|
return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"})
|
2023-04-21 21:34:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var keys []string
|
|
|
|
|
|
|
|
objectCh := client.ListObjects(ctx, bucket, minio.ListObjectsOptions{})
|
|
|
|
for object := range objectCh {
|
|
|
|
if object.Err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, map[string]string{
|
|
|
|
"message": "Error during minio#ListObjects",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
keys = append(keys, object.Key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, keys)
|
|
|
|
}
|
|
|
|
|
2023-04-24 18:40:17 -04:00
|
|
|
// handleV1DocumentCreate permet d'ajouter un object dans un bucket, par multipart/form-data
|
2023-04-24 17:05:18 -04:00
|
|
|
func handleV1DocumentCreate(c echo.Context) error {
|
2023-04-26 15:00:17 -04:00
|
|
|
documents_buckets := viper.GetStringSlice("server.documents.buckets")
|
2023-04-24 18:29:05 -04:00
|
|
|
documents_endpoint := viper.GetString("server.documents.endpoint")
|
|
|
|
documents_access_key_id := viper.GetString("server.documents.access_key_id")
|
|
|
|
documents_secret_access_key := viper.GetString("server.documents.secret_access_key")
|
|
|
|
documents_use_ssl := viper.GetBool("server.documents.use_ssl")
|
|
|
|
|
|
|
|
bucket := c.Param("bucket")
|
|
|
|
|
|
|
|
form_file, err := c.FormFile("file")
|
|
|
|
if err != nil {
|
2023-04-28 15:57:09 -04:00
|
|
|
log.Println(err)
|
|
|
|
return c.JSON(http.StatusBadRequest, map[string]interface{}{
|
|
|
|
"message": "Error during handleV1DocumentCreate's echo#Context.FormFile",
|
|
|
|
"error": err,
|
|
|
|
})
|
2023-04-24 18:29:05 -04:00
|
|
|
}
|
|
|
|
|
2023-04-26 15:00:17 -04:00
|
|
|
allowed := false
|
|
|
|
for _, bucket_allowed := range documents_buckets {
|
|
|
|
if bucket == bucket_allowed {
|
|
|
|
allowed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !allowed {
|
|
|
|
/*
|
|
|
|
return c.JSON(http.StatusBadRequest, map[string]string{
|
|
|
|
"message": "Bucket is not allowed in server.documents.buckets",
|
|
|
|
})
|
|
|
|
*/
|
|
|
|
return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"})
|
|
|
|
}
|
|
|
|
|
2023-04-24 18:29:05 -04:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// Initialize minio client object
|
|
|
|
client, err := minio.New(documents_endpoint, &minio.Options{
|
|
|
|
Creds: credentials.NewStaticV4(documents_access_key_id, documents_secret_access_key, ""),
|
|
|
|
Secure: documents_use_ssl,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, map[string]string{
|
|
|
|
"message": "Error during minio#New",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
src, err := form_file.Open()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer src.Close()
|
|
|
|
|
|
|
|
info, err := client.PutObject(ctx, bucket, form_file.Filename, src, form_file.Size, minio.PutObjectOptions{
|
|
|
|
ContentType: form_file.Header.Get("Content-Type"),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, map[string]string{
|
|
|
|
"message": "Error during minio#PutObject",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, map[string]interface{}{
|
|
|
|
"message": "ok",
|
|
|
|
"info": map[string]interface{}{
|
|
|
|
"bucket": info.Bucket,
|
|
|
|
"key": info.Key,
|
|
|
|
"size": info.Size,
|
|
|
|
},
|
2023-04-24 17:05:18 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-04-24 18:40:17 -04:00
|
|
|
// handleV1DocumentRead permet de lire le contenu d'un fichier et protentiellement de le télécharger
|
2023-04-24 17:05:18 -04:00
|
|
|
func handleV1DocumentRead(c echo.Context) error {
|
2023-04-26 15:00:17 -04:00
|
|
|
documents_buckets := viper.GetStringSlice("server.documents.buckets")
|
2023-04-26 13:23:53 -04:00
|
|
|
documents_endpoint := viper.GetString("server.documents.endpoint")
|
|
|
|
documents_access_key_id := viper.GetString("server.documents.access_key_id")
|
|
|
|
documents_secret_access_key := viper.GetString("server.documents.secret_access_key")
|
|
|
|
documents_use_ssl := viper.GetBool("server.documents.use_ssl")
|
|
|
|
|
|
|
|
bucket := c.Param("bucket")
|
|
|
|
document := c.Param("document")
|
|
|
|
|
2023-04-26 15:00:17 -04:00
|
|
|
allowed := false
|
|
|
|
for _, bucket_allowed := range documents_buckets {
|
|
|
|
if bucket == bucket_allowed {
|
|
|
|
allowed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !allowed {
|
|
|
|
/*
|
|
|
|
return c.JSON(http.StatusBadRequest, map[string]string{
|
|
|
|
"message": "Bucket is not allowed in server.documents.buckets",
|
|
|
|
})
|
|
|
|
*/
|
|
|
|
return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"})
|
|
|
|
}
|
|
|
|
|
2023-04-26 13:23:53 -04:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// Initialize minio client object
|
|
|
|
client, err := minio.New(documents_endpoint, &minio.Options{
|
|
|
|
Creds: credentials.NewStaticV4(documents_access_key_id, documents_secret_access_key, ""),
|
|
|
|
Secure: documents_use_ssl,
|
2023-04-24 17:05:18 -04:00
|
|
|
})
|
2023-04-26 13:23:53 -04:00
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, map[string]string{
|
|
|
|
"message": "Error during minio#New",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket_exists, err := client.BucketExists(ctx, bucket)
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, "Error during minio#BucketExists")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bucket_exists {
|
|
|
|
return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"})
|
|
|
|
}
|
|
|
|
|
|
|
|
document_info, err := client.StatObject(ctx, bucket, document, minio.StatObjectOptions{})
|
2023-04-26 15:00:17 -04:00
|
|
|
|
2023-04-26 13:23:53 -04:00
|
|
|
if err != nil {
|
2023-04-26 15:00:17 -04:00
|
|
|
if err.Error() == "The specified key does not exist." {
|
|
|
|
|
|
|
|
return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"})
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusInternalServerError, map[string]interface{}{
|
2023-04-26 13:23:53 -04:00
|
|
|
"message": "Error during minio#StatObject",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = document_info
|
|
|
|
|
|
|
|
document_object, err := client.GetObject(ctx, bucket, document, minio.GetObjectOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, map[string]string{
|
|
|
|
"message": "Error during minio#GetObject",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
defer document_object.Close()
|
|
|
|
|
|
|
|
return c.Stream(http.StatusOK, document_info.ContentType, document_object)
|
2023-04-24 17:05:18 -04:00
|
|
|
}
|
|
|
|
|
2023-04-24 18:40:17 -04:00
|
|
|
// handleV1DocumentUpdate permet de mettre à jour certains champs d'un object, comme le Content-Type ou le Filename
|
2023-04-24 17:05:18 -04:00
|
|
|
func handleV1DocumentUpdate(c echo.Context) error {
|
|
|
|
return c.JSON(http.StatusNotImplemented, map[string]string{
|
|
|
|
"message": "Not Implemented",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-04-24 18:40:17 -04:00
|
|
|
// handleV1DocumentDelete permet de supprimer un object
|
2023-04-24 17:05:18 -04:00
|
|
|
func handleV1DocumentDelete(c echo.Context) error {
|
2023-04-26 15:00:17 -04:00
|
|
|
documents_buckets := viper.GetStringSlice("server.documents.buckets")
|
2023-04-24 22:34:57 -04:00
|
|
|
documents_endpoint := viper.GetString("server.documents.endpoint")
|
|
|
|
documents_access_key_id := viper.GetString("server.documents.access_key_id")
|
|
|
|
documents_secret_access_key := viper.GetString("server.documents.secret_access_key")
|
|
|
|
documents_use_ssl := viper.GetBool("server.documents.use_ssl")
|
|
|
|
|
|
|
|
bucket := c.Param("bucket")
|
|
|
|
document := c.Param("document")
|
|
|
|
|
2023-04-26 15:00:17 -04:00
|
|
|
allowed := false
|
|
|
|
for _, bucket_allowed := range documents_buckets {
|
|
|
|
if bucket == bucket_allowed {
|
|
|
|
allowed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !allowed {
|
|
|
|
/*
|
|
|
|
return c.JSON(http.StatusBadRequest, map[string]string{
|
|
|
|
"message": "Bucket is not allowed in server.documents.buckets",
|
|
|
|
})
|
|
|
|
*/
|
|
|
|
return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"})
|
|
|
|
}
|
|
|
|
|
2023-04-24 22:34:57 -04:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// Initialize minio client object
|
|
|
|
client, err := minio.New(documents_endpoint, &minio.Options{
|
|
|
|
Creds: credentials.NewStaticV4(documents_access_key_id, documents_secret_access_key, ""),
|
|
|
|
Secure: documents_use_ssl,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, map[string]string{
|
|
|
|
"message": "Error during minio#New",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket_exists, err := client.BucketExists(ctx, bucket)
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, "Error during minio#BucketExists")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bucket_exists {
|
|
|
|
return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"})
|
|
|
|
}
|
|
|
|
|
|
|
|
document_info, err := client.StatObject(ctx, bucket, document, minio.StatObjectOptions{})
|
|
|
|
if err != nil {
|
2023-04-26 15:00:17 -04:00
|
|
|
if err.Error() == "The specified key does not exist." {
|
|
|
|
|
|
|
|
return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"})
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusInternalServerError, map[string]interface{}{
|
2023-04-24 22:34:57 -04:00
|
|
|
"message": "Error during minio#StatObject",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO Add error validation
|
|
|
|
_ = document_info
|
|
|
|
|
|
|
|
err = client.RemoveObject(ctx, bucket, document, minio.RemoveObjectOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, map[string]string{
|
|
|
|
"message": "Error during minio#RemoveObject",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, map[string]string{
|
|
|
|
"message": "Bucket deleted",
|
2023-04-24 17:05:18 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-03-21 18:37:51 -04:00
|
|
|
// HTML Handlers
|
|
|
|
|
|
|
|
func handleIndex(c echo.Context) error {
|
|
|
|
return c.Render(http.StatusOK, "index-html", nil)
|
|
|
|
}
|
2023-03-21 19:59:41 -04:00
|
|
|
|
2023-03-21 20:29:06 -04:00
|
|
|
func handleAPropos(c echo.Context) error {
|
2023-03-24 20:05:52 -04:00
|
|
|
return c.Render(http.StatusOK, "a-propos-html", nil)
|
2023-03-21 20:29:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleActualite(c echo.Context) error {
|
2023-03-24 20:05:52 -04:00
|
|
|
return c.Render(http.StatusOK, "actualite-html", nil)
|
2023-03-21 20:29:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleActualiteArticle(c echo.Context) error {
|
|
|
|
article := c.Param("article")
|
|
|
|
return c.String(http.StatusOK, fmt.Sprintf("Article: %s", article))
|
|
|
|
}
|
2023-04-26 15:15:43 -04:00
|
|
|
|
2023-03-21 20:29:06 -04:00
|
|
|
func handleVieEtudiante(c echo.Context) error {
|
2023-03-24 20:05:52 -04:00
|
|
|
return c.Render(http.StatusOK, "vie-etudiante-html", nil)
|
2023-03-21 20:29:06 -04:00
|
|
|
}
|
2023-04-26 15:15:43 -04:00
|
|
|
|
2023-03-21 20:29:06 -04:00
|
|
|
func handleVieEtudianteOrganisme(c echo.Context) error {
|
|
|
|
organisme := c.Param("organisme")
|
|
|
|
return c.String(http.StatusOK, fmt.Sprintf("Organisme: %s", organisme))
|
|
|
|
}
|
2023-04-26 15:15:43 -04:00
|
|
|
|
2023-03-21 20:29:06 -04:00
|
|
|
func handleDocumentation(c echo.Context) error {
|
2023-04-26 16:51:13 -04:00
|
|
|
client, err := api.New("http", "localhost", viper.GetInt("server.port"), api.APIOptions{
|
|
|
|
KeyAuth: viper.GetBool("server.api.auth"),
|
|
|
|
Key: viper.GetString("server.api.key"),
|
|
|
|
})
|
|
|
|
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 []string
|
|
|
|
|
|
|
|
err = json.Unmarshal(result, &buckets)
|
|
|
|
if err != nil {
|
|
|
|
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Bucket struct {
|
|
|
|
Name string
|
|
|
|
Documents []string
|
|
|
|
}
|
|
|
|
|
|
|
|
var data []Bucket
|
|
|
|
|
|
|
|
for _, bucket := range buckets {
|
|
|
|
result, 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(result, &documents)
|
|
|
|
if err != nil {
|
|
|
|
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
data = append(data, Bucket{
|
|
|
|
Name: bucket,
|
|
|
|
Documents: documents,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Render(http.StatusOK, "documentation-html", data)
|
2023-03-21 20:29:06 -04:00
|
|
|
}
|
2023-04-26 15:15:43 -04:00
|
|
|
|
2023-03-24 18:21:22 -04:00
|
|
|
func handleFormulaires(c echo.Context) error {
|
2023-03-24 20:05:52 -04:00
|
|
|
return c.Render(http.StatusOK, "formulaires-html", nil)
|
2023-03-21 20:29:06 -04:00
|
|
|
}
|
|
|
|
|
2023-04-26 18:27:58 -04:00
|
|
|
func handlePublicDocumentation(c echo.Context) error {
|
|
|
|
client, err := api.New("http", "localhost", viper.GetInt("server.port"), api.APIOptions{
|
|
|
|
KeyAuth: viper.GetBool("server.api.auth"),
|
|
|
|
Key: viper.GetString("server.api.key"),
|
|
|
|
})
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-04-26 19:15:22 -04:00
|
|
|
func handleAdmin(c echo.Context) error {
|
2023-04-26 19:28:20 -04:00
|
|
|
return c.Render(http.StatusOK, "admin-html", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleAdminDocumentsUpload(c echo.Context) error {
|
|
|
|
return c.Render(http.StatusOK, "admin-upload-html", nil)
|
2023-04-26 19:15:22 -04:00
|
|
|
}
|
|
|
|
|
2023-04-26 19:43:43 -04:00
|
|
|
func handleAdminDocumentsUploadPOST(c echo.Context) error {
|
2023-04-28 15:57:09 -04:00
|
|
|
client, err := api.New("http", "localhost", viper.GetInt("server.port"), api.APIOptions{
|
|
|
|
KeyAuth: viper.GetBool("server.api.auth"),
|
|
|
|
Key: viper.GetString("server.api.key"),
|
|
|
|
BasicAuth: viper.GetBool("server.admin.auth"),
|
|
|
|
Username: viper.GetString("server.admin.username"),
|
|
|
|
Password: viper.GetString("server.admin.password"),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"})
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket := c.FormValue("bucket")
|
|
|
|
|
|
|
|
document, err := c.FormFile("document")
|
|
|
|
if err != nil {
|
|
|
|
//return c.JSON(http.StatusBadRequest, map[string]string{"message": "Error during file parse"})
|
|
|
|
return c.JSON(http.StatusNotFound, map[string]string{"message": "Error during echo#Context.FormFile", "error": err.Error()})
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
file, err := document.Open()
|
|
|
|
if err != nil {
|
|
|
|
//return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"})
|
|
|
|
return c.JSON(http.StatusNotFound, map[string]string{"message": "Error during file.Open()", "error": err.Error()})
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
*/
|
|
|
|
|
|
|
|
response, err := client.UploadDocument(bucket, document)
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusNotFound, map[string]string{"message": "Error duing api#client.UploadDocument", "error": err.Error()})
|
|
|
|
//return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"})
|
2023-04-26 19:43:43 -04:00
|
|
|
}
|
|
|
|
|
2023-04-28 15:57:09 -04:00
|
|
|
return c.Render(http.StatusOK, "admin-upload-html", struct{ Message string }{Message: response})
|
2023-04-26 19:43:43 -04:00
|
|
|
}
|
|
|
|
|
2023-03-21 19:59:41 -04:00
|
|
|
// CSS Handlers
|
|
|
|
|
|
|
|
func handleStaticCSSIndex(c echo.Context) error {
|
|
|
|
// TODO Ajouter gestion d'erreurs
|
|
|
|
data, _ := embedFS.ReadFile("css/index.css")
|
|
|
|
return c.Blob(http.StatusOK, "text/css", data)
|
|
|
|
}
|
2023-03-24 20:02:46 -04:00
|
|
|
|
|
|
|
func handleStaticCSSGeneral(c echo.Context) error {
|
|
|
|
// TODO Ajouter gestion d'erreurs
|
|
|
|
data, _ := embedFS.ReadFile("css/general.css")
|
|
|
|
return c.Blob(http.StatusOK, "text/css", data)
|
|
|
|
}
|