From 451ccd1852435085e063a3c195851836e72aa57b Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Thu, 22 Aug 2024 13:22:11 -0400 Subject: [PATCH] merge: cmd dans cmd.go Aussi fix example dans config.go --- cmd/server.go => cmd.go | 177 ++++++++++++++++++++++++++++++---------- cmd/config.go | 44 ---------- cmd/root.go | 63 -------------- cmd/version.go | 21 ----- config.go | 2 +- 5 files changed, 135 insertions(+), 172 deletions(-) rename cmd/server.go => cmd.go (60%) delete mode 100644 cmd/config.go delete mode 100644 cmd/root.go delete mode 100644 cmd/version.go diff --git a/cmd/server.go b/cmd.go similarity index 60% rename from cmd/server.go rename to cmd.go index faf6dba..f627dbf 100644 --- a/cmd/server.go +++ b/cmd.go @@ -1,41 +1,118 @@ /* -Copyright © 2023 AGECEM +Copyright © 2023-2024 AGECEM */ -package cmd +package main import ( "crypto/subtle" - "fmt" - "log" - "embed" - "html/template" + "encoding/json" + "fmt" "io" + "log" "net/http" + "os" + "strings" + "text/template" "codeberg.org/vlbeaudoin/pave/v2" "codeberg.org/vlbeaudoin/serpents" - "github.com/spf13/cobra" - "github.com/spf13/viper" - "git.agecem.com/agecem/agecem-org/api" - "git.agecem.com/agecem/agecem-org/apihandler" - "git.agecem.com/agecem/agecem-org/apirequest" - "git.agecem.com/agecem/agecem-org/apiresponse" - "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/templates" - "git.agecem.com/agecem/agecem-org/webhandler" + "git.agecem.com/agecem/agecem-org/version" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" + "github.com/spf13/cobra" + "github.com/spf13/viper" ) +// configCmd represents the config command +var configCmd = &cobra.Command{ + Use: "config", + Short: "Print the config to stdout in indented JSON format", + Run: func(cmd *cobra.Command, args []string) { + var cfg Config + + if err := viper.Unmarshal(&cfg); err != nil { + log.Fatal(err) + } + + printConfig(cfg) + }, +} + +func init() { + rootCmd.AddCommand(configCmd) +} + +func printConfig(config Config) error { + buf, err := json.MarshalIndent(config, "", " ") + if err != nil { + return err + } + + fmt.Println(string(buf)) + + return nil +} + +var cfgFile string + +// rootCmd represents the base command when called without any subcommands +var rootCmd = &cobra.Command{ + Use: "agecem-org", + Short: "Application du site web de l'AGECEM", + Long: "Application du site web de l'AGECEM, l'Association Générale Étudiante du Cégep Édouard-Montpetit.", +} + +// Execute adds all child commands to the root command and sets flags appropriately. +// This is called by main.main(). It only needs to happen once to the rootCmd. +func Execute() { + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} + +func init() { + cobra.OnInitialize(initConfig) + + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.agecem-org.yaml)") +} + +// initConfig reads in config file and ENV variables if set. +func initConfig() { + if cfgFile != "" { + // Use config file from the flag. + viper.SetConfigFile(cfgFile) + } else { + // Find home directory. + home, err := os.UserHomeDir() + cobra.CheckErr(err) + + // Search config in home directory with name ".agecem-org" (without extension). + viper.AddConfigPath(home) + viper.SetConfigType("yaml") + viper.SetConfigName(".agecem-org") + } + + viper.SetEnvPrefix("AGECEM_ORG") + viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) + viper.AutomaticEnv() // read in environment variables that match + + // If a config file is found, read it in. + if err := viper.ReadInConfig(); err == nil { + fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) + } +} + type Template struct { templates *template.Template } -var cfg config.Config +var cfg Config var ( publicFS embed.FS @@ -210,62 +287,63 @@ func RunServer() { p := pave.New() - v1Handler := apihandler.V1Handler{ + v1Handler := V1Handler{ Config: cfg, MediaClient: mediaClient, Pave: &p, } - groupV1.GET("", v1Handler.V1GET) + groupV1.GET("", v1Handler.ListRoutes) if err := pave.EchoRegister[ - apirequest.V1SeedPOST, - apiresponse.V1SeedPOST](groupV1, &p, "/v1", http.MethodPost, "/seed", "Créer buckets manquants définis dans `server.documents.buckets`", "V1SeedPOST", v1Handler.V1SeedPOST); err != nil { + ExecuteSeedRequest, + ExecuteSeedResponse](groupV1, &p, "/v1", http.MethodPost, "/seed", "Créer buckets manquants définis dans `server.documents.buckets`", "ExecuteSeed", v1Handler.ExecuteSeed); err != nil { log.Fatal(err) } if err := pave.EchoRegister[ - apirequest.V1DocumentKeyPUT, - apiresponse.V1DocumentKeyPUT](groupV1, &p, "/v1", http.MethodPut, "/bucket/:bucket/:document/key", "Renommer un document", "V1DocumentKeyPUT", v1Handler.V1DocumentKeyPUT); err != nil { + UpdateDocumentKeyRequest, + UpdateDocumentKeyResponse](groupV1, &p, "/v1", http.MethodPut, "/bucket/:bucket/:document/key", "Renommer un document", "UpdateDocumentKey", v1Handler.UpdateDocumentKey); err != nil { log.Fatal(err) } if err := pave.EchoRegister[ - apirequest.V1SpecGET, - apiresponse.V1SpecGET](groupV1, &p, "/v1", http.MethodGet, "/spec", apihandler.DescriptionV1SpecGET, "V1SpecGET", v1Handler.V1SpecGET); err != nil { + ReadSpecRequest, + ReadSpecResponse](groupV1, &p, "/v1", http.MethodGet, "/spec", DescriptionV1SpecGET, "SpecRead", v1Handler.ReadSpec); err != nil { log.Fatal(err) } if err := pave.EchoRegister[ - apirequest.V1BucketsGET, - apiresponse.V1BucketsGET](groupV1, &p, "/v1", http.MethodGet, "/bucket", "List buckets", "V1BucketsGET", v1Handler.V1BucketsGET); err != nil { + ListBucketsRequest, + ListBucketsResponse](groupV1, &p, "/v1", http.MethodGet, "/bucket", "List buckets", "ListBuckets", v1Handler.ListBuckets); err != nil { log.Fatal(err) } if err := pave.EchoRegister[ - apirequest.V1BucketGET, - apiresponse.V1BucketGET](groupV1, &p, "/v1", http.MethodGet, "/bucket/:bucket", "Read bucket content", "V1BucketGET", v1Handler.V1BucketGET); err != nil { + ReadBucketRequest, + ReadBucketResponse](groupV1, &p, "/v1", http.MethodGet, "/bucket/:bucket", "Read bucket content", "ReadBucket", v1Handler.ReadBucket); err != nil { log.Fatal(err) } if err := pave.EchoRegister[ - apirequest.CreateDocumentsResponse, - apiresponse.V1DocumentsPOST](groupV1, &p, "/v1", http.MethodPost, "/bucket/:bucket/many", "Upload documents to specified bucket", "V1DocumentsPOST", v1Handler.V1DocumentsPOST); err != nil { + CreateDocumentsRequest, + CreateDocumentsResponse](groupV1, &p, "/v1", http.MethodPost, "/bucket/:bucket/many", "Upload documents to specified bucket", "CreateDocuments", v1Handler.CreateDocuments); err != nil { log.Fatal(err) } if err := pave.EchoRegister[ - apirequest.V1DocumentPOST, - apiresponse.V1DocumentPOST](groupV1, &p, "/v1", http.MethodPost, "/bucket/:bucket", "Upload document to specified bucket", "V1DocumentPOST", v1Handler.V1DocumentPOST); err != nil { + CreateDocumentRequest, + CreateDocumentResponse](groupV1, &p, "/v1", http.MethodPost, "/bucket/:bucket", "Upload document to specified bucket", "CreateDocument", v1Handler.CreateDocument); err != nil { log.Fatal(err) } - groupV1.GET("/bucket/:bucket/:document", v1Handler.V1DocumentGET) + // Do not move to pave, uses echo.Stream instead of echo.JSON + groupV1.GET("/bucket/:bucket/:document", v1Handler.ReadDocument) if err := pave.EchoRegister[ - apirequest.V1DocumentDELETE, - apiresponse.V1DocumentDELETE](groupV1, &p, "/v1", http.MethodDelete, "/bucket/:bucket/:document", "Delete document in specified bucket", "V1DocumentDELETE", v1Handler.V1DocumentDELETE); err != nil { + DeleteDocumentRequest, + DeleteDocumentResponse](groupV1, &p, "/v1", http.MethodDelete, "/bucket/:bucket/:document", "Delete document in specified bucket", "DeleteDocument", v1Handler.DeleteDocument); err != nil { log.Fatal(err) } @@ -278,25 +356,25 @@ func RunServer() { log.Fatal(err) } - webHandler := webhandler.WebHandler{ + webHandler := WebHandler{ ApiClient: apiClient, } - e.GET("/", webhandler.HandleIndex) + e.GET("/", HandleIndex) - //e.GET("/a-propos", webhandler.HandleAPropos) + //e.GET("/a-propos", HandleAPropos) - //e.GET("/actualite", webhandler.HandleActualite) + //e.GET("/actualite", HandleActualite) - //e.GET("/actualite/:article", webhandler.HandleActualiteArticle) + //e.GET("/actualite/:article", HandleActualiteArticle) - e.GET("/vie-etudiante", webhandler.HandleVieEtudiante) + e.GET("/vie-etudiante", HandleVieEtudiante) - e.GET("/vie-etudiante/:organisme", webhandler.HandleVieEtudianteOrganisme) + e.GET("/vie-etudiante/:organisme", HandleVieEtudianteOrganisme) e.GET("/documentation", webHandler.HandleDocumentation) - e.GET("/formulaires", webhandler.HandleFormulaires) + e.GET("/formulaires", HandleFormulaires) // Public Routes @@ -304,7 +382,7 @@ func RunServer() { // Admin Routes - groupAdmin.GET("", webhandler.HandleAdmin) + groupAdmin.GET("", HandleAdmin) groupAdmin.GET("/documents/upload", webHandler.HandleAdminDocumentsUpload) @@ -317,3 +395,16 @@ func RunServer() { func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error { return t.templates.ExecuteTemplate(w, name, data) } + +// versionCmd represents the version command +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Print version registered at build time", + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("agecem-org", version.Version()) + }, +} + +func init() { + rootCmd.AddCommand(versionCmd) +} diff --git a/cmd/config.go b/cmd/config.go deleted file mode 100644 index a8d41f0..0000000 --- a/cmd/config.go +++ /dev/null @@ -1,44 +0,0 @@ -/* -Copyright © 2023 AGECEM -*/ -package cmd - -import ( - "encoding/json" - "fmt" - "log" - - "git.agecem.com/agecem/agecem-org/config" - "github.com/spf13/cobra" - "github.com/spf13/viper" -) - -// configCmd represents the config command -var configCmd = &cobra.Command{ - Use: "config", - Short: "Print the config to stdout in indented JSON format", - Run: func(cmd *cobra.Command, args []string) { - var cfg config.Config - - if err := viper.Unmarshal(&cfg); err != nil { - log.Fatal(err) - } - - printConfig(cfg) - }, -} - -func init() { - rootCmd.AddCommand(configCmd) -} - -func printConfig(config config.Config) error { - buf, err := json.MarshalIndent(config, "", " ") - if err != nil { - return err - } - - fmt.Println(string(buf)) - - return nil -} diff --git a/cmd/root.go b/cmd/root.go deleted file mode 100644 index 94917bf..0000000 --- a/cmd/root.go +++ /dev/null @@ -1,63 +0,0 @@ -/* -Copyright © 2023 AGECEM -*/ -package cmd - -import ( - "fmt" - "os" - "strings" - - "github.com/spf13/cobra" - "github.com/spf13/viper" -) - -var cfgFile string - -// rootCmd represents the base command when called without any subcommands -var rootCmd = &cobra.Command{ - Use: "agecem-org", - Short: "Application du site web de l'AGECEM", - Long: "Application du site web de l'AGECEM, l'Association Générale Étudiante du Cégep Édouard-Montpetit.", -} - -// Execute adds all child commands to the root command and sets flags appropriately. -// This is called by main.main(). It only needs to happen once to the rootCmd. -func Execute() { - err := rootCmd.Execute() - if err != nil { - os.Exit(1) - } -} - -func init() { - cobra.OnInitialize(initConfig) - - rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.agecem-org.yaml)") -} - -// initConfig reads in config file and ENV variables if set. -func initConfig() { - if cfgFile != "" { - // Use config file from the flag. - viper.SetConfigFile(cfgFile) - } else { - // Find home directory. - home, err := os.UserHomeDir() - cobra.CheckErr(err) - - // Search config in home directory with name ".agecem-org" (without extension). - viper.AddConfigPath(home) - viper.SetConfigType("yaml") - viper.SetConfigName(".agecem-org") - } - - viper.SetEnvPrefix("AGECEM_ORG") - viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) - viper.AutomaticEnv() // read in environment variables that match - - // If a config file is found, read it in. - if err := viper.ReadInConfig(); err == nil { - fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) - } -} diff --git a/cmd/version.go b/cmd/version.go deleted file mode 100644 index e2b73c2..0000000 --- a/cmd/version.go +++ /dev/null @@ -1,21 +0,0 @@ -package cmd - -import ( - "fmt" - - "git.agecem.com/agecem/agecem-org/version" - "github.com/spf13/cobra" -) - -// versionCmd represents the version command -var versionCmd = &cobra.Command{ - Use: "version", - Short: "Print version registered at build time", - Run: func(cmd *cobra.Command, args []string) { - fmt.Println("agecem-org", version.Version()) - }, -} - -func init() { - rootCmd.AddCommand(versionCmd) -} diff --git a/config.go b/config.go index f426dde..4d80229 100644 --- a/config.go +++ b/config.go @@ -6,7 +6,7 @@ Permet de contenir la configuration obtenue par cobra/viper Example d'utilisation sans error handling: ``` -var cfg config.Config +var cfg Config viper.Unmarshal(&cfg) ```