Refactor app sous v4/
This commit is contained in:
parent
c74e093a3b
commit
3ccebd8cfb
19 changed files with 1372 additions and 0 deletions
56
v4/cmd/root.go
Normal file
56
v4/cmd/root.go
Normal file
|
@ -0,0 +1,56 @@
|
|||
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: "bottin",
|
||||
Short: "Application de gestion de distribution d'agendas",
|
||||
}
|
||||
|
||||
// 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/.bottin.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 ".bottin" (without extension).
|
||||
viper.AddConfigPath(home)
|
||||
viper.SetConfigType("yaml")
|
||||
viper.SetConfigName(".bottin")
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
}
|
106
v4/cmd/server.go
Normal file
106
v4/cmd/server.go
Normal file
|
@ -0,0 +1,106 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"crypto/subtle"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"git.agecem.com/agecem/bottin/v4/data"
|
||||
"git.agecem.com/agecem/bottin/v4/handlers"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var (
|
||||
apiPort int
|
||||
apiKey string
|
||||
)
|
||||
|
||||
// serverCmd represents the server command
|
||||
var serverCmd = &cobra.Command{
|
||||
Use: "server",
|
||||
Short: "Démarrer le serveur API",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
e := echo.New()
|
||||
|
||||
// Middlewares
|
||||
|
||||
e.Pre(middleware.AddTrailingSlash())
|
||||
|
||||
if apiKey != "" {
|
||||
e.Use(middleware.KeyAuth(func(key string, c echo.Context) (bool, error) {
|
||||
return subtle.ConstantTimeCompare([]byte(key), []byte(apiKey)) == 1, nil
|
||||
}))
|
||||
}
|
||||
|
||||
// Routes
|
||||
|
||||
e.GET("/v4/", handlers.GetV4)
|
||||
|
||||
e.POST("/v4/seed/", handlers.PostSeed)
|
||||
|
||||
e.GET("/v4/membres/:membre_id/", handlers.ReadMembre)
|
||||
e.POST("/v4/membres/", handlers.PostMembres)
|
||||
|
||||
// Execution
|
||||
|
||||
connection := data.PostgresConnection{
|
||||
User: viper.GetString("db.user"),
|
||||
Password: viper.GetString("db.password"),
|
||||
Host: viper.GetString("db.host"),
|
||||
Database: viper.GetString("db.database"),
|
||||
Port: viper.GetInt("db.port"),
|
||||
}
|
||||
|
||||
client, err := data.NewDataClient(connection)
|
||||
if err != nil {
|
||||
log.Fatalf("Could not establish database connection.\n Error: %s\n", err)
|
||||
}
|
||||
|
||||
err = client.DB.Ping()
|
||||
if err != nil {
|
||||
log.Fatalf("Database was supposed to be ready but Ping() failed.\n Error: %s\n", err)
|
||||
}
|
||||
|
||||
client.DB.Close()
|
||||
|
||||
e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", apiPort)))
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(serverCmd)
|
||||
|
||||
// api.key
|
||||
serverCmd.Flags().StringVar(
|
||||
&apiKey, "api-key", "bottin",
|
||||
"API server key. Leave empty for no key auth. (config: 'api.key')")
|
||||
|
||||
// api.port
|
||||
serverCmd.Flags().IntVar(
|
||||
&apiPort, "api-port", 1312,
|
||||
"API server port (config:'api.port')")
|
||||
|
||||
// db.database
|
||||
serverCmd.Flags().String("db-database", "bottin", "Postgres database (config:'db.database')")
|
||||
viper.BindPFlag("db.database", serverCmd.Flags().Lookup("db-database"))
|
||||
|
||||
// db.host
|
||||
serverCmd.Flags().String("db-host", "", "Postgres host (config:'db.host')")
|
||||
viper.BindPFlag("db.host", serverCmd.Flags().Lookup("db-host"))
|
||||
|
||||
// db.password
|
||||
serverCmd.Flags().String("db-password", "", "Postgres password (config:'db.password')")
|
||||
viper.BindPFlag("db.password", serverCmd.Flags().Lookup("db-password"))
|
||||
|
||||
// db.port
|
||||
serverCmd.Flags().Int("db-port", 5432, "Postgres port (config:'db.port')")
|
||||
viper.BindPFlag("db.port", serverCmd.Flags().Lookup("db-port"))
|
||||
|
||||
// db.user
|
||||
serverCmd.Flags().String("db-user", "", "Postgres user (config:'db.user')")
|
||||
viper.BindPFlag("db.user", serverCmd.Flags().Lookup("db-user"))
|
||||
}
|
20
v4/cmd/web.go
Normal file
20
v4/cmd/web.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// webCmd represents the web command
|
||||
var webCmd = &cobra.Command{
|
||||
Use: "web",
|
||||
Short: "Démarrer le client web",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Println("web called")
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(webCmd)
|
||||
}
|
Reference in a new issue