change!: commandline tool from serpents/cobra/viper to couleuvre
Ajouter `config.Config` pour stocker valeurs de flags Migrer `cmd/` à couleuvre Ajouter `cmd.Cfg()` pour retourner une copie courante de la configuration. Cleanup dependencies avec `go get` et `go mod tidy` BREAKING: renommer flags BREAKING: déplacer `cmd.runServer` à `handlers.RunServer(cfg config.Config)` BREAKING: changer backend à couleuvre, qui gère différemment le commandline
This commit is contained in:
parent
cdcd10ba1e
commit
605206197a
9 changed files with 140 additions and 227 deletions
67
cmd/cmd.go
Normal file
67
cmd/cmd.go
Normal file
|
@ -0,0 +1,67 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"os"
|
||||
|
||||
"codeberg.org/vlbeaudoin/couleuvre"
|
||||
"git.agecem.com/agecem/babillard/config"
|
||||
"git.agecem.com/agecem/babillard/handlers"
|
||||
)
|
||||
|
||||
var cfg config.Config
|
||||
|
||||
func Cfg() config.Config { return cfg }
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&cfg.ServerContenuDir, ServerContenuDirName, ServerContenuDirDefault, ServerContenuDirDescription)
|
||||
flag.IntVar(&cfg.ServerPort, ServerPortName, ServerPortDefault, ServerPortDescription)
|
||||
}
|
||||
|
||||
const (
|
||||
ServerContenuDirName = "servercontenudir"
|
||||
ServerContenuDirDefault = "contenu"
|
||||
ServerContenuDirDescription = "Répertoire du contenu à exposer"
|
||||
|
||||
ServerPortName = "serverport"
|
||||
ServerPortDefault int = 8080
|
||||
ServerPortDescription = "Port réseau à utiliser pour le serveur"
|
||||
)
|
||||
|
||||
const (
|
||||
ServerCmdName = "server"
|
||||
ServerCmdDesc = "Démarrer le serveur web"
|
||||
)
|
||||
|
||||
func ServerCmdExecuter() error {
|
||||
handlers.RunServer(Cfg())
|
||||
return nil
|
||||
}
|
||||
|
||||
func Execute() error {
|
||||
app := couleuvre.NewApp("BABILLARD_", ".", "_")
|
||||
|
||||
if err := app.Parse(&cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := app.NewCommand(ServerCmdName, ServerCmdDesc, ServerCmdExecuter); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var commandName string
|
||||
if len(os.Args) > 1 {
|
||||
commandName = flag.Arg(0)
|
||||
}
|
||||
|
||||
cmd, err := app.ParseCommand(commandName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := cmd.Execute(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
72
cmd/root.go
72
cmd/root.go
|
@ -1,72 +0,0 @@
|
|||
/*
|
||||
Copyright © 2021 AGECEM
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"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: "babillard",
|
||||
Short: "Application web pour afficher une rotation de nouvelles sous forme d'images",
|
||||
}
|
||||
|
||||
// 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() {
|
||||
cobra.CheckErr(rootCmd.Execute())
|
||||
}
|
||||
|
||||
func init() {
|
||||
cobra.OnInitialize(initConfig)
|
||||
|
||||
rootCmd.PersistentFlags().StringVarP(&cfgFile,
|
||||
"config", "c",
|
||||
"", "fichier de config (default: /etc/babillard/babillard-config.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 {
|
||||
viper.AddConfigPath("/etc/babillard")
|
||||
viper.SetConfigType("yaml")
|
||||
viper.SetConfigName("babillard-config")
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
}
|
|
@ -1,72 +0,0 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"codeberg.org/vlbeaudoin/serpents"
|
||||
"git.agecem.com/agecem/babillard/handlers"
|
||||
"git.agecem.com/agecem/babillard/public"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// serverCmd represents the server command
|
||||
var serverCmd = &cobra.Command{
|
||||
Use: "server",
|
||||
Short: "Démarrer le serveur web",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
port := viper.GetInt("server.port")
|
||||
//static_dir := viper.GetString("server.static_dir")
|
||||
|
||||
runServer(port)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(serverCmd)
|
||||
declareFlags(serverCmd.Flags())
|
||||
}
|
||||
|
||||
func declareFlags(flagSet *pflag.FlagSet) {
|
||||
if err := serpents.IntP(flagSet, "server.port", "port", "p", 8080, "Port réseau à utiliser pour le serveur"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if err := serpents.String(flagSet, "server.contenu_dir", "contenu_dir", "contenu", "Répertoire du contenu à exposer"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func runServer(port int) {
|
||||
log.Print("[I] Starting webserver")
|
||||
|
||||
e := echo.New()
|
||||
|
||||
e.Pre(middleware.AddTrailingSlash())
|
||||
|
||||
e.Group("").Use(middleware.StaticWithConfig(middleware.StaticConfig{
|
||||
Root: "/html/",
|
||||
Filesystem: http.FS(public.HTMLFS()),
|
||||
}))
|
||||
e.Group("/public/css").Use(middleware.StaticWithConfig(middleware.StaticConfig{
|
||||
Root: "/css/",
|
||||
Filesystem: http.FS(public.CSSFS()),
|
||||
}))
|
||||
e.Group("/public/js").Use(middleware.StaticWithConfig(middleware.StaticConfig{
|
||||
Root: "/js/",
|
||||
Filesystem: http.FS(public.JSFS()),
|
||||
}))
|
||||
|
||||
groupAPI := e.Group("/api")
|
||||
|
||||
groupAPI.GET("/", handlers.HandleAPIShow)
|
||||
groupAPI.GET("/contenu/", handlers.HandleAPIContenuList)
|
||||
groupAPI.GET("/contenu/:filename/", handlers.HandleAPIContenuFile)
|
||||
|
||||
e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", port)))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue