début de réécriture pour v7
This commit is contained in:
parent
369332db26
commit
6d98375adb
29 changed files with 421 additions and 661 deletions
141
cmd/api.go
141
cmd/api.go
|
@ -5,9 +5,6 @@ import (
|
|||
"fmt"
|
||||
"log"
|
||||
|
||||
"codeberg.org/vlbeaudoin/serpents"
|
||||
"git.agecem.com/agecem/bottin/v6/data"
|
||||
"git.agecem.com/agecem/bottin/v6/handlers"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
"github.com/spf13/cobra"
|
||||
|
@ -19,14 +16,51 @@ var (
|
|||
apiKey string
|
||||
)
|
||||
|
||||
const (
|
||||
ViperAPIPort string = "api.port"
|
||||
FlagAPIPort string = "api-port"
|
||||
DefaultAPIPort int = 1312
|
||||
DescriptionAPIPort string = "API server port"
|
||||
|
||||
ViperAPIKey string = "api.key"
|
||||
FlagAPIKey string = "api-key"
|
||||
DefaultAPIKey string = "bottin"
|
||||
DescriptionAPIKey string = "API server key. Leave empty for no key auth (not recommended)"
|
||||
|
||||
ViperDBDatabase string = "db.database"
|
||||
FlagDBDatabase string = "db-database"
|
||||
DefaultDBDatabase string = "bottin"
|
||||
DescriptionDBDatabase string = "Postgres database"
|
||||
|
||||
ViperDBHost string = "db.host"
|
||||
FlagDBHost string = "db-host"
|
||||
DefaultDBHost string = "db"
|
||||
DescriptionDBHost string = "Postgres host"
|
||||
|
||||
ViperDBPassword string = "db.password"
|
||||
FlagDBPassword string = "db-password"
|
||||
DefaultDBPassword string = "bottin"
|
||||
DescriptionDBPassword string = "Postgres password"
|
||||
|
||||
ViperDBPort string = "db.port"
|
||||
FlagDBPort string = "db-port"
|
||||
DefaultDBPort int = 5432
|
||||
DescriptionDBPort string = "Postgres port"
|
||||
|
||||
ViperDBUser string = "db.user"
|
||||
FlagDBUser string = "db-user"
|
||||
DefaultDBUser string = "bottin"
|
||||
DescriptionDBUser string = "Postgres user"
|
||||
)
|
||||
|
||||
// apiCmd represents the api command
|
||||
var apiCmd = &cobra.Command{
|
||||
Use: "api",
|
||||
Short: "Démarrer le serveur API",
|
||||
Args: cobra.ExactArgs(0),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
apiKey = viper.GetString("api.key")
|
||||
apiPort = viper.GetInt("api.port")
|
||||
apiKey = viper.GetString(ViperAPIKey)
|
||||
apiPort = viper.GetInt(ViperAPIPort)
|
||||
|
||||
e := echo.New()
|
||||
|
||||
|
@ -41,40 +75,42 @@ var apiCmd = &cobra.Command{
|
|||
}
|
||||
|
||||
// DataClient
|
||||
/*
|
||||
client, err := data.NewDataClientFromViper()
|
||||
if err != nil {
|
||||
log.Fatalf("Could not establish database connection.\n Error: %s\n", err)
|
||||
}
|
||||
defer client.DB.Close()
|
||||
|
||||
client, err := data.NewDataClientFromViper()
|
||||
if err != nil {
|
||||
log.Fatalf("Could not establish database connection.\n Error: %s\n", err)
|
||||
}
|
||||
defer client.DB.Close()
|
||||
err = client.DB.Ping()
|
||||
if err != nil {
|
||||
log.Fatalf("Database was supposed to be ready but Ping() failed.\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)
|
||||
}
|
||||
|
||||
_, err = client.Seed()
|
||||
if err != nil {
|
||||
log.Fatalf("Error during client.Seed(): %s", err)
|
||||
}
|
||||
|
||||
h := handlers.New(client)
|
||||
_, err = client.Seed()
|
||||
if err != nil {
|
||||
log.Fatalf("Error during client.Seed(): %s", err)
|
||||
}
|
||||
*/
|
||||
|
||||
// Routes
|
||||
/*
|
||||
h := handlers.New(client)
|
||||
|
||||
e.GET("/v6/health/", h.GetHealth)
|
||||
e.GET("/v7/health/", h.GetHealth)
|
||||
|
||||
e.POST("/v6/membres/", h.PostMembres)
|
||||
e.POST("/v7/membres/", h.PostMembres)
|
||||
|
||||
e.GET("/v6/membres/", h.ListMembres)
|
||||
e.GET("/v7/membres/", h.ListMembres)
|
||||
|
||||
e.GET("/v6/membres/:membre_id/", h.ReadMembre)
|
||||
e.GET("/v7/membres/:membre_id/", h.ReadMembre)
|
||||
|
||||
e.PUT("/v6/membres/:membre_id/prefered_name/", h.PutMembrePreferedName)
|
||||
e.PUT("/v7/membres/:membre_id/prefered_name/", h.PutMembrePreferedName)
|
||||
|
||||
e.POST("/v6/programmes/", h.PostProgrammes)
|
||||
e.POST("/v7/programmes/", h.PostProgrammes)
|
||||
|
||||
e.POST("/v6/seed/", h.PostSeed)
|
||||
e.POST("/v7/seed/", h.PostSeed)
|
||||
*/
|
||||
|
||||
// Execution
|
||||
|
||||
|
@ -86,37 +122,44 @@ func init() {
|
|||
rootCmd.AddCommand(apiCmd)
|
||||
|
||||
// api.key
|
||||
serpents.String(apiCmd.Flags(),
|
||||
"api.key", "api-key", "bottin",
|
||||
"API server key. Leave empty for no key auth")
|
||||
apiCmd.Flags().String(FlagAPIKey, DefaultAPIKey, DescriptionAPIKey)
|
||||
if err := viper.BindPFlag(ViperAPIKey, apiCmd.Flags().Lookup(FlagAPIKey)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// api.port
|
||||
serpents.Int(apiCmd.Flags(),
|
||||
"api.port", "api-port", 1312,
|
||||
"API server port")
|
||||
apiCmd.Flags().Int(FlagAPIPort, DefaultAPIPort, DescriptionAPIPort)
|
||||
if err := viper.BindPFlag(ViperAPIPort, apiCmd.Flags().Lookup(FlagAPIPort)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// db.database
|
||||
serpents.String(apiCmd.Flags(),
|
||||
"db.database", "db-database", "bottin",
|
||||
"Postgres database")
|
||||
apiCmd.Flags().String(FlagDBDatabase, DefaultDBDatabase, DescriptionDBDatabase)
|
||||
if err := viper.BindPFlag(ViperDBDatabase, apiCmd.Flags().Lookup(FlagDBDatabase)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// db.host
|
||||
serpents.String(apiCmd.Flags(),
|
||||
"db.host", "db-host", "db",
|
||||
"Postgres host")
|
||||
apiCmd.Flags().String(FlagDBHost, DefaultDBHost, DescriptionDBHost)
|
||||
if err := viper.BindPFlag(ViperDBHost, apiCmd.Flags().Lookup(FlagDBHost)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// db.password
|
||||
serpents.String(apiCmd.Flags(),
|
||||
"db.password", "db-password", "bottin",
|
||||
"Postgres password")
|
||||
apiCmd.Flags().String(FlagDBPassword, DefaultDBPassword, DescriptionDBPassword)
|
||||
if err := viper.BindPFlag(ViperDBPassword, apiCmd.Flags().Lookup(FlagDBPassword)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// db.port
|
||||
serpents.Int(apiCmd.Flags(),
|
||||
"db.port", "db-port", 5432,
|
||||
"Postgres port")
|
||||
apiCmd.Flags().Int(FlagDBPort, DefaultDBPort, DescriptionDBPort)
|
||||
if err := viper.BindPFlag(ViperDBPort, apiCmd.Flags().Lookup(FlagDBPort)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// db.user
|
||||
serpents.String(apiCmd.Flags(),
|
||||
"db.user", "db-user", "bottin",
|
||||
"Postgres user")
|
||||
apiCmd.Flags().String(FlagDBUser, DefaultDBUser, DescriptionDBUser)
|
||||
if err := viper.BindPFlag(ViperDBUser, apiCmd.Flags().Lookup(FlagDBUser)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ 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",
|
||||
Short: "Bottin étudiant de l'AGECEM",
|
||||
}
|
||||
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
|
|
135
cmd/web.go
135
cmd/web.go
|
@ -7,12 +7,7 @@ import (
|
|||
"html/template"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"codeberg.org/vlbeaudoin/serpents"
|
||||
"git.agecem.com/agecem/bottin/v6/data"
|
||||
"git.agecem.com/agecem/bottin/v6/web"
|
||||
"git.agecem.com/agecem/bottin/v6/web/webhandlers"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
"github.com/spf13/cobra"
|
||||
|
@ -29,6 +24,43 @@ var (
|
|||
webApiProtocol string
|
||||
)
|
||||
|
||||
const (
|
||||
viperWebUser string = "web.user"
|
||||
flagWebUser string = "web-user"
|
||||
defaultWebUser string = "bottin"
|
||||
descriptionWebUser string = "Web client basic auth user"
|
||||
|
||||
viperWebPassword string = "web.password"
|
||||
flagWebPassword string = "web-password"
|
||||
defaultWebPassword string = "bottin"
|
||||
descriptionWebPassword string = "Web client basic auth password"
|
||||
|
||||
viperWebPort string = "web.port"
|
||||
flagWebPort string = "web-port"
|
||||
defaultWebPort int = 2312
|
||||
descriptionWebPort string = "Web client port"
|
||||
|
||||
viperWebAPIHost string = "api.host"
|
||||
flagWebAPIHost string = "api-host"
|
||||
defaultWebAPIHost string = "api"
|
||||
descriptionWebAPIHost string = "Target API server host"
|
||||
|
||||
viperWebAPIKey string = "api.key"
|
||||
flagWebAPIKey string = "api-key"
|
||||
defaultWebAPIKey string = "bottin"
|
||||
descriptionWebAPIKey string = "Target API server key"
|
||||
|
||||
viperWebAPIPort string = "api.port"
|
||||
flagWebAPIPort string = "api-port"
|
||||
defaultWebAPIPort int = 1312
|
||||
descriptionWebAPIPort string = "Target API server port"
|
||||
|
||||
viperWebAPIProtocol string = "api.protocol"
|
||||
flagWebAPIProtocol string = "api-protocol"
|
||||
defaultWebAPIProtocol string = "http"
|
||||
descriptionWebAPIProtocol string = "Target API server protocol (http/https)"
|
||||
)
|
||||
|
||||
var templatesFS embed.FS
|
||||
|
||||
type Template struct {
|
||||
|
@ -45,27 +77,28 @@ var webCmd = &cobra.Command{
|
|||
Short: "Démarrer le client web",
|
||||
Args: cobra.ExactArgs(0),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
webApiHost = viper.GetString("web.api.host")
|
||||
webApiKey = viper.GetString("web.api.key")
|
||||
webApiPort = viper.GetInt("web.api.port")
|
||||
webApiProtocol = viper.GetString("web.api.protocol")
|
||||
webPassword = viper.GetString("web.password")
|
||||
webPort = viper.GetInt("web.port")
|
||||
webUser = viper.GetString("web.user")
|
||||
webApiHost = viper.GetString(viperWebAPIHost)
|
||||
webApiKey = viper.GetString(viperWebAPIKey)
|
||||
webApiPort = viper.GetInt(viperWebAPIPort)
|
||||
webApiProtocol = viper.GetString(viperWebAPIProtocol)
|
||||
webPassword = viper.GetString(viperWebPassword)
|
||||
webPort = viper.GetInt(viperWebPort)
|
||||
webUser = viper.GetString(viperWebUser)
|
||||
|
||||
// Ping API server
|
||||
/*
|
||||
client := http.DefaultClient
|
||||
defer client.CloseIdleConnections()
|
||||
|
||||
client := http.DefaultClient
|
||||
defer client.CloseIdleConnections()
|
||||
apiClient := data.NewApiClient(client, webApiKey, webApiHost, webApiProtocol, webApiPort)
|
||||
|
||||
apiClient := data.NewApiClient(client, webApiKey, webApiHost, webApiProtocol, webApiPort)
|
||||
pingResult, err := apiClient.GetHealth()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
pingResult, err := apiClient.GetHealth()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Println(pingResult)
|
||||
log.Println(pingResult)
|
||||
*/
|
||||
|
||||
e := echo.New()
|
||||
|
||||
|
@ -88,11 +121,12 @@ var webCmd = &cobra.Command{
|
|||
e.Renderer = t
|
||||
|
||||
// Routes
|
||||
/*
|
||||
handler := webhandlers.Handler{APIClient: apiClient}
|
||||
|
||||
handler := webhandlers.Handler{APIClient: apiClient}
|
||||
|
||||
e.GET("/", handler.GetIndex)
|
||||
e.GET("/membre/", handler.GetMembre)
|
||||
e.GET("/", handler.GetIndex)
|
||||
e.GET("/membre/", handler.GetMembre)
|
||||
*/
|
||||
|
||||
// Execution
|
||||
|
||||
|
@ -103,40 +137,47 @@ var webCmd = &cobra.Command{
|
|||
|
||||
func init() {
|
||||
rootCmd.AddCommand(webCmd)
|
||||
templatesFS = web.GetTemplates()
|
||||
//templatesFS = web.GetTemplates()
|
||||
|
||||
// web.api.host
|
||||
serpents.String(webCmd.Flags(),
|
||||
"web.api.host", "web-api-host", "api",
|
||||
"Remote API server host")
|
||||
webCmd.Flags().String(flagWebAPIHost, defaultWebAPIHost, descriptionWebAPIHost)
|
||||
if err := viper.BindPFlag(viperWebAPIHost, webCmd.Flags().Lookup(flagWebAPIHost)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// web.api.key
|
||||
serpents.String(webCmd.Flags(),
|
||||
"web.api.key", "web-api-key", "bottin",
|
||||
"Remote API server key")
|
||||
webCmd.Flags().String(flagWebAPIKey, defaultWebAPIKey, descriptionWebAPIKey)
|
||||
if err := viper.BindPFlag(viperWebAPIKey, webCmd.Flags().Lookup(flagWebAPIKey)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// web.api.protocol
|
||||
serpents.String(webCmd.Flags(),
|
||||
"web.api.protocol", "web-api-protocol", "http",
|
||||
"Remote API server protocol")
|
||||
webCmd.Flags().String(flagWebAPIProtocol, defaultWebAPIProtocol, descriptionWebAPIProtocol)
|
||||
if err := viper.BindPFlag(viperWebAPIProtocol, webCmd.Flags().Lookup(flagWebAPIProtocol)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// web.api.port
|
||||
serpents.Int(webCmd.Flags(),
|
||||
"web.api.port", "web-api-port", 1312,
|
||||
"Remote API server port")
|
||||
webCmd.Flags().Int(flagWebAPIPort, defaultWebAPIPort, descriptionWebAPIPort)
|
||||
if err := viper.BindPFlag(viperWebAPIPort, webCmd.Flags().Lookup(flagWebAPIPort)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// web.password
|
||||
serpents.String(webCmd.Flags(),
|
||||
"web.password", "web-password", "bottin",
|
||||
"Web client password")
|
||||
webCmd.Flags().String(flagWebPassword, defaultWebPassword, descriptionWebPassword)
|
||||
if err := viper.BindPFlag(viperWebPassword, webCmd.Flags().Lookup(flagWebPassword)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// web.port
|
||||
serpents.Int(webCmd.Flags(),
|
||||
"web.port", "web-port", 2312,
|
||||
"Web client port")
|
||||
webCmd.Flags().Int(flagWebPort, defaultWebPort, descriptionWebPort)
|
||||
if err := viper.BindPFlag(viperWebPort, webCmd.Flags().Lookup(flagWebPort)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// web.user
|
||||
serpents.String(webCmd.Flags(),
|
||||
"web.user", "web-user", "bottin",
|
||||
"Web client user")
|
||||
webCmd.Flags().String(flagWebUser, defaultWebUser, descriptionWebUser)
|
||||
if err := viper.BindPFlag(viperWebUser, webCmd.Flags().Lookup(flagWebUser)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue